控制器实现的接口用法

时间:2014-02-21 18:58:20

标签: asp.net-mvc oop interface

我在我的应用程序中使用MVC模式。对于每个模型类,我都有一个控制器。所有控制器类都有saveOrUpdate()方法。我想知道这是否足以创建一个定义所述方法的接口,然后所有控制器都实现它。

请注意saveOrUpdate()接收模型类作为参数。所以它会像UserController#saveOrUpdate(User user)CourseController#saveOrUpdate(Course course)AppleManager#saveOrUpdate(Apple apple)

2 个答案:

答案 0 :(得分:1)

创建一个界面

interface ISave
 {
      void Save(object obj);
 }

现在在您的控制器中实现它。

public class AppleControler : Controller , ISave
{

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

        }

 }

选项二

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


public class AppleControler : Controller , ISave<Apple>
{

        public void Save(Apple obj) 
        {

        }

 }

答案 1 :(得分:1)

我认为您需要的是通用存储库,它实现给定实体的通用功能。我最近开始在我的MVC项目中实现Repository PatternUnit of Work。我就是这样做的。

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

    }

工作单位:

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

通用存储库:

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

    }

继承自通用仓库的实体存储库:

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

控制器:

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