存储库模式问题和泛型?
我需要有关 C# 中的设计问题(存储库设计模式)的帮助。我有一个包含多种类型“产品”的系统。每个产品都有“搜索”功能。我想设计一个可重用/通用的搜索界面。根据搜索的产品不同,搜索条件以及返回的数据也会有所不同。
搜索功能只会在特定产品类型内进行搜索。目前,搜索是唯一的功能,但将来可能会有其他功能,例如“GetByID”,它们的行为也会根据类型而有所不同。
我最初的想法是创建一个带有“DoSearch:函数”的“产品”界面
public interface IProduct
{
[return type?] DoSearch([paramters?]);
...
}
public class Product1 : IProduct
{
[product1 result] DoSearch([parameters?])
{
//Do searching logic
return [product1 result]
}
}
,然后我将有一个“业务”类
public class ProductBusiness
{
IProduct _product;
public ProductBusiness(IProduct product)
{
_product = product
}
public [return type?] DoSearch([parameters])
{
return _product.DoSearch([paramters]);
}
}
我的问题是如何使业务类中的[参数]和[返回类型]成为“通用”,因为根据产品类型,参数和返回类型会有所不同?
使用它的客户端代码看起来像这样:
ProductBusiness productBusiness = new ProductBusiness(new Product1());
[product1 result] = productBusiness.DoSearch[parameters]);
...process result code
I need help with a design issue (Repository design pattern) in C#. I have a system with several types of "products". Each product has a "search" function. I want to design a reusable/generic search interface. Depending on the product being searched, the search criteria will be different as well as the returned data.
The search function will only search within the specific product type. For now Search is the only function, but in the future there could be other functions like "GetByID" that would also behave different based on the type.
My initial thoughts were to create a "product" interface with a "DoSearch: function
public interface IProduct
{
[return type?] DoSearch([paramters?]);
...
}
public class Product1 : IProduct
{
[product1 result] DoSearch([parameters?])
{
//Do searching logic
return [product1 result]
}
}
then I would have a "business" class
public class ProductBusiness
{
IProduct _product;
public ProductBusiness(IProduct product)
{
_product = product
}
public [return type?] DoSearch([parameters])
{
return _product.DoSearch([paramters]);
}
}
My issue is how do a make the [parameters] and [return type] in the business class "generic" because depending on the product type the paramters and the return type would be different?
the client code using this would look something like this:
ProductBusiness productBusiness = new ProductBusiness(new Product1());
[product1 result] = productBusiness.DoSearch[parameters]);
...process result code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会实现这样的事情。它足够通用,代码的最大部分可以在存储库的基类中实现。
I would implement something like this. It's generic enough and the biggest part of the code can be implemented in a base class for repository.