Why use Repository Pattern or please explain it to me?

前端 未结 8 1662
隐瞒了意图╮
隐瞒了意图╮ 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:32

    Repository systems are good for testing.

    One reason being that you can use Dependency Injection.

    Basically you create an interface for your repository, and you reference the interface for it when you are making the object. Then you can later make a fake object (using moq for instance) which implements that interface. Using something like ninject you can then bind the proper type to that interface. Boom you've just taken a dependence out of the equation and replaced it with something testable.

    The idea is to be able to easily swap out implementations of objects for testing purposes Hope that makes sense.

提交回复
热议问题