taking advantage of inheritance in Controllers and Views

前端 未结 3 454
清歌不尽
清歌不尽 2021-01-04 06:10

I had posted this review on codereview.stackexchange.com a while ago... I feel it may be more suitable for stackoverflow, since it is more of a question than a code review.

3条回答
  •  长情又很酷
    2021-01-04 06:59

    Forgive me if I've misunderstood but provided you added a genric UOW it seems to me you could do something like this: I don't see why it would be bad to do this

    public class AdBaseController : ControllerBase
    {
        private IUnitOfWork _unitOfWork;
    
        public AdBaseController(IUnitOfWork unitOfWork)
        {
            _unitOfWork = unitOfWork;
        }
    
        public ActionResult GetDisplayAction(long id)
        {
            SimpleAd simpleAd = _unitOfWork.GenericAdRepository.Get(id)
            var viewModel = Mapper.Map(simpleAd);         
            return View(viewModel);
        }
    }
    
    public class SimpleAdController : ControllerBase
    {    
        public SimpleAdController(IUnitOfWork unitOfWork) : base(unitOfWork)
        {
        }
    
        [HttpGet]
        public ActionResult Display(long id)
        {
            return GetDisplayAction();
        }
    }
    

提交回复
热议问题