用于轻松扩展搜索条件的设计模式 [英] Design pattern for extending search criteria easily

查看:80
本文介绍了用于轻松扩展搜索条件的设计模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索条件应使用哪种设计模式。



像按名称搜索,ID ...以后我们可以通过DOB轻松扩展它/>


我尝试了什么:



工厂设计模式,但不能能够做到这一点。我是设计模式的初学者,刚刚开始实现它。

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.

推荐答案

您好,对不起的回答,



使用shearch-Criteria类通常用于prjects。

您可以为每个数据访问对象创建一个搜索类。

这是一个小例子:< br $>


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});



然后,您可以在不更改旧代码的情况下向搜索类添加属性。



缺点:

你一般只使用几个字段/ SearchClass的Prop,要知道填充的确切支柱,你应该回去查看每次代码,这就是为什么方法命名非常重要,它有助于找出要使用的字段。



更多:

您可以考虑使用BaseSearchCriteria类来实现常用功能......



希望有所帮助。


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.


这篇关于用于轻松扩展搜索条件的设计模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆