从服务或回购分离工作单位 [英] Decouple unit of work from services or repo

查看:124
本文介绍了从服务或回购分离工作单位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的脱钩从我的服务或资料库的工作单位,这样我不会有触摸UOW code,每当我想补充一个新的服务。我该怎么做呢?

  _categoryService = _unitOfWork.Get< ICategoryService>();

所以不是

  _unitOfWork.CategoryService.Add(类别)

我只能说;

  _categoryService.Add(类);


解决方案

  

我想从我的服务或脱钩我的工作单位
  贮存库,让我不会有触摸UOW code,每当我想
  以添加新的服务


嗯,这是一个良好的开端! ; - )

解决方案我presenting不是唯一可能的解决方案,有落实UOW几个很好的方式(谷歌将帮助你)。但是,这应该给你的大图片。

首先,创建2个接口:IUnitOfWork和IRepository

  public接口IUnitOfWork:System.IDisposable的
{
  IRepository< TEntity> GetRepository< TEntity>()其中TEntity:类;
  无效保存();
}公共接口IRepository< T> :IDisposable的其中T:类
{
  无效添加(T实体);
  无效删除(T实体);
  无效更新(T实体);
  ŧGetById(长ID);
  IEnumerable的< T>所有();
  IEnumerable的< T> AllReadOnly();
  IEnumerable的< T>查找(前pression<&Func键LT; T,BOOL>> predicate);
}

的实现是非常简单(我删除了我的可读性目的的所有意见,但不要忘了加你;-))

 公共类的UnitOfWork< TContext> :IUnitOfWork哪里TContext:IDbContext,新的()
{
  私人只读IDbContext _ctx;
  私人字典<类型,对象> _repositories;
  私人布尔_disposed;  公众的UnitOfWork()
  {
    _ctx =新TContext();
    _repositories =新词典<类型,对象>();
    _disposed = FALSE;
  }  公共IRepository< TEntity> GetRepository< TEntity>()其中TEntity:类
  {
    如果(_repositories.Keys.Contains(typeof运算(TEntity)已))
      返回_repositories [typeof运算(TEntity)已作为IRepository< TEntity取代;    变种库=新库< TEntity>(_ CTX);
    _repositories.Add(typeof运算(TEntity)已,库);
    返回库;
  }  公共无效保存()
  {
     尝试
     {
       _ctx.SaveChanges();
     }
     赶上(DbUpdateConcurrencyException前)
     {
       。ex.Entries.First()重载();
     }
  }  ...
}公共类库< T> :IRepository< T>其中T:类
{
  私人只读IDbContext _context;
  私人只读IDbSet< T> _dbset;  公共库(IDbContext上下文)
  {
    _context =背景;
    _dbset = context.Set< T>();
  }  公共虚拟无效添加(T实体)
  {
    _dbset.Add(实体);
  }  公共虚拟无效删除(T实体)
  {
    VAR进入= _context.Entry(实体);
    entry.State = EntityState.Deleted;
  }  公共虚拟无效更新(T实体)
  {
    VAR进入= _context.Entry(实体);
    _dbset.Attach(实体);
    entry.State = EntityState.Modified;
  }  公众实际的t GetById(长ID)
  {
    返回_dbset.Find(ID);
  }  公共虚拟IEnumerable的< T>所有()
  {
    返回_dbset.ToList();
  }  公共虚拟IEnumerable的< T> AllReadOnly()
  {
    返回_dbset.AsNoTracking()了ToList()。
  }  公共IEnumerable的< T>查找(前pression<&Func键LT; T,BOOL>> predicate)
  {
    返回_dbset.Where(predicate);
  }}

正如你所看到的,既实现使用IDbContext接口。这个接口仅仅是简单的测试目的:

 公共接口IDbContext
{
  DbSet< T>设置< T>()其中T:类;
  DbEntityEntry< T>进入< T>(T实体),其中T:类;
  INT的SaveChanges();
  无效的Dispose();
}

(正如你看到的,我使用的EntityFramework code前)

现在,整个管道设置,让我们一起来看看如何可以在服务中使用。
我有一个基本的服务,看起来是这样的:

 内部类服务< T>其中T:类
{
  内部服务(Infrastructure.IUnitOfWork UOW)
  {
    _repository = uow.GetRepository< T>();
  }  保护Infrastructure.IRepository< T>知识库
  {
    {返回_repository; }
  }  私人只读Infrastructure.IRepository< T> _repository;
}

和我的所有服务从这个基本的服务继承。

 内部类的CustomerService:服务与LT; Model.Customer>
{
  内部的CustomerService(Infrastructure.IUnitOfWork UOW):基地(UOW)
  {
  }  内部空间添加(Model.Customer客户)
  {
    Repository.Add(客户);
  }  内部Model.Customer GetByID(INT ID)
  {
    返回Repository.Find(C => c.CustomerId == ID);
  }}

这就是它!

现在,如果你想共享相同的UOW了几项服务,在一个门面的方法或其他地方,它可能只是看起来像这样:

 使用(VAR UOW =新的UnitOfWork< CompanyContext>())
{
  VAR catService =新Services.CategoryService(UOW);
  VAR custService =新Services.CustomerService(UOW);  VAR的猫=新Model.Category {名称= catName};
  catService.Add(DEP);  custService.Add(新Model.Customer {名称= CUSTNAME,分类= Cat});  uow.Save();
}

希望这有助于!

I am trying to decouple my unit of work from my services or repository so that I wont have to touch the UoW code whenever I wish to add a new service. How do I do this?

_categoryService = _unitOfWork.Get<ICategoryService>();

so instead of

_unitOfWork.CategoryService.Add(category)

I can just say;

_categoryService.Add(category);

解决方案

I am trying to decouple my unit of work from my services or repository so that I won’t have to touch the UoW code whenever I wish to add a new service

Well, that’s a good start! ;-)

The solution I am presenting is not the one and only possible solution, there are several good ways to implement UoW (Google will help you out). But this should give you the big picture.

First, create 2 interfaces: IUnitOfWork and IRepository

public interface IUnitOfWork : System.IDisposable
{
  IRepository<TEntity> GetRepository<TEntity>() where TEntity : class;
  void Save();
}

public interface IRepository<T> : IDisposable where T : class
{
  void Add(T entity);
  void Delete(T entity);
  void Update(T entity);
  T GetById(long Id);
  IEnumerable<T> All();
  IEnumerable<T> AllReadOnly();
  IEnumerable<T> Find(Expression<Func<T, bool>> predicate);
} 

The implementations are quite straightforward (I removed all my comments for readability purpose, but do not forget to add yours ;-) )

public class UnitOfWork<TContext> : IUnitOfWork where TContext : IDbContext, new()
{
  private readonly IDbContext _ctx;
  private Dictionary<Type, object> _repositories;
  private bool _disposed;

  public UnitOfWork()
  {
    _ctx            = new TContext();
    _repositories   = new Dictionary<Type, object>();
    _disposed       = false;
  }

  public IRepository<TEntity> GetRepository<TEntity>() where TEntity : class
  {
    if (_repositories.Keys.Contains(typeof(TEntity)))
      return _repositories[typeof(TEntity)] as IRepository<TEntity>;

    var repository = new Repository<TEntity>(_ctx);
    _repositories.Add(typeof(TEntity), repository);
    return repository;
  }

  public void Save()
  {
     try
     {
       _ctx.SaveChanges();
     }
     catch (DbUpdateConcurrencyException ex)
     {
       ex.Entries.First().Reload();
     }
  }

  …
}

public class Repository<T> : IRepository<T> where T : class
{
  private readonly IDbContext _context;
  private readonly IDbSet<T> _dbset;

  public Repository(IDbContext context)
  {
    _context = context;
    _dbset   = context.Set<T>();
  }

  public virtual void Add(T entity)
  {
    _dbset.Add(entity);
  }

  public virtual void Delete(T entity)
  {
    var entry = _context.Entry(entity);
    entry.State = EntityState.Deleted;
  }

  public virtual void Update(T entity)
  {
    var entry = _context.Entry(entity);
    _dbset.Attach(entity);
    entry.State = EntityState.Modified;
  }

  public virtual T GetById(long id)
  {
    return _dbset.Find(id);
  }

  public virtual IEnumerable<T> All()
  {
    return _dbset.ToList();
  }

  public virtual IEnumerable<T> AllReadOnly()
  {
    return _dbset.AsNoTracking().ToList();
  }

  public IEnumerable<T> Find(Expression<Func<T, bool>> predicate)
  {
    return _dbset.Where(predicate);
  }

}

As you can see, both implementations make use of IDbContext interface. This interface is just for easy testing purpose:

public interface IDbContext
{
  DbSet<T> Set<T>() where T : class;
  DbEntityEntry<T> Entry<T>(T entity) where T : class;
  int SaveChanges();
  void Dispose();
}

(As you can see, I’m using EntityFramework Code First)

Now that the whole plumbing is set up, let’s have a look at how this could be used in a service. I have a base service that looks like this:

internal class Service<T> where T : class
{
  internal Service(Infrastructure.IUnitOfWork uow)
  {
    _repository = uow.GetRepository<T>();
  }

  protected Infrastructure.IRepository<T> Repository
  {
    get { return _repository; }
  }

  private readonly Infrastructure.IRepository<T> _repository;
}

And all my services inherit from this base service.

internal class CustomerService : Service<Model.Customer>
{
  internal CustomerService(Infrastructure.IUnitOfWork uow) : base(uow)
  {   
  }

  internal void Add(Model.Customer customer)
  {
    Repository.Add(customer);
  }

  internal Model.Customer GetByID(int id)
  {
    return Repository.Find(c => c.CustomerId == id);
  }

}

And that’s it!

Now, if you want to share the same UoW to several services, in a facade method or somewhere else, it could just look like this:

using (var uow = new UnitOfWork<CompanyContext>())
{
  var catService = new Services.CategoryService(uow);
  var custService = new Services.CustomerService(uow);

  var cat = new Model.Category { Name = catName };
  catService.Add(dep);

  custService.Add(new Model.Customer { Name = custName, Category = cat });

  uow.Save();
}

Hope this helps!

这篇关于从服务或回购分离工作单位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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