简单的问题 我有一个类(class)说的人,他拥有另一个类(class)说的成员作为属性(property)。 用代码来说:
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
在我的业务对象层中,我只需执行一些业务逻辑并调用数据访问层,如下所示:
玻尔:
return DAL.GetPerson(1); //returns a person from my Data access layer
在我的 DAL 中,我只是调用一个存储过程,该过程从一个人的表中提取此信息。唯一的问题是我不提取部门数据,因为它的结构相当大......
所以我的问题是,在我的对象不知道或调用业务对象层的情况下,如何在我的 get 属性中延迟加载这个部门对象。此外,我认为将 Department
对象与 BOL
对象紧密耦合是不好的做法。
换句话说,我不想在我的 person 类中执行此操作:
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)操作不接受 0 个参数
我尝试从 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);
我的员工类(class)是这样的:
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; }
最佳答案
您可以为人员对象提供一个回调方法,该方法可以加载该人员的部门:
class person {
private Department _department = null;
public Func<Department> GetDepartment { private get; set; }
public string fname { get; set; }
public string lname { get; set; }
public Department d {
get {
if (_department == null) {
_department = GetDepartment();
}
return _department;
}
}
}
当业务层获取到person对象时:
p.GetDepartment = () => BOL.GetDepartment(1);
关于c# - 如何延迟加载而不从我的对象调用我的业务对象层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7770732/