Why use Repository Pattern or please explain it to me?

前端 未结 8 1643
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 01:10

I am learning repository pattern and was reading Repository Pattern with Entity Framework 4.1 and Code First and Generic Repository Pattern - Entity Framework, ASP.NET MVC and

相关标签:
8条回答
  • 2021-01-30 01:35

    I don't think you should.

    The Entity Framework is already an abstraction layer over your database. The context uses the unit of work pattern and each DBSet is a repository. Adding a Repository pattern on top of this distances you from the features of your ORM.

    I talked about this in my blog post: http://www.nogginbox.co.uk/blog/do-we-need-the-repository-pattern

    The main reason adding your own repository implementation is so that you can use dependency injection and make your code more testable.

    EF is not very testable out of the box, but it's quite easy to make a mockable version of the EF data context with an interface that can be injected.

    I talked about that here: http://www.nogginbox.co.uk/blog/mocking-entity-framework-data-context

    If we don't need the repository pattern to make EF testable then I don't think we need it at all.

    0 讨论(0)
  • 2021-01-30 01:37

    I know it is bad provide links in answer here, however wanted to share the video which explains various advantages of Repository Pattern when using it with Entity framework. Below is the link of youtube.

    https://www.youtube.com/watch?v=rtXpYpZdOzM

    It also provides details about how to implement Repository pattern properly.

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