In our system we have our domain which is just a bunch of bog standard related poco's, and we use NHibernate with all the lazy loading etc to hydrate and persist the objects data
Lets say I have the structure in the domain. (This is a pure example I created on the fly)
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Postcode { get; set; }
public Region Region { get; set; }
}
public class Region
{
public int Id { get; set; }
public string Name { get; set; }
public CountryCode CountryCode { get; set; }
}
public class CountryCode
{
public string Name { get; set; }
public string Code { get; set; }
public CostCode CostCode { get; set; }
}
public class CostCode
{
public string Name { get; set; }
public string Description { get; set; }
}
Now say the Customer wants to get the Name of the CostCode.Name value for itself, would it be better to have a method in Customer that does this
public string GetCountryCostName()
{
return Address.Region.CountryCode.CostCode.Name;
}
or would it be better, to do it so Customer and the other objects have functions to get the information as such
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
public string GetCountryCostName()
{
return Address.GetCountryCostCodeName();
}
}
public class Address
{
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Postcode { get; set; }
public Region Region { get; set; }
public string GetCountryCostCodeName()
{
return Region.GetCountryCostCodeName();
}
}
public class Region
{
public int Id { get; set; }
public string Name { get; set; }
public CountryCode Country { get; set; }
public string GetCountryCostCodeName()
{
return Country.GetCostCodeName();
}
}
public class CountryCode
{
public string Name { get; set; }
public string Code { get; set; }
public CostCode CostCode { get; set; }
public string GetCostCodeName()
{
return CostCode.Name;
}
}
public class CostCode
{
public string Name { get; set; }
public string Description { get; set; }
}
now with the first way, Nhibernate will create a single sql to hydrate all the Proxies it creates, but the second way if I am correct in how lazy loading works with NHibernate it will create lots of smaller sql statements as function is accessed. So which way would be the best approach from and OO design and performance points of view?
regards
CD