开发者

is it correct architecture of ASP.NET MVC & EF app?

开发者 https://www.devze.com 2023-04-12 06:35 出处:网络
I have the following repository class: public class Model1Repository { private 开发者_如何学JAVANEOGOV_IdeasEntities _dataContext;

I have the following repository class:

public class Model1Repository
{
    private 开发者_如何学JAVANEOGOV_IdeasEntities _dataContext;
    public Model1Repository()
    {
        _dataContext = new NEOGOV_IdeasEntities();
    }
    public IdeaType IdeaType(int ID)
    {
        var q = from i in _dataContext.IdeaTypes where i.ID == ID select i;
        return q.FirstOrDefault();
    }
    ... rest methods....
}

following controller:

public class TestController : Controller
{
    private Model1Repository _repository;

    public TestController()
    {
        _repository = new Model1Repository();
    }
    public ActionResult Tagedit()
    {
        return View(_repository.GetDataFromDB1());
    }

    public ActionResult Avatar()
    {
        return View(_repository.GetDataFromDB2());
    }
}

is it correct architecture? Or not? As I understand, in my case _repository variable creates for each request...


Personally, I'd instantiate the repository in the controller action, not the constructor. If you're positive that all controller actions will require the repository, then fair enough, but since the operations on a controller relate to the operation of the UI, it's not at all unusual to have controller methods that don't require it.

But it's really a style thing at the end of the day (OK, perhaps with some minor performance overhead); if you want to do it that way, feel free!


Can't answer for the entire architecture, but there will be one and only one repository Model1Repository object instantiated for the TestController, not one per request (HTTP request).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号