0

I'm using MVC pattern in my application. For each model class I have a controller one. All controller classes have a saveOrUpdate() method. I am wondering if this is enough to create an Interface which defines said method, and then all controller implements it.

Please note that saveOrUpdate() receive a model class as a parameter. So it would be something like UserController#saveOrUpdate(User user), CourseController#saveOrUpdate(Course course), AppleManager#saveOrUpdate(Apple apple).

2 Answers 2

1

I think what you need is generic repository which implements generic functionality for a given entity. I've recently started implementing Repository Pattern along with Unit of Work in my MVC projects. Here is how I do that.

MyDbContext.cs:

public class MyDbContext : DbContext
    {
        public MyDbContext() : base("name=DefaultConnection”)
        {
        }

        public System.Data.Entity.DbSet<User> Users { get; set; }
        public System.Data.Entity.DbSet<Course> Courses { get; set; }

    }

Unit of Work:

public class UnitOfWork : IUnitOfWork
    {
        //private variable for db context
        private MyDbContext _context;
        //initial db context variable when Unit of Work is constructed
        public UnitOfWork()
        {
            _context = new MyDbContext();
        }
        //property to get db context
        public MyDbContext Context
        {
            //if not null return current instance of db context else return new
            get { return _context ?? (_context = new MyDbContext()); }
        }
        //save function to save changes using UnitOfWork
        public void Save()
        {
            _context.SaveChanges();
        }
    }

Generic Repository:

public class RepositoryBase<T> : IRepositoryBase<T> where T : class
    {
        protected readonly IUnitOfWork _unitOfWork;
        private readonly IDbSet<T> _dbSet;

        public RepositoryBase(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
            _dbSet = _unitOfWork.Context.Set<T>();
        }

    public virtual void Save()
        {
            _unitOfWork.Save();
        }

        public virtual void Add(T entity)
        {
            _dbSet.Add(entity);
            _unitOfWork.Save();
        }
    //Similarly you can have Update(), Delete(), GetAll() implementation here

    }

Entity Repository inheriting from generic repo:

public class UserRepository:RepositoryBase<User>,IUserRepository
    {
        public UserRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
    //Here you can also define functions specific to User
    }

Controller:

public class UserController : Controller
    {
        private readonly IUserRepository _dbUserRepository;

        public UserController(IUserRepository dbUserRepository)
        {
            _dbUserRepository = dbUserRepository;
        }

        // GET: /User/
        public ActionResult Index()
        {
        var users = _dbUserRepository.GetAll();

            return View(users.ToList());
        }
}
Sign up to request clarification or add additional context in comments.

Comments

1

create an interface

interface ISave
 {
      void Save(object obj);
 }

now in your controller implement it.

public class AppleControler : Controller , ISave
{

        public void Save(Object obj) 
        {
              //you can cast your object here. 

        }

 }

Option two

 interface ISave<T>
 {
      void Save(T obj);
 }


public class AppleControler : Controller , ISave<Apple>
{

        public void Save(Apple obj) 
        {

        }

 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.