Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Architecture 延迟加载BusinessObject中的子列表_Architecture_Lazy Loading_N Tier Architecture - Fatal编程技术网

Architecture 延迟加载BusinessObject中的子列表

Architecture 延迟加载BusinessObject中的子列表,architecture,lazy-loading,n-tier-architecture,Architecture,Lazy Loading,N Tier Architecture,我使用的是3层(UI、BLL、DAL)架构,并在这些层之间使用BusinessObjects(又称数据传输对象-DTO)。假设我有以下DTO: public class EmployeeDTO { public int EmpID { get; set; } public string Name { get; set; } public ICollection<EmailDTO> EmailList { get; set; } } public class

我使用的是3层(UI、BLL、DAL)架构,并在这些层之间使用BusinessObjects(又称数据传输对象-DTO)。假设我有以下DTO:

public class EmployeeDTO
{
    public int EmpID { get; set; }
    public string Name { get; set; }
    public ICollection<EmailDTO> EmailList { get; set; }
}

public class EmailDTO
{
    public int EmailID { get; set; }
    public string Message { get; set; }
}
BLL 达尔
现在在DAL级别,当我使用empDTO.EmailList()时,它应该延迟加载数据。我怎么能这样做呢。我需要一个干净的解决方案(请注意,我没有使用任何ORM)。谢谢。

首先-将电子邮件声明为虚拟:

public class EmployeeDTO
{
    public int EmpID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EmailDTO> EmailList { get; set; }
}
最后-从DAL返回
LazyEmployeeDTO
,但确保客户端依赖于simple
EmployeeDTO

public EmployeeDTO GetEmployeeByID (id)
{
    reader = get employee data by id...

    EmployeeDTO empDTO = new LazyEmployeeDTO();
    empDTO.EmpID = reader[1];
    empDTO.Name  = reader[2];
    // do not load emails
    return empDTO;
}

其余的代码应该保持原样。此懒惰员工将充当基本员工的代理,客户端将不知道他们没有使用
EmployeeDTO

您的员工DTO可能需要某种回调来获取下一组记录。感谢回复。这澄清了很多事情,但还有几个问题(对不起,我是这个概念的新手,所以请接受我的问题)。1) 调用代码“EmployeDTO empDTO=new LazyEmployeeDTO();”时如何传递id。2) 你说从达尔回来是什么意思。3) 在UI中,我们如何实际调用EmailList集合。4). 在本例中,我们有一个子集合。如果我们有n个子集合,那么我们必须创建n个继承类。感谢您的回答。1)您不需要传递id。保持原样-
empDTO.EmpID=reader.GetInt32(0)
2)请参阅最后一个代码块-
LazyEmployeeDTO
实例已创建,但已分配给
EmployeeDTO
变量。方法还返回
EmployeeDTO
type。3) 与您在
EmployeeDTO
class 4)中使用的方法相同,如果您有N个子集合,则可以在单个
LazyEmployeeDTO
类中覆盖所有这些集合。
public EmployeeDTO GetEmployeeByID (id)
{
    reader = get employee data by id...

    EmployeeDTO empDTO = new EmployeeDTO ();
    empDTO.EmpID = reader [1];
    empDTO.Name  = reader [2];
    // I can load the email list but i dont
    return empDTO;
}
public class EmployeeDTO
{
    public int EmpID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<EmailDTO> EmailList { get; set; }
}
public class LazyEmployeeDTO : EmployeeDTO
{
    public override ICollection<EmailDTO> EmailList
    {
        get 
        {
            if (base.EmailList == null)
               base.EmailList = LoadEmails();

            return base.EmailList;
        }

        set { base.EmailList = value; }
    }

    private ICollection<EmailDTO> LoadEmails()
    {
        var emails = new List<EmailDTO>();
        // get emails by EmpID from database
        return emails;            
    }
}
public EmployeeDTO GetEmployeeByID (id)
{
    reader = get employee data by id...

    EmployeeDTO empDTO = new LazyEmployeeDTO();
    empDTO.EmpID = reader[1];
    empDTO.Name  = reader[2];
    // do not load emails
    return empDTO;
}