1
\$\begingroup\$

What can be done better in this code? I am sure it's not missing much.

You can copy and paste the whole thing in LinqPad; it's all there.

public interface IRep<T>
{
    List<T> GetAll();
}

public interface IEntity
{
    int Id{get;set;}
}

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

public class Product: IEntity
{
    public int Id { get; set; }
    public string Code { get; set; }
    public decimal Price {get;set;}
}

public class RepProduct: IRep<Product>
{
    public List<Product> GetAll()
    {
        List<Product> list = new List<Product>();
        list.Add(new Product() {Id=1, Code = "burger", Price = 2.99M});
        list.Add(new Product() {Id=1, Code = "fries", Price = 1.99M});
        list.Add(new Product() {Id=1, Code = "pepsi", Price = 1.99M});
        return list;
    }
}

public class RepCustomer: IRep<Customer>
{
    public List<Customer> GetAll()
    {
        List<Customer> list = new List<Customer>();
        list.Add(new Customer() {Id=1,Name="Fred", Age=44   });
        list.Add(new Customer() {Id=2,Name="Victoria", Age=13   });
        list.Add(new Customer() {Id=3,Name="Kiefer", Age=10 });
        return list;}
}

void Main()
{

    IRep<IEntity> rep = null;



}
\$\endgroup\$
4
\$\begingroup\$

To get it to work, use IEnumerable<T> instead of List<T> as it's covariant:

public interface IRep<out T>
{
    IEnumerable<T> GetAll();
}

public interface IEntity
{
    int Id { get; set; }
}

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

public class Product: IEntity
{
    public int Id { get; set; }
    public string Code { get; set; }
    public decimal Price {get;set;}
}

public class RepProduct: IRep<Product>
{
    public IEnumerable<Product> GetAll()
    {
        List<Product> list = new List<Product>();
        list.Add(new Product() {Id=1, Code = "burger", Price = 2.99M});
        list.Add(new Product() {Id=1, Code = "fries", Price = 1.99M});
        list.Add(new Product() {Id=1, Code = "pepsi", Price = 1.99M});
        return list;
    }
}

public class RepCustomer: IRep<Customer>
{
    public IEnumerable<Customer> GetAll()
    {
        List<Customer> list = new List<Customer>();
        list.Add(new Customer() {Id=1,Name="Fred", Age=44   });
        list.Add(new Customer() {Id=2,Name="Victoria", Age=13   });
        list.Add(new Customer() {Id=3,Name="Kiefer", Age=10 });
        return list;
    }
}

void Main()
{
    IRep<IEntity> rep = null;

    rep = new RepCustomer();
    Console.WriteLine(rep.GetAll());

    rep = new RepProduct();
    Console.WriteLine(rep.GetAll());
}
\$\endgroup\$
  • \$\begingroup\$ YES!!!!!!!!!!!! i spent hours looking for the solution.. THANKS A LOT \$\endgroup\$ – SerenityNow Aug 15 '13 at 15:31
  • \$\begingroup\$ What is the OUT for in <out T> \$\endgroup\$ – SerenityNow Aug 15 '13 at 15:33
  • \$\begingroup\$ That indicates the type parameter for the interface is covariant, that is, it can accept type T and subclasses of T as well. That's the secret sauce here. \$\endgroup\$ – Jesse C. Slicer Aug 15 '13 at 15:34
  • \$\begingroup\$ You have no idea how hard i tried to make this code work.. you really helped me out.. thanks a lot again \$\endgroup\$ – SerenityNow Aug 15 '13 at 15:38
  • 1
    \$\begingroup\$ @RedSoxFred: As a more intuitive explanation of the purpose of covariance: It's illegal to pass List<Dog> to method expecting a List<Mammal> because the method might try to add a Cat to the list (as this is legal with a List<Mammal>). It's legal to pass IEnumerable<Dog> to a method expecting IEnumerable<Mammal> because there is no way to add entries to an IEnumerable. It is still possible to pull entries out, but an IEnumerable<Dog> will never contain non-mammals. Hence, you use the keyword out to say that you plan to pull stuff out (but not put stuff in). \$\endgroup\$ – Brian Aug 15 '13 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.