best practice to implement repository pattern and unitOfWork in ASP.NET MVC [closed]

落爺英雄遲暮 提交于 2019-12-04 19:39:08

While it is opinion based, just the name of of Unit Of Work pattern suggest limited timespan of the object. So the use of it in controller should be something like

public ActionResult Index()
{
   using(var UOF = new Function_UnitOfWork()) {

     var a1 = UOF.GetAllFunctions();

     foreach(var item in a1)
     {
       var b1 = item.Title;
     }
   }

    return View();
}

Also one approach we use is (in short)

public class DataObject { }

public class Repo: IRepo<T> where T: DataObject

public class RepoController<T> : Controller where T: DataObject {
   protected IRepo<T> Repo;
}

So controllers will be generic and will have field for particular repo

You will use some dependency injection tool, like ninject or mef to bound controllers with repos behind the scene.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!