的UnitOfWork实施UnitOfWork

由网友(烟雨)分享简介:香港专业教育学院已经能够执行工作有点凉单元与实体框架工作。Ive been able to implement a little cool unit of work to work with entity framework.我想出了.. public class UnitOfWork : IUnitOfWork...

香港专业教育学院已经能够执行工作有点凉单元与实体框架工作。

Ive been able to implement a little cool unit of work to work with entity framework.

我想出了..

public class UnitOfWork : IUnitOfWork
    {
        private Database _database;
        private IDatabaseFactory _databaseFactory;

        private DbTransaction transaction;

        public UnitOfWork(IDatabaseFactory databaseFactory)
        {
            _databaseFactory = databaseFactory;
            _database = Database;

            transaction = _database.Database.Connection.BeginTransaction();
        }

        public Database Database
        {
            get { return _database ?? (_database = _databaseFactory.Get()); }
        }

        public void Dispose()
        {
            try
            {
                _database.SaveChanges();
                transaction.Commit();
            }
            catch (Exception ex)
            {
                transaction.Rollback();
            }
        }
    }

我是pretty的确保每个人都嫉妒本单位工作的了。 (开玩笑)

I am pretty sure everyone is jealous of this unit of work now. (Kidding)

但我必须在此服务层的小设计问题。

But i have a little design problem in this service layer.

public class JournalService : IJournalService
    {
        IJournalRepository _journalRepository;

        public JournalService(IJournalRepository journalRepository)
        { 
            _journalRepository = journalRepository;
        }

        public void AjouterJournal(Journal j)
        {
           [B]using (IUnitOfWork uow = new UnitOfWork())[/B]
            {
                var journal = new Journal();
                journalRepository.AddJournal(journal);

            }
        }
    }

现在的问题是工作单元需要一个数据库喷射,所以我不能创建它的一个实例。我无法提供工作的单位直接在服务层,因为它根本没有任何意义,因为工作的单位必须是一次性的。

The problem is the unit of work need a database injection so i cant create an instance of it. I can't provide an unit of work directly in the service layer because it would make no sense since the unit of work need to be disposable.

因为我使用存储库来增加我的东西有没有必要直接访问工作的单位,将出现全自动保存时,它总要被处置。

And because i use repository to add my stuff there no need to access the unit of work directly, the saving will occur automaticly when it will be disposed anyway.

我可以在我的服务层注入IDatabaseFactory但这个想法是不使用它。实际上,业务层不应该知道这一点。

I could inject the IDatabaseFactory in my service layer but the idea is to not use it there. Actually the service layer shouldnt know about it.

怎么样的的UnitOfWork工厂?

How about an UnitOfWork factory ?

我如何才能解决这个问题的任何意见或建议?

Any ideas or suggestion on how i can fix this?

感谢。

推荐答案

您应该注入的UnitOfWork投入使用,如果你想使用当前的架构。您的服务将不会对的UnitOfWork实现内(隐藏)的依赖,这将是更好的测试。这齐头并进与面向对象的架构的许多原则。

You should inject UnitOfWork into service if you want to use your current architecture. Your service will not have inner (hidden) dependency on UnitOfWork implementation and it will be better testable. It goes hand in hand with many principles of object oriented architecture.

另一件事是,这个实现是只可用于简单的CRUD操作。在更复杂的服务,您将结束与多个操作组成的(可能来自多发性服务),将分别经营与的UnitOfWork。在一个业务操作调用多个调用SaveChanges(和交易)很可能不是你通常希望 - 在这种情况下,你想从一些顶级的服务或服务的调用者调用的SaveChanges只有一次。典型的情况是,单一的业务操作有一个工作单元与一个事务,但你可以做很多服务的操作中,这样的商业操作的一部分。

Other thing is that this implementation is only usable for simple CRUD operations. In more complex services you will end up with composition of multiple operations (possibly from multipe services) which will each operate with UnitOfWork. Calling multiple SaveChanges (and transactions) in one business operation is probably not what you usually want - in such case you want to call SaveChanges only once from some top level service or from service's caller. Typical scenario is that single business operation has one unit of work with one transaction but you can do a lot of service's operations as part of this business operation.

另外一个含义是建设你的资料库中。他们可能需要访问数据库,不是吗?所以,你可能已经注入UOW到存储库的构造。如果你这样做,你可以在所有的避免UOW和基本服务之间的关系。

Another implication is construction of your Repositories. They probably need access to Database, don't they? So you probably already inject UoW into repository constructor. If you do this you can avoid relation between UoW and basic services at all.

阅读全文

相关推荐

最新文章