简单的问题 我有一个班级说有人作为财产,另一个班级的成员说部门。 在代码中:
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; }
答案 0 :(得分:3)
您可以为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;
}
}
}
当你在业务层获得person对象时:
p.GetDepartment = () => BOL.GetDepartment(1);