c# - MVC Repository - Domain Model vs Entity Model -
i have created repository returning data database using entity framework , need provide data view, before need convert objects domain model.
my schema looks this:
table project id int primary key name nvarchar(100) table resource id int primary key firstname nvarchar(100) lastname nvarchar(100) table projectresources project_id int primary key -- links project table resource_id int primary key -- links resource table
i generated entity model ended looking this:
project | ---->projectresources | ---->resource
i have repository returns project:
public interface iprojectrepository { project getproject(int id); }
and controller action:
public actionresult edit(int id) { project project = projectrepository.getproject(id); return view(project); }
this doesn't seem work when try , post data. getting entitycollection initialized error when trying reconstruct projectresources collection.
i think smarter create domain model little simpler:
public class projectedit { public string projectname { get; set; } public list<projectresource> resources { get; set; } } public class projectresource { public string firstname { get; set; } public string lastname { get; set; } }
this seems little nicer since don't have intermediate projectresources -> resource jump. projectresource have fields need. instead of doing like:
@foreach( var resource in model.projectresources ) { @html.displayfor(m => m.resource.firstname) }
i can do:
@foreach( var resoure in model.resources ) { @html.displayfor(m => resource.firstname); }
my question follows should returning domain model repository or should handled controller or other class in middle? if it's handled in controller maps project projectedit, like?
my own view shouldn't return controller or view dependant on implementation of repository.
if you're using ef poco generator, it's reasonable use classes domain model because they're independent of ef implementation (you replace ef , retain poco's).
but if you're using ef entityobjects, believe should convert domain model. if data access encapsulated in wcf service used repository pattern internally, wouldn't worry returning entityobjects repository. if you're using repository directly mvc, use domain model interface repository.
Comments
Post a Comment