存储库模式问题和泛型?

发布于 2024-10-16 01:11:58 字数 1060 浏览 1 评论 0原文

我需要有关 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏九 2024-10-23 01:11:59

我会实现这样的事情。它足够通用,代码的最大部分可以在存储库的基类中实现。

public interface IRepository<T> where T: IProduct
{
   IEnumerable<T> Search(Predicate<T> query)
   IEnumerable<T> Search(IEnumerable<Predicate<T>> query>

}

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.

public interface IRepository<T> where T: IProduct
{
   IEnumerable<T> Search(Predicate<T> query)
   IEnumerable<T> Search(IEnumerable<Predicate<T>> query>

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文