2014-04-18 3 views
1

과 협력의 단위는 다음 코드는저장소 패턴, 제네릭

public interface ICustomer 
{ 

    int Age { get; set; } 

    string Name { get; set; } 
} 

public class Customer : ICustomer 
{ 

    public int Age { get; set; } 


    public string Name { get; set; } 
} 

public interface ICustomerRepository<T> where T : class 
{ 
    IEnumerable<T> GetCustomers(); 

    T GetCustomer(); 

    void AddCustomer(T customer); 
} 

public class CustomerRepository<T> : ICustomerRepository<T> where T:class 
{ 
    public IEnumerable<T> GetCustomers() 
    { 
     return new List<T>(); 
    } 


    public T GetCustomer() 
    { 
     return null; 
    } 


    public void AddCustomer(T customer) 
    { 

    } 
} 


public class UnitOfWork //: IUnitOfWork 
{ 

    public ICustomerRepository<ICustomer> CusRepo 
    { 
     get 
     { 
      return new CustomerRepository<Customer>(); 
      //Error: Error 1 Cannot implicitly convert type 'ConsoleApplication1.CustomerRepository<ConsoleApplication1.Customer>' to 'ConsoleApplication1.ICustomerRepository<ConsoleApplication1.ICustomer>'. An explicit conversion exists (are you missing a cast?)  
     } 

    } 
} 

나는 UnitOfWork에 클래스에 다음과 같은 오류를 (나는 그것을 간단하게 특정 영역을 ommited 한)했다. 이 문제를 어떻게 해결할 수 있습니까?

. 오류 1 암시 적으로 'ConsoleApplication1.CustomerRepository'유형을 'ConsoleApplication1.ICustomerRepository'로 변환 할 수 없습니다. 명시 적 변환이 존재합니다 (캐스트가 누락 되었습니까?)

매개 변수가 내부 및 외부 목적으로 모두 사용되므로 공분산을 사용할 수 없습니다.

답변

2

당신은 다음과 같은 저장소를 반환해야합니다 : 당신의 ICustomerRepository<T>가 및 위치 안팎의 일반적인 매개 변수를 가지고 있기 때문에

get 
{ 
    return new CustomerRepository<ICustomer>(); 
} 

현재 분산을 사용할 수 없습니다. 분산을 사용하려면 다음과 같은 두 개의 인터페이스로 ICustomerRepository<T>을 분할 할 수 있습니다 :

interface IReadRepository<out T> 
{ 
    IEnumerable<T> GetItems(); 
    T GetItem(int id); 
} 

interface IWriteRepository<in T> 
{ 
    void AddItem(T item); 
} 
0

나는 샘플 코드의 패턴을 리팩토링했다.

public class Customer 
{ 
    public int Age { get; set; } 
    public string Name { get; set; } 
} 

public interface IUnitOfWork 
{ 
    int Save(); 
} 

public interface IBaseRepository<TEntity, TKey> : IUnitOfWork where TEntity : class 
{ 
    void Add(TEntity entity); 
    TEntity Find(TKey id); 
    IEnumerable<TEntity> GetAll(); 
} 



public interface ICustomerRepository :IBaseRepository<Customer, int> 
{ 

} 

public class CustomerRepository : ICustomerRepository 
{ 
    public void Add(Customer entity) 
    { 
     throw new System.NotImplementedException(); 
    } 

    public Customer Find(int id) 
    { 
     throw new System.NotImplementedException(); 
    } 

    public IEnumerable<Customer> GetAll() 
    { 
     throw new System.NotImplementedException(); 
    } 

    public int Save() 
    { 
     throw new System.NotImplementedException(); 
    } 
} 


public class UnitOfWork : IUnitOfWork 
{ 
    private ICustomerRepository _customers; 

    public ICustomerRepository Customers 
    { 
     get { return _customers; } 
    } 

    public UnitOfWork() 
    { 
     _customers = new CustomerRepository(); 
    } 

    public int Save() 
    { 
     throw new System.NotImplementedException(); 
    } 
} 
+0

응답을 주셔서 감사합니다, 그것은 어느 정도 도움이됩니다. 나는 당신의 솔루션에 작은 문제가 있습니다. 내 인터페이스는 별도의 프로젝트에 있고 구체적 구현은 별도의 프로젝트에 있습니다. 그래서 나는 다음 공용 인터페이스를 수행 할 수 없다 (고객은 다른 프로젝트에있다). ICustomerRepository : IBaseRepository user3547774