C# 如何在不从我的对象调用我的业务对象层的情况下延迟加载
简单问题 我有一个阶级发言人,他拥有另一个阶级发言人部门的成员作为财产。 就守则而言:C# 如何在不从我的对象调用我的业务对象层的情况下延迟加载,c#,oop,lazy-loading,C#,Oop,Lazy Loading,简单问题 我有一个阶级发言人,他拥有另一个阶级发言人部门的成员作为财产。 就守则而言: 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
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:
在我的DAL中,我只是调用一个存储过程,从一个人的表中提取这些信息。唯一的问题是我不拉部门数据,因为它是一个相当大的结构
因此,我的问题是,在对象不知道或调用业务对象层的情况下,如何在get属性中延迟加载这个department对象。此外,我认为将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; }
第二次编辑
基本上这就是我所拥有的:
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; }
我的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;
}
您可以为person对象提供一个回调方法,该方法可以为此人加载部门:
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;
}
}
}
也可以利用
Lazy
类型来处理延迟初始化。可以很好地与委托一起工作。@Guffa-我想我不明白这一点……让我们假设我创建了一个person对象,而现在department对象为空。在你说的属性中,\u department=GetDepartment()代码>在哪里定义GetDepartment?如果你说在person类中,那么这并不能解决问题,因为这个方法将不得不调用业务对象层…好的,在重新阅读它之后,它是有意义的,我看到它变成了一种委托类型,并在需要时调用BOL。非常好的解决方案+1。@Guffa-一个问题是,是否可以将参数传递给GetDepartment
?下面是我尝试的代码:e.GetDepartment=()=>BOL.GetFunctionalGroup((string)reader[“FunctionalGroupID]”)代码>错误为错误1代表“System.Action”不需要0个参数我意识到我的错误,guffa获得了积分:代码根据上述编辑进行了更改。
public static FunctionalGroup GetFunctionalGroup(string fgID)
{
return DAL.GetFunctionalGroup(fgID);
}
/// <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;
}
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;
}
}
}
p.GetDepartment = () => BOL.GetDepartment(1);