public class CompanyRepository : SqlCeRepositoryBase<Company>, ICompanyRepository
{
#region Private Fields
#endregion
#region Public Constructors
public CompanyRepository()
: this(null)
{
}
public CompanyRepository(IUnitOfWork unitOfWork)
: base(unitOfWork)
{
}
#endregion
#region BuildChildCallbacks
protected override void BuildChildCallbacks()
{
this.ChildCallbacks.Add("addresses",
delegate(Company company, object childKeyName)
{
this.AppendAddresses(company);
});
}
#endregion
#region GetBaseQuery
protected override string GetBaseQuery()
{
return "SELECT * FROM Company";
}
#endregion
#region GetBaseWhereClause
protected override string GetBaseWhereClause()
{
return " WHERE CompanyID = '{0}';";
}
#endregion
#region Unit of Work Implementation
protected override void PersistNewItem(Company item)
{
StringBuilder builder = new StringBuilder(100);
builder.Append(string.Format("INSERT INTO Company ({0},{1},{2},{3},{4},{5},{6}) ",
CompanyFactory.FieldNames.CompanyId,
CompanyFactory.FieldNames.CompanyName,
CompanyFactory.FieldNames.CompanyShortName,
CompanyFactory.FieldNames.Phone,
CompanyFactory.FieldNames.Fax,
CompanyFactory.FieldNames.Url,
CompanyFactory.FieldNames.Remarks));
builder.Append(string.Format("VALUES ({0},{1},{2},{3},{4},{5},{6});",
DataHelper.GetSqlValue(item.Key),
DataHelper.GetSqlValue(item.Name),
DataHelper.GetSqlValue(item.Abbreviation),
DataHelper.GetSqlValue(item.PhoneNumber),
DataHelper.GetSqlValue(item.FaxNumber),
DataHelper.GetSqlValue(item.Url),
DataHelper.GetSqlValue(item.Remarks)));
this.Database.ExecuteNonQuery(
this.Database.GetSqlStringCommand(builder.ToString()));
// Now do the addresses
this.InsertAddresses(item);
}
protected override void PersistUpdatedItem(Company item)
{
StringBuilder builder = new StringBuilder(100);
builder.Append("UPDATE Company SET ");
builder.Append(string.Format("{0} = {1}",
CompanyFactory.FieldNames.CompanyName,
DataHelper.GetSqlValue(item.Name)));
builder.Append(string.Format(",{0} = {1}",
CompanyFactory.FieldNames.CompanyShortName,
DataHelper.GetSqlValue(item.Abbreviation)));
builder.Append(string.Format(",{0} = {1}",
CompanyFactory.FieldNames.Phone,
DataHelper.GetSqlValue(item.PhoneNumber)));
builder.Append(string.Format(",{0} = {1}",
CompanyFactory.FieldNames.Fax,
DataHelper.GetSqlValue(item.FaxNumber)));
builder.Append(string.Format(",{0} = {1}",
CompanyFactory.FieldNames.Url,
DataHelper.GetSqlValue(item.Url)));
builder.Append(string.Format(",{0} = {1}",
CompanyFactory.FieldNames.Remarks,
DataHelper.GetSqlValue(item.Remarks)));
builder.Append(" ");
builder.Append(this.BuildBaseWhereClause(item.Key));
this.Database.ExecuteNonQuery(
this.Database.GetSqlStringCommand(builder.ToString()));
// Now do the addresses
// First, delete the existing ones
this.DeleteAddresses(item);
// Now, add the current ones
this.InsertAddresses(item);
}
protected override void PersistDeletedItem(Company item)
{
// Delete the company addresses first
this.DeleteAddresses(item);
// Now delete the company
string query = string.Format("DELETE FROM Company {0}",
this.BuildBaseWhereClause(item.Key));
this.Database.ExecuteNonQuery(
this.Database.GetSqlStringCommand(query));
}
#endregion
#region Private Callback and Helper Methods
private void DeleteAddresses(Company company)
{
string query = string.Format("DELETE FROM CompanyAddress {0}",
this.BuildBaseWhereClause(company.Key));
this.Database.ExecuteNonQuery(
this.Database.GetSqlStringCommand(query));
}
private void InsertAddresses(Company company)
{
foreach (Address address in company.Addresses)
{
this.InsertAddress(address, company.Key,
(company.HeadquartersAddress == address));
}
}
private void InsertAddress(Address address, object key,
bool isHeadquartersAddress)
{
StringBuilder builder = new StringBuilder(100);
builder.Append(string.Format("INSERT INTO CompanyAddress ({0},{1},{2},{3},{4},{5}) ",
CompanyFactory.FieldNames.CompanyId,
AddressFactory.FieldNames.Street,
AddressFactory.FieldNames.City,
AddressFactory.FieldNames.State,
AddressFactory.FieldNames.PostalCode,
CompanyFactory.FieldNames.IsHeadquarters));
builder.Append(string.Format("VALUES ({0},{1},{2},{3},{4},{5});",
DataHelper.GetSqlValue(key),
DataHelper.GetSqlValue(address.Street),
DataHelper.GetSqlValue(address.City),
DataHelper.GetSqlValue(address.State),
DataHelper.GetSqlValue(address.PostalCode),
DataHelper.GetSqlValue(isHeadquartersAddress)));
this.Database.ExecuteNonQuery(
this.Database.GetSqlStringCommand(builder.ToString()));
}
private void AppendAddresses(Company company)
{
string sql = string.Format
("SELECT * FROM CompanyAddress WHERE CompanyID = '{0}'",
company.Key);
using (IDataReader reader = this.ExecuteReader(sql))
{
Address address = null;
while (reader.Read())
{
address = AddressFactory.BuildAddress(reader);
company.Addresses.Add(address);
if (CompanyFactory.IsHeadquartersAddress(reader))
{
company.HeadquartersAddress = address;
}
}
}
}
#endregion
}
The function that I cannot understand is BuildChildCallbacks function at the CompanyRepository
class. What is its purpose? Especially I wonder the purpose of delegate action.
First, I am also a beginner. I have not read this book. This is just my personal thought and is for reference only.
Here the BuildChildCallbacks method allows each subclass object that inherits from
SqlCeRepositoryBase<Company> to access each other. The parent class SqlRepositoryBase<Company>
adds a linked list pointer for each subclass object, which is stored in its field, and then each subclass can access the parent class through this pointer, and the parent class object can also access the subclass method. In addition, It
is also implemented that subclass objects can access each other.
Feel Free to let me know if you have any questions.
Best Regards
Abraham
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
44 Points
106 Posts
About the Term "ChildCallbacks" and Explanation of a Function
Jul 15, 2018 06:07 PM|varoulscuprens|LINK
Hi There;
I am reading the book ".NET Domain-Driven Design" written by Tim McCarty, and I cannot understand the code below. Could you please explain?
First of all, Company class shall be known:
Here is the SqlCeRepositoryBase class that has been used in the Contact Repository class:
Here is the ContactRepository class
The function that I cannot understand is BuildChildCallbacks function at the CompanyRepository class. What is its purpose? Especially I wonder the purpose of delegate action.
Thanks in advance.
Member
740 Points
321 Posts
Re: About the Term "ChildCallbacks" and Explanation of a Function
Jul 16, 2018 03:39 PM|Abraham Qian|LINK
Hi Varoulscuprens,
First, I am also a beginner. I have not read this book. This is just my personal thought and is for reference only.
Here the BuildChildCallbacks method allows each subclass object that inherits from SqlCeRepositoryBase<Company> to access each other. The parent class SqlRepositoryBase<Company> adds a linked list pointer for each subclass object, which is stored in its field, and then each subclass can access the parent class through this pointer, and the parent class object can also access the subclass method. In addition, It is also implemented that subclass objects can access each other.
Feel Free to let me know if you have any questions.
Best Regards
Abraham