申请工作单位的通用信息库 [英] applying unit of work in a generic repository

查看:86
本文介绍了申请工作单位的通用信息库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在学习与工作单位做存储库。我也知道该怎么做DI / IOC。但我的问题是我无法揣摩出适用工作单位在我的code。下面是一个通用的存储库。

I'm learning on doing repository with unit of work. I also know how to do DI/IOC. But my problem is I can't figure out where to apply Unit of Work in my code. Below is a Generic Repository.

public abstract class Repository<T, C> : //IDisposable,
        IRepository<T> where T : class
        where C : DbContext, new()

    {
        private C entities = new C();

        public C Context 
        {
            get
            {
                return this.entities;
            }
            set
            {
                this.entities = value;
            }
        }

       public virtual void Insert(T entity)
       {
         this.entities.Set<T>().Add(entity);
       }

       // remove some code for brevity
    }

果然不出我所到目前为止已经试过:

What I had tried so far:


  1. 请在工作单位

public class UnitOfWork : IUnitOfWork
{
  private readonly FooContext _dbContext;
  public UnitOfWork()
  {
    _dbContext = new DataContext;
  }

  public void Save()
  {
    _dbContext.SaveChanges();

  }

  // Dispose method
}


在我的服务:

public class ProductService : Repository<Product, FooContext>, IProductService
{
  private readonly IProductRepository _prodRepo;
  private readonly IUnitOfWork _uow;
  public ProductService(IUnitOfWork uow, IProductRepository prodRepo)
  {
    _uow = uow;
    _prodRepo = prodRepo;
  }

  public override void Insert(Item entity)
  {
    base.Insert(entity);
    Save();
  }

  public void Save()
  {
    uow.Save();
  }

  // remove some code for brevity
}

有没有错误,当我建立它。当我尝试在我的控制器适用它不给我一些错误。但是,当我尝试运行和调试它,在智能跟踪,它不给我一个插入语句,并再次,它不给我一个错误..哪里有我哪里呢?

There's no error when I build it. And when I try it apply in my Controller it doesn't give me some error. But when I try to run and debug it, in the Intellitrace, It doesn't give me an Insert statement and again, it does not give me an error.. Where have I gone wrong?

任何帮助将是非常美联社preciated。谢谢!

Any help would be much appreciated. Thanks!

推荐答案

我们看到一起,你应该从你的资料库分开你的服务。

We saw together you should separate your service from your repository.

在这里,你的问题似乎来自您使用两个不同的DbContext实例的事实。
一个在你存储库,一个在你的UnitOfWork。

Here, your problem seems to come from the fact you use two different dbcontext instances. one in your repository and one in your UnitOfWork.

相反,你应该在注入你的资料库和您的UnitOfWork相同的DbContext实例。

Instead, you should inject the same Dbcontext instance in your repository and your UnitOfWork.

编辑:
你应该写你的不同层是这样的:

You should write your different layers like this:

public class ProductService
{
    private readonly IProductRepository productRepository;

    private readonly IUnitOfWork unitOfWork;

    public ProductService(IProductRepository productRepository, IUnitOfWork unitOfWork)
    {
        this.productRepository = productRepository;
        this.unitOfWork = unitOfWork;
    }

    public IEnumerable<Product> GetCurrentProductsOnOrderForCustomer(int customerId)
    {
        // etc.
    }
}

控制器层应该这样做:

The controller layer should do this:

public class ProductController : Controller
{
     private readonly IProductService prodService;

     public ProductController(IProductService prodService)
     {
         this.prodService = prodService;
     }
}

下面是你纠正的UnitOfWork:

Here is your corrected UnitOfWork:

public class UnitOfWork : IUnitOfWork
{
    private readonly IDbContext _dbContext;

    public UnitOfWork(IDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    public void Save()
    {
        _dbContext.SaveChanges();
    }

    // Dispose method
}

下面是仓库的一个例子

public class ProductRepository
{
    private readonly IDbContext _context;

    public ProductRepository(IDbContext dbContext)
    {
        this._context = context;
    }    

    public virtual void Insert(T entity)
    {
        this._context.Products.Add(entity);
    }

       // remove some code for brevity
}

和您创建一个从继承的DbContext上下文类,和您在UOW和回购协议注入。

And you create a context class that inherits from DbContext, and that you inject in UoW and Repos.

public class MyApplicationContext : DbContext
{
    public MyApplicationContext(string connectionString)
    {
        // Configure context as you want here
    }

    public DbSet<Product> Products { get; set; }
}

这篇关于申请工作单位的通用信息库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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