views:

64

answers:

2

I'm looking for a simple way to link a view to a viewmodel and then to Entity Framework through databinding.

Can someout out there give me some pointers. What I'm looking for is a very simple implementation that would allow the view to automatically list all the contents of for example a one field table and for changes in the view to be propogated back to the database through EF.

AAfter spending a long time looking I'm still searching for a way to do this and any help would be very much appreciated.

Thanks,

A: 

Technically using a ViewModel means you don't actually bind to your entities. Your ViewModel classes should have everything on them that is needed by the view (and yes, this can cause what feels like duplication, but it's for the greater good), and therefore you don't even have this problem.

You can ease the pain of duplication by implementing something like AutoMapper to avoid the legwork of "left to right" coding where you're just copying properties across.

If you are binding a list, consider having an EmployeeListViewModel that has a property of type IList<EmployeeViewModel> on it, so that you're still not binding your entities directly to the view. This is useful because you can then reuse that EmployeeViewModel for a single-record detail view.

Neil Barnwell
Don't use AutoMapper for projecting onto a view model; it's wildly inefficient in comparison to LINQ, as it doesn't (last I checked) support `IQueryable<T>`. However, AutoMapper works fine for going the other way -- updating from an edit model back to your data layer.
Craig Stuntz
A: 

I have a full example here.

Craig Stuntz