I have an ASP.NET application with 3 layers UI, Service Layer and Repository.
I have to implement a search functionality on the products in the database.
ProductRepository
is my repository class and the signature of the method that gets all the products from the database is:
IQueryable<Product> GetAllProducts();
Therefore from my Service Layer I use:
IProductRepository _ProductRepository = new ProductRepository();
IQueryable<Product> products = _ProductRepository.GetAllProducts();
If I would like to filter IQueryable<Product> products
, for instance by taking those product with price > 100 or just those with color = "yellow".
So I was wondering, instead of creating methods in ProductRepository
such as:
IQueryable<Product> GetAllProductsByColor(int colorId)
I was wondering if it is a good practice to create in my Service Layer a set of methods that accept IQueryable<Product>
as parameter and perform the filtering directly there:
IQueryable<Product> FilterProducts(IQueryable<Product> products, Dictionary<string, object> filters)
Where Dictionary<string, string>
represent a set with (propertyName, value).
The advantage of this solution is that if I have to apply multiple filters I just pass the IQueryable<Product>
already filtered, instead of taking everytime the intersection between the filtered product sets.
Is it a good practice (as long as I keep the context open) or it is not "allowed"by the multi-layered architecure pattern?