[英]Is there a code smell in this Generic Repository using Ado.Net?
我一直在阅读这篇文章,以刷新我对存储库模式的理解,并发现我的存储库可以是适用于使用该存储库的业务层的多种方法的组合。 因此,有了这种了解,我想到了UserRepository的此类和接口
public class UserRepo : IUserRepo
{
public TheUser RegisterUser(UserRegistrationDetails details)
{
//The details here are what is required for inserting to the user table plus a
//few other related tables which are decided and populated at the Business layer.
}
//example method returning data from a combination of multiple tables
//and a dto created specially for that purpose
public BaggageDetails GetBaggageDetailsForUser(string username)
{
//implementation here
}
//example method returning data from a combination of multiple
//different tables
public TravelDetails GetTravelDetailsForUser(string username)
{
//implementation here
}
}
在这里,这些方法基本上返回一个自定义dto
,在每种情况下,该dto
具有[Users]
表的全部或某些字段,以及不属于[Users]
表的一堆其他字段。
然后,我碰到了这篇文章 ,其中谈到了一个通用存储库,所以现在我的类和接口看起来像这样
public class UserRepo : IDisposable, IUserRepo, IGenericRepo<TheUser> //,IGenericRepo<Users>
{
public TheUser RegisterUser(UserRegistrationDetails details)
{
//
}
//example method....
public BaggageDetails GetBaggageDetailsForUser(string username)
{
//implementation here
}
//example method....
public TravelDetails GetTravelDetailsForUser(string username)
{
//implementation here
}
//New methods added for the generic repository
public IList<TheUser> GetAll()
{
//
}
public TheUser GetById(int id)
{
//
}
public void Save(TheUser saveThis)
{
//
}
public void Delete(TheUser deleteThis)
{
//
}
//Should I implement this here?
public void Dispose()
{
//TODO
}
}
我可以通过许多这样的接口实现UserRepo
吗? 通用存储库具有与存储库的聚合根相关的方法(如果我使用的是“正确”一词)。
问题
IGenericRepo<Users>
IGenericRepo<TheUser>
上面所做的那样,将属于聚合根的dto放到IGenericRepo<Users>
还是将更大的dto IGenericRepo<Users>
比更新[Users]
表所需的信息更多的信息上。 UserRepo
,则不确定要使用哪个接口 编辑除了上述问题的答案以外,我还在寻找有关创建存储库的想法。 我知道这属于“取决于”类别,但是很想听听大家对这种构建存储库的想法。 在现场项目中创建和使用存储库的人做了什么?
通用存储库只有在您以通用方式处理数据时才有意义。 经典的存储库模式会建立Get(GetAll / GetById等),Create和Delete,但是可能会跳过其中的一些。 这非常取决于您的应用程序。 重要的是要考虑到存储库为您的数据存储创建了抽象级别,以便能够用另一种替换现有的数据存储,并且您更有可能使用一些模拟数据进行测试。
在您的示例中,您实现了非常特定的用户存储库,而我看不到您的应用程序中还需要多少数据。 因此,您似乎在尝试通过实现通用存储库接口来强制用户存储库执行根本不需要的操作。
如果您确实具有基于数据处理的通用基础,我建议使用通用方法。
对于一个特定的用例,DTO(数据传输对象)应在用户端(例如UI)上提供所需的大量数据。 这实际上取决于您要一次可视化/使用多少数据,并且应该与数据传输成本和服务呼叫次数保持平衡。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.