4

我有一个处理 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));

我的问题确实是这样的,我认为:

  1. 创建这样的映射器是否可行,如果可以...
  2. 使用 AutoMapper 之类的工具是否可行,如果可以...
  3. 是否可以利用我已经拥有的 AutoMapper 映射,DomainType或者DataEntityType我需要显式映射Query<DomainType>Query<DataEntityType>

我最终希望这样做是为了能够灵活地使用不一定是简单对象属性的任意映射函数。

4

2 回答 2

1

恕我直言,这个设计看起来很复杂。您是否调查过直接通过 ORM 定义域类型的可能性?毕竟,这就是 ORM 的设计目的......如果没有这种巨大的额外努力,它肯定会打开很多可能性......

很多人不知道 Linq 2 SQL至少在某种程度上支持POCO 风格?如果您可以考虑其他 ORM 替代方案,NHibernate 和实体框架都具有更好的 POCO 支持。

POCO 允许您直接在 ORM 之上定义域模型,但您的域类可以(或多或少,取决于实际的 ORM)“不了解持久性”。因此,您可以利用 ORM 的查询功能向您的用户公开丰富的域查询 API。

如果您考虑实体框架 (v4),您可能还想看看WCF 数据服务

只是我的2美分...

于 2010-08-28T22:09:42.667 回答
0

如果您要允许针对 ViewModel 编写的 LINQ 查询并在使用实体模型的存储库中执行它们,您还需要检查 LINQ 表达式树和表达式树转换。AutoMapper 在翻译对象时会有所帮助,但您的表达式树也需要翻译。

http://blogs.msdn.com/b/charlie/archive/2008/01/31/expression-tree-basics.aspx

于 2012-12-28T09:43:04.580 回答