2

Entity Framework 컨텍스트 위에 빌드 된 내 WCF RIA 도메인 서비스 계층 테스트를위한 테스트 계층을 작성해야합니다. 저장소를 사용하고 도메인 서비스 팩토리를 사용하여 저장소 인스턴스를 사용하여 도메인 서비스를 사용하도록 제안하는 몇 가지 패턴을 발견했습니다. 비제이의 블로그 (http://blogs.msdn.com/vijayu/archive/2009/06/08/unit-testing-business-logic-in-net-ria-services.aspx)에서 요구 사항에 맞는 샘플 중 하나를 설명합니다. 이 구현의 문제점은 특정 도메인 객체에 대해서만 저장소를 초기화한다는 것입니다. 고객/제품이지만 어떤 객체를 반환 할 수있는 저장소를 생성 할 방법이 없습니다.WCF를 테스트하기위한 모의 저장소 작성 방법 Entity Framework 컨텍스트 위에 RIA 도메인 서비스 빌드

이 작업을 수행하는 올바른 방법과 가능한지 여부를 알려주십시오. 미리

감사

마노 I 자동 또한 필요 LinqToSqlRepositories를 인스턴스화하고 RepositoryCollection 객체와 샘플을 연장함으로써이 문제를 가지고

답변

1

는 수동 모의/스텁 저장소의 삽입을 허용 단위 테스트.

public class RepositoryCollection : IDisposable 
{ 
    private Dictionary<Type, object> _repositories = new Dictionary<Type, object>(); 
    private DataContext _context; 

    public RepositoryCollection() { } 

    public RepositoryCollection(DataContext context) 
    { 
     _context = context; 
    } 

    public IRepository<T> Get<T>() where T : class 
    { 
     if(!_repositories.ContainsKey(typeof(T))) 
      _repositories.Add(typeof(T), new LinqToSqlRepository<T>(_context)); 

     return _repositories[typeof(T)] as IRepository<T>; 
    } 

    public RepositoryCollection Insert<T>(IRepository<T> repository) where T : class 
    { 
     _repositories[typeof(T)] = repository; 
     return this; 
    } 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    public void SubmitChanges() 
    { 
     if (_context != null) 
      _context.SubmitChanges(); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      if(_context != null) 
       _context.Dispose(); 
     } 
    } 
} 

그런 다음, 도메인 서비스, 당신과 같이 사용 :

private RepositoryCollection _repositoryCollection; 

public MyDomainService(RepositoryCollection repositoryCollection = null) 
{ 
    _repositoryCollection = repositoryCollection ?? new RepositoryCollection(new MyDataContext()); 
} 

public IQueryable<Customer> GetCustomers() 
{ 
    return _repositoryCollection.Get<Customer>().Query(); 
} 

public IQueryable<Product> GetProducts() 
{ 
    return _repositoryCollection.Get<Product>().Query(); 
} 

.. other methods go here ...