1

エンティティフレームワーク4.0で作業単位パターンを実装する場合、オブジェクトコンテキストのCreateQueryメソッドを使用して動的クエリを作成するための正しい設計とは何ですか?そして正しいことは、何とか私がモックオブジェクトを使って単体テストで使うことができるように設計することを意味します。Entity Framework 4の動的クエリのユニットテスト

ありがとうございました。

答えて

0

あなたの状況に応じて「正しい」という意味が分かりませんが、一般的な答えは、テスト中に置き換えることができる抽象概念が必要であるということです。いくつかのインスピレーションのために、ここで私たちが使用しているものである(私たちはPOCOエンティティを追跡し、自己を使用)

/// <summary> 
/// Object context interface for abstracting the service layer from the 
/// implementation details of object storage. 
/// </summary> 
public interface IObjectContext : IDisposable 
{ 
    /// <summary> 
    /// Returns the entity objects of the given type known by this object context. 
    /// </summary> 
    /// <typeparam name="TEntity">The type of the entity.</typeparam> 
    /// <returns>The instances of the given type already loaded into this object context</returns> 
    IEnumerable<TEntity> GetManagedEntities<TEntity>() where TEntity : EntityBase; 

    /// <summary> 
    /// Creates an object set for the provided entity type. 
    /// </summary> 
    /// <typeparam name="TEntity">The type of the entity.</typeparam> 
    /// <returns></returns> 
    IObjectSet<TEntity> CreateObjectSet<TEntity>() where TEntity : EntityBase; 

    /// <summary> 
    /// Applies the changes made to the provided entity object graph to this object context. 
    /// </summary> 
    /// <typeparam name="TEntity">The type of entity (inferred).</typeparam> 
    /// <param name="entity">The entity object graph that has been modified.</param> 
    void ApplyChanges<TEntity>(TEntity entity) where TEntity : EntityBase, IObjectWithChangeTracker; 

    /// <summary> 
    /// Saves the changes known by this object context instance to the database. 
    /// </summary> 
    void Save(); 

    /// <summary> 
    /// Creates a new logical unit of work spanning a single business transaction. 
    /// </summary> 
    IUnitOfWork CreateUnitOfWork(); 

    /// <summary> 
    /// Creates a new logical unit of work spanning a single business transaction. 
    /// </summary> 
    /// <param name="isolationLevel">The transaction isolation level used by the 
    /// unit of work ambient transaction.</param> 
    /// <returns></returns> 
    IUnitOfWork CreateUnitOfWork(IsolationLevel isolationLevel); 
} 

とあなたのUOWを実装+これらのインターフェイスをサポートするために、オブジェクトコンテキストの生成コードを変更し、その後の取引

/// <summary> 
/// Interface abstraction for a unit of work, aka all persistent work spanning a single 
/// business transaction and to be performed in unity. 
/// </summary> 
/// <remarks>Used to support outer and inner units of work where one outer UoW may consist 
/// of multiple nested inner UoW instances and have all of them share a transaction.</remarks> 
public interface IUnitOfWork : IDisposable 
{ 
    /// <summary> 
    /// Gets the transaction isolation level used by the unit of work ambient transaction. 
    /// </summary> 
    /// <value>The isolation level.</value> 
    IsolationLevel IsolationLevel { get; } 

    /// <summary> 
    /// Gets the transaction timeout time span used by the unit of work ambient transaction. 
    /// </summary> 
    /// <value>The transaction timeout duration.</value> 
    TimeSpan TransactionTimeout { get; } 

    /// <summary> 
    /// Completes the unit of work that this instance represents. 
    /// </summary> 
    /// <remarks> 
    /// For an outer unit of work, will persist all changes represented by this business 
    /// transaction and will then signal the ambient transaction in use as complete. 
    /// For an inner unit of work, will signal itself as completed to the outer unit of 
    /// work that it is part of. 
    /// </remarks> 
    void Complete(); 
} 

を抽象化しますあなたのニーズに応じて(簡略化のために私たちの実装は省略されています)。 HTH

関連する問題