IRepository骨料和延迟加载 [英] IRepository Aggregates and Lazy Loading

查看:150
本文介绍了IRepository骨料和延迟加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直拖网计算器和一般整天对IRepository模式试图更好地了解它之前,我尝试在愤怒使用互联网。

I've been trawling stackoverflow and the internet in general all day about the IRepository pattern trying to better understand it before I try and use it in anger.

从我读过(和请不要纠正我,如果我错了)一个repoistory封装访问其聚合根和子对象暴露,然后将其注入或嘲笑的通用接口。

From what I've read (and please do correct me if I'm mistaken) a repoistory encapsulates access to its aggregate root and child objects exposing a common interface that can then be injected or mocked.

所以,在你有你的聚合根对象的实例:

So in the instance where you have your aggregate root object:

class Employee {
    string FirstName;
    string LastName;
    IEnumerable<Address> Addresses;
    IEnumerable<PhoneNumber> PhoneNumbers;
}



那么它的子对象:

Then its child objects:

class Address {
    string BuildingName;
    ...etc
}

class PhoneNumber {
    string PhoneNo;
    ...etc
}



所以库看起来是这样的:

So the repository would look something like:

class EmployeeRepository : IRepository<Employee> {
    Employee Get(id) {
    ...does stuff, builds full Employee object including Addresses/Phone No's and returns
    }
}

不过,说我不想得到整个员工,说我只是想名姓平板员工记录,并能够在延迟加载其余的在后面。应如何实现呢?难道是允许有这样的:

But say I dont want to get the whole Employee, say I just want the flat Employee record with FirstName LastName and be able to lazy load in the rest in later. How should this be accomplished? Would it be permissible to have something like:

class EmployeeRepository : IRepository<Employee> {
    Employee Get(id) {
    ...does stuff and builds flat Employee object without Addresses and Phone Numbers
    }

    Employee GetAddresses (Employee emp) {
    ...
    }

    Employee GetPhoneNumbers (Employee emp) {
    ...
    }
}

这是确定或会被我打破了一些神圣不可侵犯的DDD规则和刻录在开发商地狱?延迟加载应该如何适应这种模式,再次试图寻找,但我所发现的是让NHibernate的/实体框架/ ORM为您代劳。

Is this ok or would I be breaking some sacrosanct DDD rule and burn in developer hell? How is lazy loading supposed to fit this model, again tried searching but all I've found is "let NHibernate/Entity Framework/ORM do it for you".

谢谢在前进。

ð。

推荐答案

真正延迟加载意味着你有你的聚合根的属性(你懒洋洋地风荷载的对象图和其他地方),这将是足够聪明来加载真正的实体,并用它替换代理到代理对象的引用时,要求的属性被调用它。使用类似城堡的动态代理或李林甫的动态代理是这样做,因为动态代理是复杂的野兽已经被那些家伙得到了很好的实施的最佳方式。

true lazy loading would mean that you have references to proxy objects in the properties of your aggregate root (and anywhere else in the object graph you wind up loading lazily) that would be smart enough to load the real entity and replace the proxy with it when properties are invoked that require it. using something like castle's dynamic proxy or linfu's dynamic proxy are the best way to do this because dynamic proxies are complex beasts that have been well implemented by those guys.

做你建议要求你的消费代码注意什么,并没有被加载并把负担用户了解的延迟加载,并认为它在客户端代码。用动态代理,你不必去想它。

doing what you have suggested requires that your consuming code be aware of what has and hasn't been loaded and puts the burden on the user to know about the lazy loading and think about it in the client code. with a dynamic proxy, you don't have to think about it.

真的,最好的答案,不过,是你已经发现之一。这是已经解决了的ORM的问题。使用NHibernate和担心你的域名,而不是实施的东西,已经实施并行之有效的,并吨的其他项目使用。这是有很多细微的问题,你还是使用的是已经在那里更好。

really, the best answer, though, is the one you have already found. this is a problem that has been solved by ORMs. use NHibernate and worry about your domain rather than implementing something that is already implemented and well tested and used by tons of other projects. it's a problem with a lot of nuances and you are better off using what is already out there.

这篇关于IRepository骨料和延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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