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); 
} 

및 추출 거래

/// <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(); 
} 

는 이들 인터페이스는 + 당신의 UOW 구현 지원하는 개체 컨텍스트 생성 코드를 수정

에 대한

귀하의 필요에 따라 (간결성을 위해 구현이 생략되었습니다). HTH