如何工作类的单位知道在提交调用的信息库? [英] How does unit of work class know which repository to call on commit?
问题描述
* preface:我是pretty新的工作模式的单位*
我的目标是实现工作类的单位,将能够保持已在整个给定的交易改变了所有对象的轨道。一切,我读到工作格局的单位有它并排与存储库模式。所以这是我想使用的方法。
My goal is to implement a unit of work class that will be able to keep track of all objects that have been changed throughout a given transaction. Everything I read about the unit of work pattern has it side by side with the repository pattern. So this is the approach I'd like to use.
例如说,我创建了一个新的用户。在我的工作对象的单位,我有新创建的对象的列表,所以我加入我的新用户到这个列表。我的用户信息库有题为创建一个方法,它在用户和调用存储过程,将数据添加到数据库中。当我打电话提交我的工作单位,怎么会知道调用基于新的对象列表上库和方法?说它包含了用户对象和评论的对象。两者都是新建的,需要在提交补充道。我不确定如何做到这一点。
Say for example I create a new User. On my unit of work object, I have a list of newly created objects, so I add my new User to this list. My user repository has a method titled Create, which takes in a User and calls a stored procedure to add the data to the database. When I call commit on my unit of work, how will it know which repository and method to call based on the list of new objects? Say it contains a User object and a Comment object. Both are newly created and need to be added on commit. I'm uncertain how to accomplish this.
有人能解释这一点更好,甚至是一个小例子,如果可能的话?
Could somebody explain this a bit better and maybe even a small example if possible?
感谢。
推荐答案
一个解决这是使用控制反转的最常见的方式。
One of most common ways of solving this is using inversion of control.
例如,您已在类用户和评论,你已经实现了一个通用的存储库 IRepository< TDomainObject>
。这是获取用户或评论的储存库只是给TDomainObject参数:
For example, you've your classes User and Comment, and you've implemented a generic repository IRepository<TDomainObject>
. That's getting a repository of User or Comment is just giving the TDomainObject parameter:
-
IRepository&lt;使用者&GT;
-
IRepository&LT;&评论GT;
IRepository<User>
IRepository<Comment>
后来你已经配置了谁的实施 IRepository&lt;使用者&GT;
和 IRepository&LT;注释&GT;
,因此,如果您使用像微软模式和放大器;实践通用服务定位器,而且我们在你的工作单位的commit方法的主体:
Later you've configured who's implementing IRepository<User>
and IRepository<Comment>
, so if you use something like Microsoft Pattern & Practices' Common Service Locator, and we're in the body of commit method in your unit of work:
foreach(DomainObject some in NewObjects)
{
((IRepository<DomainObject>)ServiceLocator.Current.GetInstance(Type.GetType(string.Format("NamespacesToGenericRepository.IRepository`1[[{0}]]", some.GetType().FullName)))).Add(some);
}
请注意 IRepository&LT; TDomainObject&GT;
有一个逆变 TDomainObject 的泛型参数,类型必须继承一个基域类型对象调用的的domainObject 的,它允许东西的上溯造型像 IRepository&lt;使用者&GT;
到 IRepository&LT; domainObject的&GT;
。
Note IRepository<TDomainObject>
has a contravariant TDomainObject generic parameter which type must inherit a base type of Domain Object called DomainObject, which permits an upcast of something like IRepository<User>
to IRepository<DomainObject>
.
在换句话说,你的 IRepository&LT; TDomainObject&GT;
接口签名将是这样的:
In other words, your IRepository<TDomainObject>
interface signature will look like this:
public interface IRepository<out TDomainObject>
where TDomainObject : DomainObject
这仅仅是一个有关如何实现定位的具体资料库总结和/或暗示的这样的 的域对象的工作能够管理任何专门的域对象的那些单位。
This is just a summary and/or hint about how to implement locating the concrete repository so an unit of work of domain objects can manage ones of any specialized domain object.
如果您想了解更多有关的控制反转的检查此Wikipedia文章:
If you want to learn more about inversion of control check this Wikipedia article:
- http://en.wikipedia.org/wiki/Inversion_of_control
和,因为我自己的经验,我想建议你温莎城堡作为的控制反转的选择的框架:
And, because of my own experience, I'd like to suggest you Castle Windsor as inversion of control framework of choice:
- http://www.castleproject.org/container/
这篇关于如何工作类的单位知道在提交调用的信息库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!