IUnitOfWork如何使用 - 最佳实践 [英] IUnitOfWork how to use - best practice

查看:246
本文介绍了IUnitOfWork如何使用 - 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.Net4.0网络表单(不是MVC!)应用程序中的EF4.3.1。



我倾向于使用存储库模式与IUnitOfWork接口。但我想知道我是否遵循最佳做法,特别是因为我遵循的大多数例子都是基于MVC应用程序。



我会说这只是一个小型网站应用程序,这可能会影响解决方案的选择。



解决方案目前有3个项目,模型,逻辑和站点。模型包含代码第一个实体和IUnitOfWork接口。逻辑包含存储库和服务层。网站显然包含网站,codebehind等。



我不使用任何第三方注入实用程序(ninject等)。我用IUnitOfWork手动注入存储库,即



public BookingRepository(IUnitOfWork unitOfWork)



我不太清楚与服务层有关,IUnitOfWork也应该在Site项目中出现,或者只存在于Logic和Model层。



目前我注入一个存储库和一个单元的工作,即



public BookingService(IUnitOfWork unitOfWork,IBookingRepository存储库,IAppSettings appSettings)



但是意味着提交(保存到db)在站点级完成,但我不知道是否应该在服务层中完成。这也意味着,因为我的IUnitOfWork在我的模型层中声明,所以我需要在我的网站中引用Model。



我能做些什么更好?我做任何事情吗? lol

解决方案

德米特里是对的。这是一个工作单位的示例实施。这里的好处是,工作单位通过执行所有这些共享的单个数据库上下文类来协调多个存储库的工作。



这是一个很好的资源开始了解这些模式可以如何一起使用。它适用于MVC和Web Forms开发。 在ASP.NET MVC应用程序中实现存储库和工作模式单元

  public class UnitOfWork:IDisposable 
{
private DbContext _context;
private PersonRepository _personRepository;
private CompanyRepository _companyRepository;

public UnitOfWork(DbContext context)
{
this._context = context;
}

public void Commit()
{
_context.SaveChanges();
}

//我们懒惰加载我们的存储库...
public PersonRepository PersonRepository
{
get
{
if(this._personRepository == null)
{
this._personRepository = new PersonRepository(context);
}
return _personRepository;
}
}

public CompanyRepository
{
get
{
if(this._companyRepository == null)
{
this._companyRepository = new CompanyRepository(context);
}
return _companyRepository;
}
}

//为简洁起见删除IDisposable实现...
}


I'm using EF4.3.1 in a .Net4.0 web forms (not MVC!) application.

I tend to use the repository pattern with an IUnitOfWork interface. But I'm wondering if I'm following best practices, especially since most examples I've followed are based on MVC apps.

I will say it's only a small web app, so that may affect the solution choices.

The solution currently has 3 projects, Model, Logic and Site. Model contains the codefirst entities and the IUnitOfWork interface. Logic contains the repositories and service layer. Site obviously contains the website, codebehind, etc.

I don't use any third-party inject utility (ninject, etc). I manually inject repositories with an IUnitOfWork i.e.

public BookingRepository(IUnitOfWork unitOfWork)

I'm less clear what to do with the service layers, should the IUnitOfWork also exisit in the Site project, or only exist in the Logic and Model layers.

Currently I inject a repository and a unit of work into a service i.e.

public BookingService(IUnitOfWork unitOfWork, IBookingRepository repository, IAppSettings appSettings)

But this means the commiting (save to db) is done at the Site level, but I wonder if it should be done in the service layer. It also means, that since my IUnitOfWork is declared in my model layer, I need a reference to Model in my site also.

What can I do better? Am I doing anything right? lol

解决方案

Dmitry is right. Here is a sample implementation of a Unit of work. The benefit here is that the unit of work pattern coordinates the work of the multiple repositories by enforcing a single database context class shared by all of them.

This is a good resource for beginning to understand how these patterns can be used together. It is valid for both MVC and web Forms development. Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application

public class UnitOfWork : IDisposable
{
    private DbContext _context;
    private PersonRepository _personRepository;
    private CompanyRepository _companyRepository;

    public UnitOfWork(DbContext context)
    {
        this._context = context;
    }

    public void Commit()
    {
        _context.SaveChanges();
    }

    // We lazy-load our repositories...
    public PersonRepository PersonRepository 
    {
        get
        {
            if (this._personRepository == null)
            {
                this._personRepository = new PersonRepository(context);
            }
            return _personRepository;
        }
    }

    public CompanyRepository 
    {
        get
        {
            if (this._companyRepository == null)
            {
                this._companyRepository = new CompanyRepository(context);
            }
            return _companyRepository;
        }
    }

    //IDisposable implementation removed for brevity...
}

这篇关于IUnitOfWork如何使用 - 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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