如何延迟加载而不从我的对象调用我的业务对象层

发布于 2024-12-10 02:33:01 字数 5667 浏览 0 评论 0原文

简单的问题 我有一个班级说的人,他拥有另一个班级说的成员作为财产。 用代码术语来说:

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 对象紧密耦合是不好的做法。

换句话说,我不想在我的 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 Action does not take 0 argument

我尝试从 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; }

Simple question
I have a class say person who has as a property a member of another class say Department.
In code terms:

class person {
public string fname {get; set;}
public string lname {get; set;}
public Department d {get; set;}
}

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:

website:
Person p;
p = BOL.GetPerson(1); //call function that returns a person

And in my business object layer I simply do some business logic and call the data access layer like so:

BOL:

return DAL.GetPerson(1); //returns a person from my Data access layer

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...

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 Department object with the BOL object.

In other words I DONT want to do this in my person class:

public Department d {
  get 
     {
      if(d==null)
           {
            d = BOL.GetDepartmentInfo();
           }
     }
  set 
       {
        //some code
        }

That is a person class should only contain relevant information about a person, so it really should not know about the Business object layer.

How can I solve this problem?

Edit

Here is the property:

public FunctionalGroup Department
{
    get
    {
        if (Department == null)
        {
            Department = GetDepartment();
        }
    }
    set
    {
        Department = value;
    }
}

public Action<FunctionalGroup> GetDepartment { private get; set; }

This complains that Delegate Action does not take 0 arguments

I tried calling it from the BOL like so:

//assume already have an employee object
 e.GetDepartment = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);

Edit 2nd time

Basically here is what I have:

 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; }

My BOL class is trying to assign to this:

e.Department = (id) => BOL.GetFunctionalGroup(e.FunctionalGroupID);

My BOL class says:

public static FunctionalGroup GetFunctionalGroup(string fgID)
 {
  return DAL.GetFunctionalGroup(fgID);
 }

My DAL looks like this:

  /// <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;
        }

Final Edit

Ended up using my BOl with this:

e.GetDepartment = () => BOL.GetFunctionalGroup(e.FunctionalGroupID);

And my employee class with this:

 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; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多像笑话 2024-12-17 02:33:01

您可以为人员对象提供一个回调方法,该方法可以加载该人员的部门:

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);

You can give the person object a callback method that can load the department for that 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;
    }
  }

}

When you get the person object in the business layer:

p.GetDepartment = () => BOL.GetDepartment(1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文