Reputation: 15817
I am building a ASP.NET MVC application where I use the following flow :
Get Entity
Update Entity
Problem 1
The ModelView classes is special made for the views that the controler actions is connected to (it could contain both edeting objects and lists). It´s common that several actions with diffrent views uses the same ModelView Class, that means that not all objects in the ModelView object will be used in every action/view.
As long as the View uses all the properties of ModelViewObjects that can be updated there is no problem but...
Say that we got a View where some of the properties of the ModelViewObject is not used, this ModelViewObject is sent to the factory(to be updated) where the corsponding ModelObject is fetched(from db/entity framework) and then merged with the ModelViewObject with AutoMapper. The problem here is that properties on the ModelViewObject that is not set(not used in the view) will result in a overwrite of real data in the ModelObject.
To solve this u usually use AutoMapper ForMember Ignor(), but this will be a problem when a complete ModelViewObject(all properties set) will update db.
How do you handle this? Do you have diffrent Update methods in the factorys where diffrent AutoMapper setting?
It would be nice If I could only have a method like this : UpdateMyEnityt(MyEntity entity) and this methid will update och add MyEntity object.
Problem 2
Where should I place the AutoMapper mappings? So far I have placed Updates in the factory and Get in the controller. I have thought of placeing the mappings in the ModelViewObject for example ModelViewObject.ToDataModel but if I do not need a complete translate(som properties is to be ignored) then I will have to do this AutoMapper else where.
How do you handle this?
Problem 3
Say that you have an working update method in our factory class where you use AutoMapper with som ignors to translate a ModelViewObject to a ModelView(entity framework object). Say now that we updates the database table with a new field/propertie, if we run a view that handles the ModelViewObject corsponding to this tabel but do not handle the new propertie this will mean that the propertie will always be set to null/string.empty/0. When running the regular update method in the factory there will not be a ignor on this propertie and this means that the zero value will overrite the real value.
Its a big risk that these kind of updates will be done and its a big risk that I will not remember to handle this in old code.
How do I handle this?
Problem 4 AutoMapper have a validation method where you can check if the mappings will be possible, right now I am having these validations where the mapping is done, should I maby put this in some other method where the validation will be executed as fast as the application starts? Else problems in the mappings will show first when using the function.
BestRegards
Upvotes: 1
Views: 1322
Reputation: 9973
At first you should use viewModel for each view. And fetch only date that you need. You should add facility to manual mapping for "difficult" issue. I thing that AutoMapper is wrong way for transfer date from Db object to View Object.
Upvotes: 4
Reputation: 3961
Problem 1: If you are using a different View, use a different ViewModel. This way only the properties that are displayed will be automapped.
Problem 2: Different Automapper Settings in each project with
Project1.AutomapperSettings.Execute();
Project2.AutomapperSettings.Execute();
etc
in the Global.asax or similar entry point.
Problem 3: See problem 1
Problem 4: Use ValidationAttribute on viewmodel properties and let mvc take care of it
Upvotes: 1