我正在尝试将参数自动生成到 IQueryable.Where,这样我就可以从我的实体框架代码第一个数据上下文中选择实体,而无需编写和连接大量繁琐的映射代码。
我的项目包含一堆 DTO,如下所示:-
class FooDto
{
public string SomeProperty { get; set; }
public string SomeOtherProperty { get; set; }
}
还有一堆看起来像这样的实体:-
class Foo
{
public string SomeProperty { get; set; }
public string SomeOtherProperty { get; set; }
// Some other properties here.
}
DTO 包含识别数据库中某些实体子集所需的字段。我使用 DTO 查询实体的 IQueryable:-
var result = queryable.Where(
x => x.SomeProperty == dto.SomeProperty
&& x.SomeOtherProperty == dto.SomeOtherProperty)
实际属性有所不同,但查询始终具有“实体上的所有属性都匹配 DTO 上的所有匹配属性”的形式。没有更复杂的查询对象功能。
有许多 DTO 和实体。创建/维护和连接所有这些谓词是一个具有挑战性的架构问题。我们目前正在使用策略模式:-
public class FooDtoSelectStrategy : ISelectStrategy<FooDto, FooEntity>
{
public Func<FooEntity, bool> GetPredicate(FooDto dto)
{
return x => x.SomeProperty == dto.SomeProperty
&& x.SomeOtherProperty == dto.SomeOtherProperty;
}
}
除了一堆 ninject 绑定之外,我们已经有了几十个这样的绑定,随着我们的域的扩展,我们正在寻找数百个。
我们有一个类似的挑战,将值从实体映射到我们使用 AutoMapper 解决的 DTO。
automapper(或类似工具)可以创建这些谓词并允许我们实现单个GenericPredicateProvider<TDto, TEntity>
?