Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to lazy load without calling my business object layer from my object
    text
    copied!<p>Simple question I have a class say person who has as a property a member of another class say Department. In code terms:</p> <pre><code>class person { public string fname {get; set;} public string lname {get; set;} public Department d {get; set;} } </code></pre> <p>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:</p> <pre><code>website: Person p; p = BOL.GetPerson(1); //call function that returns a person </code></pre> <p>And in my business object layer I simply do some business logic and call the data access layer like so:</p> <p>BOL:</p> <pre><code>return DAL.GetPerson(1); //returns a person from my Data access layer </code></pre> <p>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...</p> <p>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 <code>Department</code> object with the <code>BOL</code> object.</p> <p>In other words I DONT want to do this in my person class:</p> <pre><code>public Department d { get { if(d==null) { d = BOL.GetDepartmentInfo(); } } set { //some code } </code></pre> <p>That is a <code>person</code> class should only contain relevant information about a person, so it really should not know about the Business object layer.</p> <p>How can I solve this problem?</p> <h2> Edit </h2> <p>Here is the property:</p> <pre><code>public FunctionalGroup Department { get { if (Department == null) { Department = GetDepartment(); } } set { Department = value; } } public Action&lt;FunctionalGroup&gt; GetDepartment { private get; set; } </code></pre> <p>This complains that <code>Delegate Action does not take 0 arguments</code></p> <p>I tried calling it from the BOL like so:</p> <pre><code>//assume already have an employee object e.GetDepartment = (id) =&gt; BOL.GetFunctionalGroup(e.FunctionalGroupID); </code></pre> <h1>Edit 2nd time</h1> <p>Basically here is what I have:</p> <pre><code> private FunctionalGroup _d = null; public FunctionalGroup Department { get { if (_d == null) { _d = GetDepartment(); } return _d; } set { _d = value; } } // public Action&lt;string, FunctionalGroup&gt; GetDepartment { private get; set; } public Func&lt;FunctionalGroup&gt; GetDepartment { private get; set; } </code></pre> <p>My BOL class is trying to assign to this:</p> <p><code>e.Department = (id) =&gt; BOL.GetFunctionalGroup(e.FunctionalGroupID);</code></p> <p>My BOL class says:</p> <pre><code>public static FunctionalGroup GetFunctionalGroup(string fgID) { return DAL.GetFunctionalGroup(fgID); } </code></pre> <p>My DAL looks like this:</p> <pre><code> /// &lt;summary&gt; /// Returns a functional group object along with all of its properties, otherwise null. /// &lt;/summary&gt; /// &lt;param name="fgID"&gt;String representation of a functional group (ex: "A-AD-C")&lt;/param&gt; /// &lt;returns&gt;Functional group object with all associated properties, otherwise null.&lt;/returns&gt; 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; } </code></pre> <h2>Final Edit</h2> <p>Ended up using my BOl with this:</p> <p><code>e.GetDepartment = () =&gt; BOL.GetFunctionalGroup(e.FunctionalGroupID);</code></p> <p>And my employee class with this:</p> <pre><code> private FunctionalGroup _d = null; public FunctionalGroup Department { get { if (_d == null) { _d = GetDepartment(); } return _d; } set { _d = value; } } public Func&lt;FunctionalGroup&gt; GetDepartment { private get; set; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload