Domanda

I have a Generic Repository class like below:

enter image description here

I have many methods in this class and I would like all classes to have an extra parameter that will take Func<T, T> or sth else (but same for all) that will be for selection of columns since most of the time I will not be pulling all the columns when querying.

I would like to know how can I do this more efficiently instead of adding one more parameter in all methods or creating overloads for each method?

È stato utile?

Soluzione

Have one generic parameter for the entity and one for the projected type:

List<TResult> Get<TEntity, TResult>(Expr<Func<TEntity, TResult>> selector)

Consider just exposing a queryable:

IQueryable<T> Query<T>() { return _context.GetTable<T>(); }

This makes all of these issues go away. Your Get* methods add no value. They just restrict the possible query patterns.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top