asp.net - MVC + POCO + Entity Framework, Passing Object between layers -


i trying hands on mvc 2, ado.net ef , poco. have generated entity classes in separate library using poco generator.these poco entities used viewpages (not sure if that's right way design or need separate viewmodels classes ?)

now, if take case of simple scenario need add employee object( related department master), should recommended way transfer these objects between layers.

layered structure of application :

enter image description here

i have thought of various alternatives:

i have method in employee controller named addemployee() accepts formcollection parameter. within form collection posted data such employee name, age , salary etc , id of selected department .

1.) 1 way can create dto employeedepartment dto used map values formcollection is. can break them @ manager layer , use them create entity objects i.e employee object , refer department query similar this:

e.department =  department.where(i => i.deptid == empdepdto.dept_id).first() 

i not big fan of , feel every time there relation involved have add dto , map entity class.

2.) second worst, i.e passing each object parameter , couple them in manager layer.

3.) use poco is, create employee object , deparment object @ controller layer , pass poco object

public void addemployee(formcollection formcollection) {     department d = new deparmtent; d.id = ""; //based on dropdown value     d.name="" //based on dropdown selected text;      employee e = new employee; e.name. e. sal....      e.department = d;      employeemanager.addemployee(e); } 

but @ manager layer think , still need recreate reference department using linq again repetitive , doesn't seems clean solution.

are there better ways of handling ? looking recommendations , best practices.

firstly, there reason you're not using mvc version 3? there's no major breaking changes, may upgrade?

secondly there reason using formcollection rather typed model-binding? change views use typed html helpers ( <%: html.textboxfor(m => m.property) %>), or make sure name attributes match property names, , have controller receive type, , model binding rest. there's plenty of tutorials showing this, , articles explaining it. model binding work name/value collection, posted form, or against json data, or can find/write custom model binders work against whatever wacky serialisation protocol want.

one thing watch though when passing actual entity types entity framework store around, have careful when updating existing objects, or foreign key references existing objects - objects must attached right entity framework context. achieve see objects received controller having properties copied freshly retrieved entity context, either manually or object mapper of kind.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -