mercredi 18 mai 2016

How to use IService in architecture with UnitOfWork and Repository in ASP.NET MVC?

I implement an architecture with unitofwork and Repository that worked correctly. I have a entity that is called Product and I Use ProductService in controller for CRUD operetioan.

 public interface IProductService
{
    IList<Product> GetAll();
    Product GetById(int id);
    void Create(Product product);
    void Update(Product product);
    void Delete(int id);
}

public class ProductService : IProductService
{
    private IRepository<Product> _productRepository;

    public ProductService(IRepository<Product> productRepository)
    {
        _productRepository = productRepository;
    }

    public IList<Product> GetAll()
    {
        return _productRepository
            .GetAll()
            .ToList();
    }

    public Product GetById(int id)
    {
        return _productRepository.GetById(id);
    }

    public void Create(Product product)
    {
        _productRepository.Create(product);
    }

    public void Update(Product product)
    {
        _productRepository.Update(product);
    }

    public void Delete(int id)
    {
        _productRepository.Delete(id);
    }

}

Now I write a Service Base that included All Method And write this:

public interface IProductService:ServiceBase

I do not want to write these methods again.

Aucun commentaire:

Enregistrer un commentaire