与多个数据源的工作单位? [英] Unit of Work with multiple Data Sources?

查看:153
本文介绍了与多个数据源的工作单位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是可能的(甚至是可能的),我只是不完全所著的Grokking一个概念工作单位。基本上,我认为这是某种在面向对象的环境中使用广泛的交易。开始工作的单位,与对象进行交互,提交或回滚。但这是如何打破以实际发生的交易对这些对象背后的数据存储?

It's possible (even probable) that I'm just not fully grokking the concept of a "unit of work." Basically, I see it as sort of a broad transaction used in an object-oriented environment. Start the unit of work, interact with the objects, commit or roll back. But how does this break down to the actual transactions on the data stores behind those objects?

在一个单一的数据库和ORM(如NHibernate的)的系统很容易。该交易可以通过ORM进行维护。但是对于其中自定义的域模型模糊许多不同的数据源系统是什么?而不是所有的这些数据源是关系数据库? (有在这里的文件系统上做了很多。)

In a system with a single DB and an ORM (such as NHibernate) it's easy. The transaction can be maintained through the ORM. But what about a system where the custom domain models are obscuring many disparate data sources? And not all of those data sources are relational databases? (There's a lot done on the file system around here.)

现在我卡上的想法,你根本无法维持跨SQL2005数据库,一个SQL2000数据库,一个DB2数据库的事务,和文件系统都在同一个'原子'经营。所以现在它的开发商对球队的责任(谁通常独立工作,相互的)手动维护在code交易。每个数据库可以有它正确的交易,但经营作为一个整体人工检查和平衡的每一步显著

Right now I'm stuck on the idea that "you simply can't maintain a transaction across a SQL2005 DB, a SQL2000 DB, a DB2 DB, and the file system all in the same 'atomic' business operation." So for now it's the responsibility of the developers on the team (who generally work independently of each other) to maintain transactions manually in the code. Each DB can have proper transactions on it, but the business operation as a whole is manually checked and balanced every significant step of the way.

然而,随着域和标准人员流动日益复杂,这种做法将变得越来越困难且容易出错的一段时间。

However, with increasing complexity in the domain and standard developer turnover, this approach will become increasingly difficult and error-prone over time.

没有任何人有一个如何像这样的域名可能最好的解决,或者它如何被解决之前,任何意见或例子?在这种情况下,实际的域还是非常多处于起步阶段,演变为原型一天扩大和消费/替换一个大的生态系统完全不同的遗留应用程序。因此,有足够的空间,重新设计和重新分解。

Does anybody have any advice or examples of how a domain like this might best be addressed, or how it has been addressed before? The actual "domain" in this case is still very much in its infancy, evolving as a prototype to one day expand and consume/replace a large ecosystem of disparate legacy applications. So there's plenty of room for re-designing and re-factoring.

有关参考,我目前瞄准设计的10000英尺的观点是:一个大集合的小哑的,可能的客户端应用程序调用中央基于消息的服务。该服务是入口进入域核心,可以作为一个大的MVC风格的应用程序被认为。请求到服务(很像动作),它们由处理器拾取(很像控制器)制成。什么程序去那里。他们的模型,其中包含所有的业务规则进行交互。该机型发布事件,听众(服务?这一部分仍然是在设计阴天需要改进)拾起,并通过与资料库(数据库X,数据库Y,文件系统,电子邮件,任何外部资源)的交互处理。所有欢快相应的依赖注入。

For reference, a 10,000-foot view of the design I'm currently aiming for is: A large collection of small as-dumb-as-possible client applications calling a central message-based service. The service is the entryway into the "domain core" and can be thought of as one big MVC-style application. Requests are made to the service (much like "actions") which are picked up by handlers (much like "controllers"). Anything procedural goes there. They interact with the models, which contain all the business rules. The models publish events which listeners ("services"? this part is still cloudy in the design and subject to improvement) pick up and handle by interacting with repositories (database x, database y, file system, email, any external resource). All merrily dependency-injected accordingly.

对不起,所有的冗长:)但是,如果任何人有任何意见,我很乐意听到这种说法。甚至(尤其是)如果该建议是你的设计不好,试试这个,而不是...谢谢!

Sorry for all the verbosity :) But if anybody has any advice, I'd love to hear it. Even (especially) if that advice is "your design is bad, try this instead..." Thanks!

推荐答案

我previously工作,可以做到这一点的系统上,它是相当简单的。既然你的项目是处于早期阶段,也许这可能是对您有用的信息。不幸的是,我不再有机会获得code,但我还是在描述它是如何工作的舒适。

I previously worked on a system that could accomplish this, and it is fairly straightforward. Since you project is in its early stages, perhaps this could be useful information for you. Unfortunately, I no longer have access to the code, but am still comfortable in describing how it worked.

我做了什么始建使用通用的存储库模式实现我的存储库。该基地库类型将永远是由服务和UOW引用。为了讨论方便,我们将其称之为BaseRepository。 T将限于IEntity实现,这表示一个域对象。从BaseRepository,我创造了另一套基类合成,如SqlBaseRepository,XmlBaseRepository等。

What I had done was built my repositories using a generic repository pattern implementation. The base repository type would always be references by the services and UoW. For sake of discussion, we will call it BaseRepository. "T" would be restricted to IEntity implementations, which denoted a Domain Object. From the BaseRepository, I had created another set of base classes for compositing, such as SqlBaseRepository, XmlBaseRepository, etc.

该UOW只在乎的东西是类型BaseRepository,这就是核心功能会存在的。基本CUD(CRUD的)将被重新presented,为创建,更新和删除等同。各有什么这些会做将创建一个委托,并将其放置在里面UOW一个队列,也随之它打算什么类型的交易是,并完成它需要相应的数据信息传递。该UOW将开始保持什么样的存储库将要必须参与交易的名单,但还是没在意这是什么类型。 Effictively,这里排队就像是在一个事务中争取。

The UoW only cares that something is of the type BaseRepository, which is where the core functionality would exist. Basic CUD (of CRUD) would be represented, providing the equivalents for Creates, Updates, and Deletes. What each of these would do would be to create a delegate and place it in a queue inside the UoW, also passing along information about what type of transaction it was going to be, and the appropriate data required to complete it. The UoW would start maintaining a list of what repositories were going to need to be involved in the transaction, but still did not care what type it was. Effictively, queueing up here is like enlisting in a transaction.

该BaseRepository定义的抽象方法被称为像.ApplyChange()。一旦.Commit()被调用的UOW,它会创建一个TransactionScope(),并开始调用列表中的delagates,传回的信息,.ApplyChange()。 .ApplyChange()的实际实现存在于特定的资源库群,即SqlRepositoryBase等,并可以通过实施覆盖,以及

The BaseRepository defined an abstract method called something like .ApplyChange(). Once .Commit() was called on the UoW, it would create a TransactionScope() and start calling the delagates in the list, passing back the information to .ApplyChange(). The actual implementation of .ApplyChange() exists in the specific repository base, i.e. the SqlRepositoryBase, etc. and could be overridden by the implementation, as well.

凡有猫腻,至少对我来说,被回滚。我只涉及一个单一的数据库,但有时不得不所做的基于文件的变化。我添加了一个.RevertChange()方法,并开始追踪原始状态和修改状态,这样我可以主要适用反向增量要回我在那里的文件堆栈中。

Where it got tricky, for me at least, was rolling back. I only dealt with a single database, but sometimes had file-based changes that were made. I added a .RevertChange() method and started tracking the original state and modified states so that I could basically apply a reverse-delta to get back to where I was on the file stack.

我希望我能上实施更具体的,但它已经过了一年,因为我已经看到了code。我可以告诉你,原始code的基础上,从书中承担,用C# .NET领域驱动设计:问题 - 设计 - 解决方案,由蒂姆·麦卡锡。大量的我的仓库实现的是基于他的例子,以大多数我的定制进来的UoWs并组织实施。

I wish that I could be more specific on the implementation, but it has been over a year since I have seen the code now. I can tell you that the basis for the original code was borne from the book, .NET Domain-Driven Design with C#: Problem - Design - Solution, by Tim McCarthy. A large amount of my repository implementation was based on his examples, with a large majority of my customization coming in on the UoWs and their implementation.

我希望有所帮助,有所! : - )

I hope that helps, somewhat! :-)

这篇关于与多个数据源的工作单位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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