开发者

Repository Pattern Question and Generics?

开发者 https://www.devze.com 2023-02-08 03:54 出处:网络
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 searc

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


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>

}
0

精彩评论

暂无评论...
验证码 换一张
取 消