对象依赖于另一个对象 - 何时加载其属性 [英] Object depending on another object - when to load its properties

查看:49
本文介绍了对象依赖于另一个对象 - 何时加载其属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一类员工:

public class Employee
 {
   public int ID { get; set; }
   public string FirstName { get; set; }
   public Employee(int ID, string FirstName)
       {
         this.ID = ID;
         this.FirstName = FirstName;
       }
 }

员工的数据从数据访问层 (DAL) 加载,这是另一个单独的类.

The data for the employee is loaded in from a data access layer (DAL), which is another seperate class.

后来我决定我需要另一个属性,即员工的部门,但是部门本身就是一个巨大的类别,因为它有自己的属性.所以我最终为它创建了一个类:

Later I decide that I need another property namely a Department for the employee, however the department is a huge category on its own as it has its own properties. So I end up making a class for it:

class Department
 {
  public string DepartmentID { get; set;}
  public string CostCenter { get; set; }
  public bool hasManager { get; set; }
  //more code
  //constructor for department
 }

所以我然后从上面更改我的员工类以包含一个部门的实例:

So I then change my employee class from above to include an instance of a department:

public class Employee {
  //existing code for an employee above PLUS the below
  public Department d { get; set; }
}

所以现在我很好,我有一个员工类,其中有一个与之相关的部门.当我调用我的 DAL 类来获取我员工的数据时,这可以在 SQL 查询中返回实际部门.所以我可以很容易地分配类部门的DepartmentID.但是我应该何时/如何分配 Department 类的所有其他属性.

So now I'm good I have an employee class with a department associated with it. When I call my DAL class to get the data for my employee this can return the actual department in the SQL query. So I can assign the DepartmentID of the class Department easily. But when / how should I assign all the other properties of the Department class.

此外,CostCenter、hasManager 等的属性都存储在数据库层.如果我最初只是取回 DepartmentID,我是否应该再次调用 DAL 类以获取该部门的所有相关信息?如果是这样,我应该在哪里调用它,我认为从我的部门构造函数调用 DAL 类不是一个好主意.我知道我可以在我的两个表之间创建关系并从部门获取相关信息,然后我可以在员工构造函数中实例化我的部门.但是员工类有很多属性,已经有很多从数据库返回的字段,所以如果我这样做,我的构造函数可能会变得越来越大......

In addition, the properties for CostCenter, hasManager, etc are all stored in the database layer. If I originally only am getting back the DepartmentID should I call the DAL class again to get all the relevant information for the department? If so where should I call that, I didnt think calling the DAL class from my department constructor was a good idea. I know I could just create a relationship between my two tables and get relevant information from the department and then I can instantiate my department within the employee constructor. But the employee class has a LOT of properties already lots of fields coming back from the database, so if I did this my constructor may become larger and larger....

推荐答案

好吧,你有选择……

我假设你有你自己的 DAL,因为 EF 和 Linq2Sql 有内置的选项.

I assume you have your own DAL beacuse EF and Linq2Sql have built-in options for this.

  1. 急切加载 - 将人员加载到部门.
  2. 按需加载:if (p.d == null) ...
  3. 延迟加载:在 d 属性的 getter 中按需执行.
  1. Eager loading - load the Department with the Person.
  2. On demand loading: if (p.d == null) ...
  3. Lazy loading: do the on-demand inside the getter of the d property.

这篇关于对象依赖于另一个对象 - 何时加载其属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆