1

I am using EF code first in one of my mvc 3 projects. I have a question about what patterns to use when passing a complex EF POCO object to and from the views.

For example, a customer object has a list of orders and each order has a list of items. The customer object will be sent to the view. The view updates the customer object and its inside objects (orders, items), then send it back the controller. The controller have EF to persist the customer object.

My questions are following:

  1. Should I serialize the EF poco object to a JSON object so I can use it inside the view?

  2. How can I re-construct the customer object when I recieve updates from view?

  3. After the customer object is reconstructed, Is it possible to save the entire object graph (customer, orders, items) in one shot?

Thanks

1
  • 2
    Never use EF objects into views. It will be the start of you downfall. You will start doing hacks and hacks and it will get messier and messier until you hit the ground and scrap everything to start over. I've been there, I've done that. Just trying to save you hours of your life and make you start in the right direction. Depending on your application size, you should consider reading about DDD and implementing some Repository / UnitOfWork patern, but for a small app, just making sure a EF POCOs / IQueryiables never reaches your views (or even better, your controllers). Commented Oct 22, 2012 at 18:58

2 Answers 2

3

I tend to stay away from using the EF POCO objects as the model for my views. I generally will create View Models from one or more POCO objects as what I need in a view never matches exactly a single EF POCO object. The view models will then create the EF objects that are then saved to the DB.

Sign up to request clarification or add additional context in comments.

2 Comments

Since it is a complex object. How do you save the object in one shot? or Do you have to loop through the object and save its child objects one by one?
The viewmodels will be pretty similar to the poco objects in terms of properties. you will need to create the ef entities from the data contained in the view model. use something like auto mapper or just do it manually. It's just a matter of newing up some entities and setting their properties.
1
  1. Should I serialize the EF poco object to a JSON object so I can use it inside the view? No.
  2. How can I re-construct the customer object when I recieve updates from view? Don't. Let the default modelbinder materialize the POSTed data into a viewmodel (or editmodel), and use that data to issue commands to a lower layer.
  3. After the customer object is reconstructed, Is it possible to save the entire object graph (customer, orders, items) in one shot? It is, but you shouldn't. Instead, deal with each update individually based on your use cases.

Follow mojo722 and Pluc's advice here. Don't use EF POCO entities in your MVC layer. Use viewmodels. Here's how it would work:

  • Controller needs data, it asks a lower layer. The lower layer gets the data and returns entities (or better yet, entity views).
  • Controller converts entities to viewmodels (AutoMapper is good for this, but you can map manually as well).
  • Controller passes viewmodels to view.
  • View sends HTTP POST data from HTML form.
  • Default model binder converts HTTP POSTed form data to a viewmodel.
  • Controller receives viewmodel data, issues a command to the lower layer.
  • Lower layer uses EF to save new entity state.

2 Comments

Thanks. I just want to see how others do it. so are the following steps correct? 1. Convert EF object to view model. 2. After update, loop through the view model and save each object(customer, orders, items) and save it to EF one by one?
As to step #2, it depends on how granular you want to be. I would avoid doing an all-in-one update, instead separating them out into discrete use cases. What does the user want to do? Change the item quantity for a particular order? That's one operation, and IMO should be encapsulated as such. If you want to do an all-in-one update, you might actually be better off with webforms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.