Simple question
I have a class say person who has as a property a member of another class say Department.
In code terms:
class person {
public string fname {get; set;}
public string lname {get; set;}
public Department d {get; set;}
}
When I load a person I have my front end web site call my business object layer which then makes a call to my data acess layer, something to this effect:
website:
Person p;
p = BOL.GetPerson(1); //call function that returns a person
And in my business object layer I simply do some business logic and call the data access layer like so:
BOL:
return DAL.GetPerson(1); //returns a person from my Data access layer
Inside my DAL i’m simply calling a stored procedure which pulls this information from a person’s table. The only thing is I dont pull the department data because its a rather large structure…
So my question is how can i lazy load this department object in my get property WITHOUT my object knowing or calling the Business Object layer. In addition, i think it is bad practice to tightly couple a Department object with the BOL object.
In other words I DONT want to do this in my person class:
public Department d {
get
{
if(d==null)
{
d = BOL.GetDepartmentInfo();
}
}
set
{
//some code
}
That is a person class should only contain relevant information about a person, so it really should not know about the Business object layer.
How can I solve this problem?
Edit
Here is the property:
public FunctionalGroup Department
{
get
{
if (Department == null)
{
Department = GetDepartment();
}
}
set
{
Department = value;
}
}
public Action<FunctionalGroup> GetDepartment { private get; set; }
This complains that Delegate Action does not take 0 arguments
I tried calling it from the BOL like so:
//assume already have an employee object
e.GetDepartment = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);
Edit 2nd time
Basically here is what I have:
private FunctionalGroup _d = null;
public FunctionalGroup Department
{
get
{
if (_d == null)
{
_d = GetDepartment();
}
return _d;
}
set
{
_d = value;
}
}
// public Action<string, FunctionalGroup> GetDepartment { private get; set; }
public Func<FunctionalGroup> GetDepartment { private get; set; }
My BOL class is trying to assign to this:
e.Department = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);
My BOL class says:
public static FunctionalGroup GetFunctionalGroup(string fgID)
{
return DAL.GetFunctionalGroup(fgID);
}
My DAL looks like this:
/// <summary>
/// Returns a functional group object along with all of its properties, otherwise null.
/// </summary>
/// <param name="fgID">String representation of a functional group (ex: "A-AD-C")</param>
/// <returns>Functional group object with all associated properties, otherwise null.</returns>
public static FunctionalGroup GetFunctionalGroup(string fgID)
{
FunctionalGroup fg = null;
if (fgID.Length != 0)
{
//connString = the string of our database app found in the resource file
using (SqlConnection con = new SqlConnection(connString))
{
using (SqlCommand cmd = new SqlCommand("EMPDLL_selFunctionalGroupByFunctionalGroupID", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@FunctionalGroupID", SqlDbType.VarChar).Value = fgID;
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
if (reader.Read())
{
//found a func group
fg = new FunctionalGroup((string)reader["FunctionalGroupID"],
(long)reader["ClientID"],
(string)reader["CostCenter"],
(string)reader["Description"],
(string)reader["Comments"],
(string)reader["AddedBy"],
reader["DateAdded"] == DBNull.Value ? null : (DateTime?)reader["DateAdded"],
(string)reader["ModifiedBy"],
reader["DateModified"] == DBNull.Value ? null : (DateTime?)reader["DateModified"],
(bool)reader["Inactive"]);
}
}
}
}
}
}
return fg;
}
Final Edit
Ended up using my BOl with this:
e.GetDepartment = () => BOL.GetFunctionalGroup(e.FunctionalGroupID);
And my employee class with this:
private FunctionalGroup _d = null;
public FunctionalGroup Department
{
get
{
if (_d == null)
{
_d = GetDepartment();
}
return _d;
}
set
{
_d = value;
}
}
public Func<FunctionalGroup> GetDepartment { private get; set; }
You can give the person object a callback method that can load the department for that person:
When you get the person object in the business layer: