design patterns - Solid approach to loading reference data into view models in ASP.NET MVC -


i want way separate loading of reference data view model controller. @ moment have view model property selected value , reference data:

public ienumerable<selectlistitem> daytypes { get; set; } public int daytypeid { get; set; } 

and data populated relevant repository in controller action:

model.daytypes = _daytyperepository.getall().toselectlist(d => d.description, d => d.identifier.tostring()); 

i change because pollutes controller lots of repositories , code not core concerns. of these dependencies make unit testing controller pain.

one possible approach solving make view model class loading require custom model binder instantiate them using ioc container provide repository dependency. option?

another approach think hinted @ in codecampserver incomplete , commented out involving attributes on field in view model:

[selectlistprovided(typeof(alldaysselectlistprovider))] public ienumerable<selectlistitem> daytypes { get; set; } 

however struggling figure out how implemented in way not require major replumbing of mvc framework.

how solve problem?

edit: want keep typed views , avoid stuffing data view data.

further edit: solution ideally model independent, mean if same reference data needed multiple view models can achieved single piece of code. matt's approach interesting tightly coupled view model.

i use service layer return me poco object map view model. controller action this:

public actionresult index(int id) {     var model = _service.getmodel(id);     var viewmodel = mapper.map<model, viewmodel>(model);     return view(); } 

i using action filters avoid mapping code on again so:

[automap(typeof(model), typeof(viewmodel))] public actionresult index(int id) {     var model = _service.getmodel(id);     return view(model); } 

this way service talks crud repositories , controller talks service , mapping layer.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

android - layout with fragment and framelayout replaced by another fragment and framelayout -