Reputation: 899
Our application does its best to keep the controllers smallish, our views are concerned with UI, and we have a ViewModel for each View which pulls in the model data. We use AutoMapper were possible to map the Models into the ViewModels.
In some cases we ave a good bit of code needed for building out the ViewModels which while technically can be done in AutoMapper's Map functionality it becomes huge and ugly. So we have this bit of logic which has made its way into some of our ViewModels but then those become 200+ lines in length and it just does not feel correct. For example, for one ViewModel we have about half a dozen properties which are not derived from a single model but from about 4-5 models and are dynamic in that they are calculated at runtime so its not as simple as mapping a name parameter from a model into our ViewModel.
Has anyone else run into this issue or have found a need to create some kind of ViewModel factory for those ViewModels which cannot easily be constructed from a single model?
Example: Lets say you have a page for a Course such as 'Beginning French 101'. The Course model might have a teacher, subject, pre-reqs, class number, section, etc. When a student visits the course page I might want to change how or what additional information is displayed about this class (or other classes) based upon the student's enrollment status, registration, past history, what other classes the student is registered for, etc. I have two models here, Course and Student, but I may want to pull in additional information to include within the ViewModel based upon certain business rules.
Upvotes: 3
Views: 524
Reputation: 1039508
If it's a business logic, then it should not be placed in your mapping layer. Define Dtos
and prepare your objects as much as possible in your business layer. If you have such humongous differences between your business models and your view models it means that you probably need another layer to translate your hefty business domain models into Data transfer Objects that will be more easily mapped to view models. In general you shoyuld avoid placing complex business logic in your mapping layer.
Unfortunately without a specific code sample from your part it would be difficult to expand any further.
Upvotes: 2