Implementing Repository pattern and doing Tests

后端 未结 2 925
别跟我提以往
别跟我提以往 2020-12-18 13:03

I have read almost all articles about Repository pattern and different implementations of it. Many of them judged bad practices (ex: using IQueryable i

2条回答
  •  萌比男神i
    2020-12-18 14:03

    The repository pattern adds an extra layer when you are using EF, so you should make sure that you need this extra level of indirection.

    The original idea of the Repository pattern is to protect the layers above from the complexity of and knowing about the structure of the database. In many ways EF's ORM model protects the code from the physical implementation in the database so the need for a repository is less.

    There are 2 basic alternatives:

    • Let the business layer talk directly to the EF model
    • Build a datalayer that implements the Repository pattern, this layers maps EF objects to POCO

    For tests:

    • When we use EF directly we use a transaction scope with rollback, so that the tests do not change the data.
    • When we use the repository pattern we use Rhino Mocks to mock the repository

    We use both approaches, Repository pattern gives a more clearly layered app and therefore maybe more control, using EF directly gives an app with less code and therefore faster to build.

提交回复
热议问题