I am following a book which uses delegate keyword (as per my understanding) as name of the function to be encapsulated in delegate (the one which is called using delegate object name/constructor). below is the code:
//Declaration of delegate object AppendChildData
public delegate void AppendChildData(T entityAggregate, object childEntityKeyValue);
//Dictionary of Delegate Objects
private Dictionary<string, AppendChildData> childCallbacks;
//Creation of dictionary object inside constructor of containing class. Also calling the BuildChildCallbacks() function
protected SqlRepositoryBase(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
this.childCallbacks = new Dictionary<string, AppendChildData>();
this.BuildChildCallbacks();
//Other Initializations
}
protected override void BuildChildCallbacks()
{
this.childCallbacks.Add("allowances", delegate(Project project, object childKeyName)
{
this.AppendProjectAllowances(project);
});
}
Now if seen carefully:
delegate(Project project, object childKeyName)
{
this.AppendProjectAllowances(project);
});
has been declared as value of dictionary childCallBacks inside BuildChildCallbacks() function. This value of dictionary childCallBacks is a function named delegate(). WHY? and how does .NET delas with usage of delegate keyword in this case?
The book i am following also uses function names instead of using delegate keyword, the complete code of BuildChlidCallBack is below:
protected override void BuildChildCallbacks()
{
this.ChildCallbacks.Add(ProjectFactory.FieldNames.OwnerCompanyId,
this.AppendOwner);
this.ChildCallbacks.Add(
ProjectFactory.FieldNames.ConstructionAdministratorEmployeeId,
this.AppendConstructionAdministrator);
this.ChildCallbacks.Add(ProjectFactory.FieldNames.PrincipalEmployeeId,
this.AppendPrincipal);
this.ChildCallbacks.Add("allowances",
delegate(Project project, object childKeyName)
{
this.AppendProjectAllowances(project);
});
this.ChildCallbacks.Add("contracts",
delegate(Project project, object childKeyName)
{
this.AppendContracts(project);
});
this.ChildCallbacks.Add("contacts",
delegate(Project project, object childKeyName)
{
this.AppendContacts(project);
});
}
on carefull observation i come to know that delegate keyword has been used in case where dictionary keys are not Foreign keys (not column of DataTable) and calling these delegates passes NULL to the second argument namely "childKeyName" of the delegate() function. The code that calls the delegate() functions using dictionary is below:
protected virtual T BuildEntityFromReader(IDataReader reader)
{
T entity = this.entityFactory.BuildEntity(reader);
if (this.childCallbacks != null && this.childCallbacks.Count > 0)
{
object childKeyValue = null;
DataTable columnData = reader.GetSchemaTable();
foreach (string childKeyName in this.childCallbacks.Keys)
{
if (DataHelper.ReaderContainsColumnName(columnData, childKeyName))
{
childKeyValue = reader[childKeyName];
}
else
{
childKeyValue = null;
}
this.childCallbacks[childKeyName](entity, childKeyValue);
}
}
return entity;
}
where reader in function is IDataReader which contains single record/entity/row from DataTable (sort of reader in function is reader.read).
My Question is that why delegate word has been used as name of function to assign to AppendChildData delegate-object in childCallBack dictionary, instead of a function name? (As it is compiling and running fine)
And, how does .NET delas with usage of delegate keyword in this case?