简体   繁体   中英

applying unit of work in a generic repository

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. Make a Unit of Work class

     public class UnitOfWork : IUnitOfWork { private readonly FooContext _dbContext; public UnitOfWork() { _dbContext = new DataContext; } public void Save() { _dbContext.SaveChanges(); } // Dispose method } 

In my service:

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?

Any help would be much appreciated. Thanks!

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

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

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

EDIT: 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;
     }
}

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
}

Here is an example of Repository

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
}

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM