我有一个处理 LINQ to SQL 自动生成实体的存储库层。这些最终被映射到表面上的领域友好类型。我现在想为客户端代码提供一些更复杂的查询功能,而客户端代码只知道域对象类型。
我想用查询对象模式(在 Martin Fowler 的企业应用程序架构模式中命名)来实现这一点,但允许客户端代码使用域类型的 lambda 表达式。在幕后,我想将域感知 lambda 表达式转换为数据库感知 lambda,并将转换后的表达式发送到存储库,以便使用 LINQ to SQL 对数据库执行。
我目前有一个穷人的实现,它将客户端的映射能力限制为简单的属性,但我想对更复杂的查询开放一点。我不确定如何使用 AutoMapper 或任何其他现有的映射工具来解决这个问题,我也不确定 OTTOMH 我如何使用本土代码来做到这一点。
这是我想要的功能:
// Example types to be interconverted...
// DomainId should map to DataEntityId and vice versa
// DomainName should map to DataEntityName and vice versa
public class DomainType
{
public int DomainId { get; set; }
public string DomainName { get; set; }
}
public class DataEntityType
{
public int DataEntityId { get; set; }
public string DataEntityName { get; set; }
}
// And this basic framework for a query object.
public class Query<T>
{
public Query(Func<T, bool> expression) { ... }
public Func<T, bool> Query { get; }
}
// And a mapper with knowledge about the interconverted query types
public class QueryMapper<TSource, TDestination>
{
public void SupplySomeMappingInstructions(
Func<TSource, object> source, Func<TDestination, object> dest);
public Query<TDestination> Map(Query<TSource> query);
}
// And a repository that receives query objects
public class Repository<T>
{
public IQueryable<T> GetForQuery(Query<T> query) { ... }
}
最终目标是让这样的事情发挥作用:
// a repository that is tied to the LINQ-to-SQL types.
var repository = new Repository<DataEntityType>(...);
// a query object that describes which domain objects it wants to retrieve
var domain_query = new Query<DomainType>(item => item.DomainId == 1);
// some mapping component that knows how to interconvert query types
var query_mapper = new QueryMapper<DomainType, DataEntityType>();
query_mapper.SupplySomeMappingInstructions(
domain => domain.DomainId, data => data.DataEntityId);
query_mapper.SupplySomeMappingInstructions(
domain => domain.DomainName, data => data.DataEntityName);
IQueryable<DataEntityType> results =
repository.GetForQuery(query_mapper.Map(domain_query));
我的问题确实是这样的,我认为:
- 创建这样的映射器是否可行,如果可以...
- 使用 AutoMapper 之类的工具是否可行,如果可以...
- 是否可以利用我已经拥有的 AutoMapper 映射,
DomainType
或者DataEntityType
我需要显式映射Query<DomainType>
到Query<DataEntityType>
?
我最终希望这样做是为了能够灵活地使用不一定是简单对象属性的任意映射函数。