Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Which design pattern should use for search criteria.

Like Search by Name, ID...and in future we can easily extend it by DOB also

What I have tried:

Factory design pattern, but coudn't able to do it. I am beginner in design pattern, just now started implementing it.
Posted
Updated 24-Jan-18 3:06am
Comments
Tomas Takac 23-Jan-18 7:25am    
I believe you are looking for specification pattern[^]. But it really depends on what you are trying to achieve. Maybe posting your code (via Improve question) to illustrate the problem would clear things up and you could get a more specific answer.
Ashfaque Hussain 23-Jan-18 11:04am    
Let say we have entered two search criteria First Name and Last Name. And I wrote a class for Searching by Name. In future, if I want to add one more criteria to search by date of birth. In this scenario which design pattern should use.
Ziee-M 23-Jan-18 10:36am    
No need for a design pattern, just use a class for search criteria, you can extend that class properties later : Method(SearchCriteria searchCriteria)
Ashfaque Hussain 23-Jan-18 10:39am    
Thank you Ziee, could you please explain more?

1 solution

Hi and sorry for the late answer,

Using shearch-Criteria classes is commonly used in prjects.
You can create a search class for each Data access object.
Here is a little example :

public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Adress { get; set; }
        public DateTime BirthDate { get; set; }
    }
    public class SearchPerson
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Adress { get; set; }
        public int Age { get; set; }
    }

    public class PersonDal
    {
        public Person GetPersonById(SearchPerson searchCriteria)
        {
            //Logic to get a  Person based on Id
        }
        public List<Person> GetListPersonByAge(SearchPerson searchCriteria)
        {
            //Logic to get a List of Person based on Age
        }
    }

//use 
PersonDal pDal = new  PersonDal ();
Person person1 = pDal.GetPersonById(new SearchPerson{Id = 5});
List<Person> personList = pDal.GetListPersonByAge(new SearchPerson{Age = 20});

You can then add properties to your search class without changing any old code.

CONS :
You will in general use only few fields/Prop of the SearchClass, to know the exact prop to fill, you should go back and check the code each time, thats why method naming is really important, it helps figure out the fields to use.

More :
You can think about a BaseSearchCriteria class for common functionality...

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900