Best Repository Pattern for ASP.NET MVC

后端 未结 4 387
野的像风
野的像风 2020-12-07 06:54

I recently learned ASP.NET MVC (I love it). I\'m working with a company that uses dependency injection to load a Repository instance in each request, and I\'m familiar with

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 07:36

    I would recommend number 1, with some caveats. Number 2 is what seems to be most common but in my experience the repository just ends up a messy dumping ground for queries. If you use a generic repository (2) it is just a thin wrapper around the DBContext, a bit pointless really unless you are planning on changing ORM's (bad idea).

    But when I access DBContext directly I prefer to use a Pipes and Filters pattern so you can reuse common logic, something like

    items = DBContext.Clients
        .ByPhoneNumber('1234%')
        .ByOrganisation(134);
    

    The ByPhoneNumber and By Organisation are just extension methods.

提交回复
热议问题