如何使用单位工作的领域层 [英] How to use Unit of Work in Domain Layer

查看:127
本文介绍了如何使用单位工作的领域层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用的工作模式的单位为我服务类。
我有我的CompanyService类

 公共类CompanyService:ICompanyService
{
    私人只读ICompanyRepository _companyRepository;

    公共CompanyService(ICompanyRepository companyRepository)
    {
        _companyRepository = companyRepository;
    }

    公共无效CreateCompany(公司公司)
    {
        _companyRepository.Add(公司);
        //我想有工作单位提交这里!
        //unitofwork.commit();
    }

    公共无效UpdateCompany(公司公司)
    { 等等...}
 

我怎么能在我的领域层注入一个的UnitOfWork实例在我ServiceClass如果我IUnitOfWork是我的数据层,而不是?我应该在我的领域层的IUnitOfWork?我只是觉得这听起来并不正确......

解决方案
  

我应该有一个IUnitOfWork在我的领域层?

如果你的领域层需要它,那么答案肯定是:是

域层应包含它需要工作的一切。你需要记住的唯一的事情是,这层不应该引用任何外部库,它不应该依赖于任何特定的技术实施。

IRepository IUnitOfWork 的具体实现,将是基础设施/数据层的地方在那里,你可以参考一下任何框架,你想要的(实体框架,...)

有一个在此<一个href="http://stackoverflow.com/questions/16861127/onion-architecture-business-services-interfaces-and-implementation/16959061#16959061">SO回答在这里我解释什么需要设在Doamin层(这是一个洋葱架构相关的问题,但它并不重要)。
同时你也可以看看这个<一href="http://stackoverflow.com/questions/15181290/decouple-unit-of-work-from-services-or-repo/15527444#15527444">SO回答在那里你会找到一个方法来轻松实现的UnitOfWork和信息库模式。

I'm trying to use the Unit of work pattern in My Service classes.
I have my CompanyService class

public class CompanyService: ICompanyService
{
    private readonly ICompanyRepository _companyRepository;

    public CompanyService(ICompanyRepository companyRepository)
    {
        _companyRepository = companyRepository;
    }

    public void CreateCompany(Company company)
    {
        _companyRepository.Add(company);
        //I want to have unit of work commit here!!! 
        //unitofwork.commit(); 
    }

    public void UpdateCompany(Company company)
    { etc...}

How can i inject an instance of an UnitOfWork In my ServiceClass if my IUnitOfWork is in My Data Layer, and not in my Domain Layer? Should i have an IUnitOfWork in my Domain Layer? I just think this doesn't sound right...

解决方案

Should i have an IUnitOfWork in my Domain Layer?

If your Domain Layer needs it, then the answer is definitely: Yes.

The Domain Layer should contains everything that it needs to work. The only thing you need to keep in mind is that this layer should not reference any external libraries, it should not be tied to any technical specific implementation.

Your IRepository or IUnitOfWork's concrete implementations will be somewhere in an Infrastructure/Data Layer where you can reference whatever framework you want (Entity Framework, ...)

Have a look at this SO answer where I explain what need to be located in the Doamin Layer (it's an Onion architecture related question but it doesn't matter).
And have a look at this SO answer where you'll find a way to implement UnitOfWork and Repository patterns easily.

这篇关于如何使用单位工作的领域层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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