.net - How to pass Lambda values to a generic repository -


i have number of repositories in app, , want try , make generic repository cut down on code duplication.

one requirement comes repeatedly need list entities in drop down list.

in current concrete repository have this:

public ienumerable getselectlist() {     return _context.cultures.select(x => new selectlistitem     {         text = x.culturekey,         value = x.cultureid.tostring()     }).tolist(); } 

so have generic getselectlist function let me specify values wanted use text , value fields in select list item (in code above culturekey , cultureid).

i not sure how make values configurable in generic repository. there way evaluate string values lambda values? or there other way of achieving want?

puting aside issue of whether generic repository should know anythig selectlistitem (protip: shouldn't) can achieve passing in 2 lamda functions

public class genericrepo<t> {     public ienumerable getselectlist(func<t,string> text, func<t,string> value)     {         return _context.cultures.select(x => new selectlistitem         {             text =  text(x),             value = value(x)         }).tolist();     } } 

assuming have intance of genericrepo<culture> this

var selectlist = repo.getselectlist(x => x.culturekey, x => x.cultureid.tostring()); 

an arguably better way might decouple this, method returns ienumerable<tuple<string,string>> , map selectlistitem in ui layer.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -