Map one entity to one viewmodel or to many viewmodels - asp.net-mvc

I have for one customer entity multiple viewmodels depending on the existing views like Create,Update,Get,Delete. These viewmodels share the same properties up to 75% with the entity.
Should I better merge all the customer viewmodels to one big viewmodel?
So I have to map only and always from one entity to one viewmodel and the way back?
Do you see any disadvantage in flexibility for certain scenarios I have not in my mind now?

In the long run, keeping them separate will be better because while the data contained in each ViewModel may be similar or even identical, the intention is different. For example, the Create and Update ViewModels are certainly similar, but have a few important differences. First, the Create view model usually doesn't have the identity of the entity and having it there may be confusing since it doesn't make sense. Second, if the application supports partial updates, the update ViewModel may be a collection of changes to an existing entity, not the entity as a whole.
If you are striving for DRY you can achieve re-usability by means other than sharing the entire ViewModel class. Instead, you can create smaller re-usable components and re-use by composition instead of inheritance. Attempting to coerce a single ViewModel class to fulfill all requirements will be buggy beacause the code is more difficult to reason about. Many times, simple copy & paste gets the job done better than what OOP offers.

On one hand, splitting up the ViewModels as you describe makes your code base really clear, as you can make sure each ViewModel is exactly fit for purpose and has no unnecessary properties. On the other hand, it means that you have more code to maintain - a change to your entity may well mean changes to several ViewModels.
On the other hand, the one big ViewModel approach has basically exactly the opposite pros and cons - less code to maintain, but the ViewModels are less fit for purpose.
There isn't really a right or wrong answer here, you've got too weigh up the pros and cons of each approach and decide what will work best for you.
One sort of halfway approach is to have a single ViewModel for Create/Update, one for Retrieve and one for Delete, as the Create/Update are likely to be very similar.

Another, most OO option for you lies in good old inheritance: define common functionality between actions in one class MyVM and extend it (inherit from it) as you see fit for different actions: MyVMEdit, MyVMDelete, MyVMCreate, `MyVMList'.
This is the best of both worlds: you only maintain things once, and you extend them to fit precisely to every view.
There is no right approach here, since its not a math, any approach you take that gets job done will get the job done :) But ... sometimes we carried away from our roots too far :) its pure good old Object Oriented approach.
If inheritance (or extension) poses any issues (for any reason), you can embed MyVM portion inside every MyVM<Action> model and achieve the same level of abstraction / functionality balance.
As usual - right tool for the right job.
Hope this will help you.

Related

MV4 Application with EF5 model first, without ViewModels or Repositories

I'm building a MVC4 app, I've used EF5 model first, and kept it pretty simple. This isn't going to a huge application, there will only ever be 4 or 5 people on it at once and all users will be authenticated before being able to access any part of the application, it's very simply a place order - dispatcher sees order - dispatcher compeletes order sort of application.
Basically my question is do I need to be worrying about repositories and ViewModels if the size and scope of my application is so small. Any view that is strongly typed to a domain entity is using all of the properties within that entity. I'm using TryOrUpdateModel in my controllers and have read some things saying this can cause a lot of problems, but not a lot of information on exactly what those problems can be. I don't want to use an incredibly complicated pattern for a very simple app.
Hopefully I've given enough detail, if anyone wants to see my code just ask, I'm really at a roadblock here though, and could really use some advice from the community. Thanks so much!
ViewModels: Yes
I only see bad points when passing an EF Entities directly to a view:
You need to do manual whitelisting or blacklisting to prevent over-posting and mass assignment
It becomes very easy to accidentally lazy load extra data from your view, resulting in select N+1 problems
In my personal opinion, a model should closely resembly the information displayed on the view and in most cases (except for basic CRUD stuff), a view contains information from more than one Entity
Repositories: No
The Entity Framework DbContext already is an implementation of the Repository and Unit of Work patterns. If you want everything to be testable, just test against a separate database. If you want to make things loosely coupled, there are ways to do that with EF without using repositories too. To be honest, I really don't understand the popularity of custom repositories.
In my experience, the requirements on a software solution tend to evolve over time well beyond the initial requirement set.
By following architectural best practices now, you will be much better able to accommodate changes to the solution over its entire lifetime.
The Respository pattern and ViewModels are both powerful, and not very difficult or time consuming to implement. I would suggest using them even for small projects.
Yes, you still want to use a repository and view models. Both of these tools allow you to place code in one place instead of all over the place and will save you time. More than likely, it will save you copy paste errors too.
Moreover, having these tools in place will allow you to make expansions to the system easier in the future, instead of having to pour through all of the code which will have poor readability.
Separating your concerns will lead to less code overall, a more efficient system, and smaller controllers / code sections. View models and a repository are not heavily intrusive to implement. It is not like you are going to implement a controller factory or dependency injection.

Controller A calling Model B: Code smell?

I have a toys controller which users can use to claim toys to play with. Right now, the claim method is implemented in the controller level (as this answer suggested I do).
However, now it's getting a bit fat with claiming logic that really shouldn't be there: A child can't claim a toy if they already have 3 toys, a child can't claim a toy claimed by another child, and so on. The sensible spot for that logic (in my mind) is in the child model, because I'm describing the behavior of a child (what they may and may not do).
That said, if I do this, the toys#claim controller action is going to call methods from the child model. Is this a code smell/bad practice?
(I'm guessing someone's going to suggest I use a service object for this. If you do, could you please point out a simple tutorial? The recent RailsCast about this is a bit too complicated for me.)
Thanks in advance!
In general (outside of Rails), it is not a smell at all. In fact, I'd argue that having a pure 1:1 mapping between "models" and "controllers" is a smell.
Note: I am not a ROR dev. I have no experience in ROR or how it implements things. However, I do understand design patterns quite well, and understand application architecture. With that said:
Instead of worrying about 1:1 mappings, step back and think about the structure of the application.
What is the Controller supposed to be doing? Well, in general it is supposed to route user actions to the application. It is just a plumbing step.
Then what is a Model (layer) supposed to be doing? In general, the Model is a layer that encompasses all of your business logic in the application. It will handle database interaction, access controls, business operations, etc. Therefore, the model is actually the vast majority of your application.
The View on the other hand is your presentational layer. It should handle all rendering, pulling data from the model layer.
Based on that understanding, your models, views and controllers should be able to vary independently to each other. In general, I'd expect to see a fairly 1:1 relationship between controllers and views. What I mean by that is that each controller that exists, I'd expect to see a view. But there can be views that exist where there's no user interaction. In those cases, you may need a controller (to render the view), or depending on your architecture, you many not need one.
But the "model classes", which are a small part of the model layer (acting as proxies or adapters for the lower model functionality) may or may not be 1:1 with controllers or views. For example, you may have a view that pulls data from multiple models. You can have a controller which acts on multiple models.
Now you could step back and say that if a controller needs to act upon multiple models, then create a new model which abstracts that operation. Sometimes that is the right thing to do. Sometimes it's not. It all boils down to the specific operations and relationships involved...
At the end of the day, there's no "right" or "wrong" here. It really comes to a design decision that you need to make as you structure your application. I wouldn't worry too much about the "smell" component, as long as it makes sense in your application...

Model-Controller Abstraction with Core Data

In learning about Core Data, I've noticed how (in Xcode's templates) Apple directly used the query classes inside the view controller. This seems like it is bad MVC (having database access logic directly inside the view controller). Would it make sense to abstract out these kinds of actions to a separate suite of classes that obtain the data from the database and pass it back to the view controller calling it?
EDIT–
So, just to be clear, when I say "kinds of actions", I specifically mean CRUD Operations. Though if you have ideas about other things that a so-called "Model-Controller" would do, I'd be interested in hearing about them.
It's a matter of opinion, and often yes the templates are the most simple form of working example. It's hard to have a template spin out multiple files, for example.
Yes, personally, I generally spin out a separate NSManagedObject subclass. I like to have a _MySubclass object that has all the auto-generated stuff, then have the model actually reference MySubclass which has model-based business logic (you can use mogenerator or other methods to do this too if so inclined). Perhaps thinking of it as "Model-Controllers" and "View-Controllers" is another way of putting it.
This is a very good question and the answer likely depends on your situation. Perhaps architecture purists would insist on separate Model controllers and there are a lot of benefits to this approach. However, sometimes I find myself using Key Values when I'm doing a simple view. When things are more complex, for example, when coding the same Model for the Mac and iOS, having separate Model Controllers will allow you to reuse a lot of code. When you must diverge, Obj C Categories are a very clean way to extend functionality without adding a lot of overhead. I personally favor categories over extensive subclassing.
Since NSFetchedResultsController was released, my model classes are leaner. There are a lot of nuances to this and experience will help you come up with the best solution for your App. I've also found that writing unit tests up front will help you force through issues and validate your design, or send you back to the drawing board :)

Why does Apple documentation that getting ManagedObjectContext from UIApplicationDelegate is bad?

Just curious why ManagedObjectContexts should be passed to UIViewControllers when they are created, rather than just grabbing them from a UIApplicationDelegate?
The docs say that this makes your applications more rigid, but I am failing to see the nuances of when to use which pattern.
Thanks!
Imagine that I ask you to do some task, like painting a room. If I just tell you "go paint a room," you'll need to ask me a lot of questions, like:
Which room?
Where's the paint?
Where are the brushes?
Should I use a dropcloth?
In short, you won't be able to complete the task without help from me. If you have to depend on me every time, you won't be a very flexible painter. One way to deal with that problem is for me to give you all the stuff you need at the outset. Instead of "go paint a room," I'll say "please paint room number 348 using this bucket of paint and this brush, and don't bother with a dropcloth." Now, you've got everything you need, and you can get right to work with no further help from me. You're a much more flexible worker because you no longer depend on me.
The same thing applies to view controllers (and objects generally); it's better to give them everything they need than to have them depend on a particular object like the app delegate. It's true not just for managed object contexts, but for any information they need to do their job.
This is mainly because you want to use dependency injection with your UIViewControllers instead of just grabbing everything from UIApplication, this keeps your delegate clean instead of full of reference hacks.
This is also to keep with the MVC pattern:
Model
View Controller (Only for view logic)
Controller (For coordinating between the view and the model)
I tend not to agree with this pattern.
First of all I try to treat Core Data as an implementation detail, and as any implementation detail it should be hidden behind a good facade. The facade is the interfaces I expose for my model objects. For example if I have two model objects; Cource and Student, any cource can have a number of students. I do not want to let the controller take upon the duty to setup predicates and sort descriptors, and jump through all Core Data hoops just to get a list of students for a particular class. There is a perfectly valid way to expose this in the model:
#interface Cource (StudentAccess)
-(NSArray*)studentsStortedByName;
#end
Then implement the ugly stuff once and for all in the Model class. Hiding all the complex details of Core Data, and no need to pass around managed object contexts. But how would I find the sources, it has to start somewhere right? Yes, it does but you need not expose it to the controller. Adding methods such as these are perfectly reasonable as well:
#interface Cource (CourceAccess)
+(Cource*)caurceByID:(NSString*)courceID;
+(NSArray*)allCources;
+(NSArray*)courcesHeldByTeacher:(Teacher*)teacher;
#end
This also helps in minimizing dependencies between controllers. And reducing he dependencies between the model and controller. Assuming I have a CourceViewController and a StudenViewController is I did not hide the Core Data details behind a facade and wanted to pass around the managed object context as well, then I would end up with a designated initializer like this:
-(id)initWithManagedObjectContext:(NSManagedObjectContext*)moc
student:(Student*)student;
Whereas with good a good facade I end up with this:
-(id)initWithStudent:(Student*)student;
Minimizing dependencies behind facades, in favor of dependency injection also makes it much easier to change the internal implementations. Passing around the managed object context encourages each controller to implement their own logic for basic stuff. Take for example studentsSortedByName method. At first it might be sorter by last/first name, if later changed to last/first name sort you would have to go to each and every controller that has sorted students and make the change. Where a good facade method requires you to change in one method, and all controller automagically get the update for free.
The Apple Docs try to foster the most widely applicable and sustainable design patterns.
Dependency injection is preferred because it allows for the most flexible, expandable, reusable and maintainable design.
As apps grow in complexity, using a quasi-singleton like parking the context in the app delegate breaks down. In more complex apps, you may have multiple context tied to multiple stores. You might want the same view-controller/view pair to display data from different context at different times or you may end up with multiple context on different threads/operations. You can't pile all those context up in the app delegate.
If you have a simple app with a single context then using the quasi-singleton with the app delegate can work well. I've used it on several smaller apps in the past without immediate issue but I did hit scalability problems on a couple of apps when the apps grew overtime.
Which pattern to use depends on your shipping constraints and you best guesses about of the evolution app over its entire lifecycle. If its a small one shot app, then the app delegate quasi-singleton will work fine. If the app is more complex, might grow more complex or might spawn other related apps that will reuse existing components, then dependency injection is the way to go.

DRY vs Security and Maintainability with MVC and View Models

I like to strive for DRY, and obviously it's not always possible. However, I have to scratch my head over a concept that seems pretty common in MVC, that of the "View Model".
The View Model is designed to only pass the minimum amount of information to the view, for both security, maintainability, and testing concerns. I get that. It makes sense.
However, from a DRY perspective, a View Model is simply duplicating data you already have. The View Model may be temporary, and used only as a DTO, but you're basically maintaing two different versions of the same model which seems to violate the DRY principal.
Do View Models violate DRY? Are they a necessary evil? Do they do more good than bad?
This has been brought up time and time again. Not only is it a pretty substantial dupe but the answer is subjective and argumentative. ViewModels are a response to DDD and the concept of persistence ignorance.
To say not using ViewModels is bad means ignoring that Django and Rails and most PHP ORM/MVC frameworks don't care at all about those concepts. Do you want somebody to tell you all those other languages and frameworks are "doing it wrong?".
Whether or not you want to use ViewModels is 100% dependent on what architecture styles you are going for and what the goals of the application are.
This is like asking is dragging and dropping GridViews in a WebForm app appropriate? Depends on a lot of things.
There is also a misconception about DRY that you have here. Do Proxy classes from a WCF service violate DRY? Does the ViewModel contain logic? The primary goal of DRY is to not have duplicated logic with a meaningful purpose. Do a couple of DTOs that share object shapres violate that?
The DDD principal of bounded contexts would make for a good read too. If a ShoppingCart object needs to function differently in a warehouse vs ecommerce website setting does that mean you to share the types? What happens when the only shared functionality is totaling a price ( price + tax + shipping )? Do you create a base class just for that therefore increasing coupling? What are the tradeoffs in time/cost/maintenance for being 100% DRY for a simple method like GetTotal(). Does violating DRY when it makes sense actually decreasing the complexity and overall cost of maintaining your codebase?
I'm sorry for answering with so many questions but hopefully now you can see the nuances and intricacies of the question you asked. ;)
One could also note that not using view models would be a violation of the single responsibility principle -- your entity should not be polluted with UI concerns.
I also think the real value of view models doesn't necessarily become apparent in version 1.0 of your application. You will thank yourself when working on version 2.0 when you completely re-think how your back-end works but you don't have to carry those changes out to the view layer.

Resources