MVVM: Where to put the database CRUD? - mvvm

Should the model save/delete itself from the database or should the logic be in the ViewModel? I'm just starting with MVVM so I am unsure.

I use repository pattern for CRUD operations. Create a folder named Repositories and put your repositories in it. Then you can call them in your ViewModel.

Related

MVVM, repository pattern and service layer

I will develop an application with Xamarin.Forms. I will use mvvm pattern, but I heart from someone that he used repository pattern and services layer with mvvm.
What is the common way of working?
So MvvM is the View ( UI) which is data bound to the ViewModel (DTO) and the Model ( which is a class/ blueprint of the object)
The Model is not the real object, the model is not the persisted data. So yes. You do need a Repository or Service or Factory something to Take the model objects and persistent them.
If you take a look at the Xamarin Forms courses in the university, they use the a hard coded list of Simpson characters which is provided to the View Model from the SimpsonFactory..
If you are building something that stores the data to a database or XML you need to add that Repository or Data Service.
Another Example of this is in the Xamarin CRM app. While it uses Azure data tables instead of direct SQLite connections, it still uses a DataService to make changes to the objects in their persisted state.
I hope this helps.

Can entities be suplied to the UI withoud duplication in the viewmodal?

I am new to this and cannot seem to find/Google a concise answer.
I am making a WPF application and trying to follow the MVVM pattern. I am modeling a database with 100+ tables. The EntityFramework entities are auto-generated on my machine. I am using a WCF service to access the local modal (the database is on another machine). My ViewModal loads/saves the data by access to the WCF service, and the EntityFramework communicates with the database.
Following the MVVM pattern, my Views use the ViewModel as their data context.
In keeping with the MVVM pattern, do I have to copy the tables/entities from the Modal to the ViewModel in order to bind to the UI? (This would be quite extensive with 100+ tables).
In particular, I would like to be able to edit one or more fields on multiple records of the same table before saving the entire table back to the database. I do not wish to create copies of the classes of each entity in the viewmodel.
Any suggestions are welcome.
The answer here depends a bit on the perspective of individual people. Many people (myself included) have no problem with allowing the ViewModel to expose Model data to the View directly, which means you could just bind directly to your entities in the View. For this to work, the ViewModel can just have a property that directly contains the matching Model instances.

Datasource, Repository, ViewModels and IoC Container. Is a Repository really needed?

I current got a flow like this:
The Repository gets injected with a Datasource. The ViewModel gets injected with the Repository.
Because there's a constant flow of Items and Mutations (deletions, changes) that need to translate to the UI I don't keep a collection of items in the Repository. Therefor I ended up with a repository that only passes along Items between the Datasource and the ViewModels.
I've always understood that you shouldn't directly use the Datasource in the UI layer (ViewModels are in my UI layer). But is this still the case if you use dependency injection? Am I putting the Repository pattern to its use or is it just causing me overhead? (Right now it feels like it does.)
I originally implemented the repository to decouple the UI and Datasource but I've found that DI does a great job without a Repository.
You might want to have a look at some of the blog posts by Oren Eini (aka Ayende Rahien)
The evils of the repository abstraction layer
or
Repository is the new singleton
More often than not repositories are just "flow heaters". They do nothing but forward calls and results and just add overhead.
Btw: if you should or should not use repositories has nothing to do with dependency injection!
Update
Oren just published a new post on this topic.

ASP.NET MVC 2 Where to put logic

I have an ASP.NET MVC 2 application with some complex business rules and I'm trying to decide where to put specific logic.
The logic happens when creating records, based on certain fields of that record other records need to be created.
I'm currently using the repository pattern with an ORM and the easiest place to put this logic would be in my repository class but I feel like this is a pretty feeble location to have important rules, I would put it directly in my partial model classes that have my validation and metadata but I then have to call methods within my controller or repository and that may be extending too much knowledge about implementation to those layers.
What are your best practice tips for me?
Thanks!
You could have a service layer between the controller and the repositories. The repository performs simple CRUD operations with your model. A service method could make use of multiple simple repository calls to compose a business operation. This business operation will be exposed to the controller.

Updating a Model in asp.net mvc

Our project manager has asked us to refactor an app that was using the Repository Pattern (it was done using Nerddinner as example) to now use a service Layer.
My problem now is that Im not sure how to Update a Model cause the UpdateModel method is supposed to be used in the controller... whats a recomended approach to updating a model using repository pattern along with a service layer??
please help
I would suggest you 'hide' your current Repository Pattern inside your service layer. Data access code should not be visible to the clients of the service.
You can implement a collection of DTOs that will be returned from service layer or accepted as parameters. Those objects can be just POCOs to hold the data in a database-agnostic way.
DTOs are usually accompanied by Adapters for translation to/from your data access classes (that represent tables). This approach allows you to change database schema without changing service layer interface.
You can treat those DTOs as models in MVC, if your project is simple and data for your views matches the service layer DTOs. You can also define your models in MVC project and let controller or another set of adapters translate models into DTOs.
My preferred design includes model that are declared in MVC (Models folder) that work with strongly-typed views. UpdateModel method then works with those classes. Next controller or ModelAdapter creates an instances of Service Layer DTOs and passes them to services. DTO adapters inside services are then responsible to populate data access classes from repository pattern.