LINQtoSQL自動生成エンティティを処理するリポジトリレイヤーがあります。これらは最終的に、表面上でドメインに適したタイプにマッピングされます。ここで、クライアントコードにさらに高度なクエリ機能を提供したいと思います。そのクライアントコードは、ドメインオブジェクトタイプについてのみ認識しています。
これをクエリオブジェクトパターン(Martin Fowlerのエンタープライズアプリケーションアーキテクチャのパターンで名前が付けられている)で実装したいのですが、クライアントコードがドメインタイプでラムダ式を使用できるようにします。裏で、ドメイン対応のラムダ式をデータベース対応のラムダに変換し、この変換された式をリポジトリに送信して、LINQtoSQLを使用してデータベースに対して実行したいと思います。
私は現在、クライアントのマッピング機能を単純なプロパティに制限する貧乏人の実装を持っていますが、それをもう少し洗練されたクエリに開放したいと思います。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>
ますか?
最終的には、必ずしも単純なオブジェクトプロパティではない任意のマッピング関数を柔軟に使用できるようにするためにこれを実行したいと思います。