lundi 18 juillet 2016

Creating a generic Search Helper object for sending search criteria across Web API and using in repository where clauses

I am attempting to create a search helper that will use generics in C#. I want this to be the only parameter to my repository functions rather than having several params of lists.

For example I want to replace the following types of methods:

    GetProducts(List<int> categoryIds = null)
    "WHERE (@CategoryIds IS NULL OR p.CategoryId IN @CategoryIds)"

    GetCustomers(List<int> projectIds = null, List<string> zipCodes = null)
    "WHERE (@ProjectIds IS NULL OR proj.ID IN @ProjectIds)
     AND (@ZipCodes IS NULL OR c.ZipCode IN @ZipCodes)"

with something like:

GetProducts(FilterHelper filters)
GetCustomers(FilterHelper filters)

filters will somehow contain all of the information I need to do the WHERE clauses with Dapper.

Since I am using Web API I am trying to create a single DTO called Filter that can send information like this down to my Repository layer. Then have the repository layer handle the rest.

Can anyone lead me in the right direction on this? I am assuming I would use generics and possibly reflection?





Aucun commentaire:

Enregistrer un commentaire