Business Logics of projects under MVVM in WPF?
I am developing an application in WPF using the MVVM pattern. If I have Student as a data model, for example, where could I add the business logic like adding new Students, deleting Students and updating Student info, calculating marks and so on?
Does all the business logics come under the model or the viewmodel?
If it is in the model how can we do that? Any example code plea开发者_开发技巧se? I need full code for an example!
Business logic should be part of of your model, as all of that code should be reusable across different technology platforms. For example, you may want to reuse your models across a web site in ASP.NET MVC, and a desktop application in WPF, so it wouldn't make sense to have business logic in your view models.
View models should contain presentation logic which is specific to that particular application instance.
As for example code, this will be specific to the domain that you are modelling, so you would have to ask a more specific question about what you are trying to model.
I suggest you read PRISM documentation on this subject: Chapter 5: Implementing the MVVM Pattern.
Even if you don't plan to use PRISM, the article is very well written and details the pattern in Microsoft's technology terms.
I would trust very little of what Microsoft has to say on Patterns as their articles are often dated, or modified to support their own proprietary frameworks or products. Also I've found MS thoughts/approaches contradictory within their different teams and also within the generally accepted approach in software.
Not all of it is good and not all of it is bad basically.
To answer your question. I'd use a Service for Business Logic. I'd instantiate a reference to it in the ViewModel and I would not put any logic in either the ViewModel or the Model that did not have anything to do with their role. ie Presentation logic is okay dependingon what it is, it might be best placed in the ViewModel as it is highly tied to the View in question. Same goes for the model.
Keeping in mind these roles you will have a scaleable and testable application.
精彩评论