c# - Repository as parameter -
i created generic dropdown list use in controller:
genericdropdownlist("mydropdown"); private void genericdropdownlist(string dropdownname, object selecteddepartment = null) { var dropdownquery = unitofwork.salesrepository.get(orderby: q => q.orderby(d => d.firstname)); viewdata[dropdownname] = new selectlist(dropdownquery, "lastname", "lastname", selecteddepartment); }
this seems work fine. i'm trying make unitofwork.testrepository
dynamic, can use every available repository in function:
genericdropdownlist("mydropdown", salesrepository); private void genericdropdownlist(string dropdownname, object repository, object selecteddepartment = null) { var dropdownquery = repository.get(orderby: q => q.orderby(d => d.firstname)); viewdata[dropdownname] = new selectlist(dropdownquery, "lastname", "lastname", selecteddepartment); }
the above doesn't work. following error:
error cs1061 'object' not contain definition 'get' , no extension method 'get' accepting first argument of type 'object' found
is possible make dropdown dynamic want?
the class object
not have .get
method makes sense not compile.
using dynamic
fix because .get
resolved @ runtime, albeit @ performance cost, , risk of runtime error if .get
not exist @ runtime.
the best approach in opinion use interface:
private void genericdropdownlist(string dropdownname, irepository repository, object selecteddepartment = null) { // ... }
when using entity framework can impose interface on repository classes using partial classes:
public partial class thisrepository : irepository { } public partial class thatrepository : irepository { }
a catch interface can not define property such firstname
, either have define method that, or use func<irepository, string>
sorting bit.
Comments
Post a Comment