locked
Repository Pattern, Unit of Work with Generics RRS feed

  • Question

  • User-1939321229 posted

    I have the following code (I have ommited certain areas to make it simple)

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

    I get the following error in the UnitOfWork class.  How do I fix this?


    .Error 1 Cannot implicitly convert type 'ConsoleApplication1.CustomerRepository<ConsoleApplication1.Customer>' to 'ConsoleApplication1.ICustomerRepository<ConsoleApplication1.ICustomer>'. An explicit conversion exists (are you missing a cast?) 

    I cannot use covariance since the <T> parameter is used for both in and out purposes.

     

     

     

    Friday, April 18, 2014 1:33 AM

All replies