1

I have built an ASP.NET MVC web application that uses Entity Framework. To provide the view with correct data I have created special view classes that represent parts of the model classes.

I have 2 questions about this.

  1. How do I best convert data from the view object to the model object? For now I am doing this manually in methods like ViewClass.ToModel() and ViewClass.FromModel(ModelClass). However if the model is updated then this conversion may have to be updated. Do I really have to do this manually?

  2. When converting a viewobject to a modelobject it can happen that some of the properties is not set so the value will be null or string.empty. The problem with this is that when running refresh(in entityframework) this empty(not set) properties will be overwriting the real data. How should I handle this?

One solution may be to get the modelobject from database and then only transfare the peroperties that have been set to the modelobject? Problem with this might be that a value on a property could be set to null, how do I know if this propertie is set or not? Or do I have to write special update methods depending on the action?

BestRegards

1 Answer 1

5

How do I best convert data from the view object to the model object?

AutoMapper

When converting a viewobject to a modelobject it can happen that some of the properties is not set so the value will be null or string.empty. The problem with this is that when running refresh(in entityframework) this empty(not set) properties will be overwriting the real data. How should I handle this?

If your data access layer (no matter what technology it is using, Entity Framework, NHibernate, ...) is not capable of handling this scenario you might need to have specially tailored update methods that work for those cases.

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

5 Comments

I will look in to the AutoMapper. So if I have created an object in a View and needs to update the database I should first get the object from databasen, update this db object with data from the object in view and then save? There is no magic for this in Entity-Framework?
I notice that there is a UpdateModel for MVC, why should I use AutoMapper instead of the UpdateModel in MVC?
@SnowJim, because UpdateModel uses the request parameters to populate your model. You could use AutoMapper to convert between your models and view models. Those ate two different types. Also instead of UpdateModel, I would recommend you using action parameters (it's basically the same, you just don't need to call this methid manually in each controller action, this task will be performed by the default model binder). Normally your repository would work with model classes. On the other hand views work with view models.
Thanks but Im not sure what you mean. My Registration action looks like this : public ActionResult Register(int categoryId, int? filterId), this is the action that will get the entity to show. Then my other Registration action looks like this : public ActionResult Register(ModelViewRegisterFilter filter). The default binder will translate from the form to my ViewModel and then I need the ViewModel to translate to my Model(this is where AutoMapper comes in). Is this the right way or am I missing somthing?
@SnowJim, this is exactly what I meant. You summarized it pretty well. Sorry for my bad English. It works also the other around: you query the repository and you get a model, then you use AutoMapper to convert this model to a view model and finally you pass the view model to the view.

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.