Using the repository pattern to support multiple providers

前端 未结 1 1274
广开言路
广开言路 2020-12-08 03:31

Well, not sure if that\'s exactly the right title, but basically I have been having a lot of problems using repositories in MVC applications in such a way that you can subst

相关标签:
1条回答
  • 2020-12-08 03:52

    What I can say? Welcome to the club ...

    What you found is problem reached by many developers who followed "repository boom" with EFv4. Yes it is the problem and the problem is really complex. I discussed this several times:

    • ASP.NET MVC 3 and Entity Framework code first architecture
    • Organizationally, where should I put common queries when using Entity framework

    Separate topic is why to use repositories:

    • Generic repository, what is the point

    Basically your proposed way is a solution but do you really want it? In my opinion the result is not repository but the Data Access Object (DAO) exposing plenty of access methods. Repository definition by Martin Fowler is:

    A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection. Client objects construct query specifications declaratively and submit them to Repository for satisfaction. Objects can be added to and removed from the Repository, as they can from a simple collection of objects, and the mapping code encapsulated by the Repository will carry out the appropriate operations behind the scenes. Conceptually, a Repository encapsulates the set of objects persisted in a data store and the operations performed over them, providing a more object-oriented view of the persistence layer. Repository also supports the objective of achieving a clean separation and one-way dependency between the domain and data mapping layers.

    I believe exposing IQueryable fulfils this 100 times better then creating a public interface similar to repositories from Stored procedures era - one access method per stored procedure (fixed query).

    The problem can be summarized by the rule of leaky abstraction. IQueryable is an abstraction of the database query but the features provided by IQueryable are dependent on the provider. Different provider = different feature set.

    What is a conclusion? Do you want such architecture because of testing? In such case start using integration tests as proposed in first two linked answers because in my opinion it is the lest painful way. If you go with your proposed approach you should still use integration tests to verify your repositories hiding all EF related logic and queries.

    0 讨论(0)
提交回复
热议问题