创建存储库模式接口

时间:2017-04-16 18:40:57

标签: generics design-patterns interface repository

我是C#中使用接口的新手。我正在观看有关它的教程并学习如何使用它。

我真的是最好的做法。在本教程中,它仅使用接口上的Person类来执行CRUD。我只是想知道为每个模型类创建不同的接口存储库是否可行,例如Person(IPersonRepository),Animal(IAnimalRespository)等,如果操作相同,我可以使用IObjectRepository吗?

1 个答案:

答案 0 :(得分:0)

如果操作相同,则可以使用通用的通用存储库接口。

一旦您需要某种特定方法来满足特定需求,您就可以创建一个不太通用的界面。

您需要考虑因为C#是强类型语言,如果您使用依赖注入构建解决方案,则无论何时何地都需要替换通用/通用接口类型我们决定定义一个具体的界面:

// This is a sample of what I was talking about in the previous paragraph. 
// Just imagine how many places might need to replace IRepository<Animal>
// with IAnimalRepository in a big solution!
public class WhateverService
{
     public WhateverService(IRepository<Animal> animalRepository)
     {
     }
}

决定取决于您,而且取决于您项目的规模。