Tipo de retorno de métodos genéricos

I'm Sorry again but this question continuance of my first question: consider this Interface:

 interface IRepository<T,U> where T:class where U:class 
{
    IEnumerable<U> SelectAll();

    bool Insert(T item);

    IEnumerable<T> FindAll(Func<T, bool> exp);
}

and I Implement this interface:

 public class Repository : IRepository<Customer,Select4ColumnsOfCustomers>
{

    #region IRepository<Customer,Select4ColumnsOfCustomers> Members

    public IEnumerable<Select4ColumnsOfCustomers> SelectAll()
    {
        throw new NotImplementedException();
    }

    public bool Insert(Customer item)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<Customer> FindAll(Func<Customer, bool> exp)
    {
        throw new NotImplementedException();
    }

    #endregion
}

public class Select4ColumnsOfCustomers
{
    public int CustomerID { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Phone { get; set; }
}

I want to return just 4 column of Customer table in northwind database. Ok.this work but if I want to add other method that return other types I must declare S,U,M,W,... in Interface and in Implement of it I must write this Code:

public class Repository : IRepository<Customer,RetType1,RetType2,RetType3,....>

this not good.what is alternative for this.Is it possible that write var for Return Types?Or a place holder for return types? thanks

preguntado el 08 de enero de 11 a las 18:01

Considera usar Datatable or an ORM for this kind of thing. -

1 Respuestas

Your repository can implement multiple interfaces.

public class Repository : IRepository<TResult1, T1>, IRepository<Tresult2, T2>, etc...

Your SelectAll method will need to be generic like

TResult SelectAll<TResult>();

However Andrzej is right, consider using an ORM so that you can generalize this code. Check out NHiberante y Marco de la entidad.

Respondido el 08 de enero de 11 a las 22:01

@Nima, sorry I actually don't have much experience with EF. In NHibernate, what I've typically done is create repository class that deals with CRUD operations and the a set of Query classes for specific querying. - Vadim

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.