【发布时间】:2011-10-14 16:34:59
【问题描述】:
简单的问题 我有一个班级说人,他拥有另一个班级的成员说部门。 在代码方面:
class person {
public string fname {get; set;}
public string lname {get; set;}
public Department d {get; set;}
}
当我加载一个人时,我的前端网站调用我的业务对象层,然后调用我的数据访问层,大意是这样的:
website:
Person p;
p = BOL.GetPerson(1); //call function that returns a person
在我的业务对象层中,我只需执行一些业务逻辑并像这样调用数据访问层:
BOL:
return DAL.GetPerson(1); //returns a person from my Data access layer
在我的 DAL 中,我只是调用了一个存储过程,该过程从一个人的表中提取此信息。唯一的问题是我不提取部门数据,因为它的结构相当大......
所以我的问题是如何在我的对象不知道或调用业务对象层的情况下将这个部门对象延迟加载到我的 get 属性中。此外,我认为将Department 对象与BOL 对象紧密耦合是不好的做法。
换句话说,我不想在我的个人课程中这样做:
public Department d {
get
{
if(d==null)
{
d = BOL.GetDepartmentInfo();
}
}
set
{
//some code
}
那是一个person 类应该只包含一个人的相关信息,所以它真的不应该知道业务对象层。
我该如何解决这个问题?
编辑
这里是属性:
public FunctionalGroup Department
{
get
{
if (Department == null)
{
Department = GetDepartment();
}
}
set
{
Department = value;
}
}
public Action<FunctionalGroup> GetDepartment { private get; set; }
这抱怨Delegate Action does not take 0 arguments
我尝试像这样从 BOL 调用它:
//assume already have an employee object
e.GetDepartment = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);
第二次编辑
基本上这就是我所拥有的:
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; }
我的 BOL 类正在尝试分配给这个:
e.Department = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);
我的 BOL 课程说:
public static FunctionalGroup GetFunctionalGroup(string fgID)
{
return DAL.GetFunctionalGroup(fgID);
}
我的 DAL 如下所示:
/// <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;
}
最终编辑
最终使用了我的 BOl:
e.GetDepartment = () => BOL.GetFunctionalGroup(e.FunctionalGroupID);
还有我的员工班级:
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; }
【问题讨论】:
-
我意识到我的错误并且 guffa 得到了信任:代码已根据上面的编辑进行了更改。
标签: c# oop lazy-loading