I’m developing an application using WPF. The pattern that I’m using for it is obviously MVVM. Also I’m using Entity Framework ORM and LINQ-to-Entities to query the EF objects.
My understanding of MVVM is that the View should have no code-behind and only knowledge it should have of the ViewModel is that the ViewModel contains certain properties with which the View is bound and also it contains commands to handle events of the view. Whereas the Model contains only code to get data from DB.
In my model classes I have just written functions which directly query EF objects using Linq-to-entities. The processing that I need to perform on the data before assigning it to properties in VM is either present in the VM or in classes present in the VM project. Just to mention here, that I have 3 projects – View, ViewModel and Model.
My question here is that, can I keep those functions (which involve processing on data from DB) in the VM project or should it be in the Model project? If in ViewModel project, should it be in the concerned VM or in separate classes present in the VM project?
One of the commonly used practice is to delegate the business logic to the service layer and perform business logic using Repository Pattern. As mentioned in other answers, the ViewModel should decorate the model with view specific properties and commands which handle UI interactions. By its own defination the Model should have all the logic for manipulating the data.
If you are following Service Oriented Architecture, then the ViewModel can be injected with a service. The service is responsible for perfoming the business functions. These business functions are mostly done on the persisted data. And that logic can be abstracted nicely using any of the ORM tools and technologies like EF or NHibernate. You can do a google search for Repository Pattern which can be helpful if you follow this architecture.
It is not necessary to use the Repository Pattern if you don’t need it. You can still use EF directly from your service layer. Advantage of having the repository is that it acts like a mapper between the persistance layer(database) and the domain layer(model).
Hope this helps.