简体   繁体   English

一种允许对存储库进行动态过滤/查询的体系结构<SomeDomainModel>

[英]An architecture that allows for dynamic filter/querying of Repository<SomeDomainModel>

I'm looking for a solid architecture that will solve the following problem: 我正在寻找一种能够解决以下问题的可靠架构:

As a user i want to dynamically filter some database of Persons, by dynamically adding criterias. 作为用户,我想通过动态添加条件来动态过滤某些人员数据库。 I also want to be able to batch-update only chosen fields to the database, based on the results of the filter. 我还希望能够根据过滤器的结果将选定的字段仅批量更新到数据库。

The following is true: 以下是正确的:

  • I have a 3-tiered solution (GUI, Business Layer, Data Access Layer). 我有一个3层解决方案(GUI,业务层,数据访问层)。
  • Suppose I have a Repository<Person> , the Person is the domain model. 假设我有一个Repository<Person> ,那么Person是域模型。
  • The repository supports linq and can give me: IQueryable<T> GetAll() 该存储库支持linq并可以给我: IQueryable<T> GetAll()

I got some ideas: 我有一些想法:

  • I'm thinking of using Dynamic Expression API, which extends LINQ functionality so i can dynamically add search criterias. 我正在考虑使用动态表达式API,该API扩展了LINQ功能,因此我可以动态添加搜索条件。
  • I've looked at NHibernate QueryBuilder, and that might be something to borrow ideas from. 我看过NHibernate QueryBuilder,这可能是可以借鉴的想法。

Solution Theories: 解决方案理论:
So, anyway I'm thinking of the GUI asking the DomainModel to describe itself (what properties exists, and what operators are allowed, and what are the allowed valuetypes) That way, I could render some cascading dropdowns for the properties and allowed operators. 因此,无论如何,我想到的是GUI要求DomainModel描述自身(存在哪些属性,允许哪些运算符以及允许的值类型)。这样,我可以为属性和允许的运算符提供一些级联的下拉列表。 I'm thinking of describing properties either from a method in the Model, for example: DescribeMyself(); 我正在考虑通过Model中的方法描述属性,例如: DescribeMyself(); And then combining this with decorating the properties with Attributes. 然后结合使用Attributes装饰属性。

Questions: 问题:
So what about describing properties for a Model, how would you do that? 那么,描述模型的属性又如何呢? For example, the property: string Name , should be searchable with either an operator of Equals , or an operator of Like. 例如,应使用Equals运算符或Like运算符来搜索属性: string Name
What about enums ? 枚举呢? Imagine MyGenderEnum Gender . 想象一下MyGenderEnum Gender When doing an Equals filter, i need to describe that it must be matched to either of any value in the given Enum. 在做Equals过滤器时,我需要描述它必须与给定Enum中的任何值匹配。

Something to think about! 想一想!

That's a rather broad question. 这是一个相当广泛的问题。

Just some points: 几点:

  • Dynamically adding search criteria to an IQueryable<T> is supported out of the box. 开箱即IQueryable<T>支持将搜索条件动态添加到IQueryable<T> Just add a call to Where for each criteria: 只需为每个条件添加呼叫到Where

    repository.GetAll<Person>().Where(x => criteria1(x)).Where(x => criteria2(x));

  • Making the model describe itself is something I wouldn't do. 我不会做使模型描述自身的事情。 I would create a new class, similar to Fluent NHibernates ClassMap<T> or Fluent Validations AbstractValidator<T> . 我将创建一个新类,类似于Fluent NHibernates ClassMap<T>或Fluent Validations AbstractValidator<T>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM