0

I am new to the MVVM pattern. I have a window which has 3 text boxes (Name, Address, Description), a save button, and a listview which displays the above fields. When the save button is clicked I want to save the fields into database as well as show the record in the listview.

How do I design my Model, ViewModel for this interface ?

1

This is how I see (and do) it.

In your case (which is rather simple), I would have a ViewModel holding a list of Models and a SelectedModel property. The view would have a form and a list of course. The form would be bound to the currently selected model. The View would be bound to the ViewModel and indirectly to the Model (the data in the list and the form). Your ViewModel would also take care of validating and saving the data to the database by calling a method on your repository (or the Context, which is basically a repository, if you're using EF).

The Model is your business logic, the Domain Model. The objects here will have all the data relevant to the business and the methods to manipulate that data. The model should be designed to be as simple as possible while accomplishing the business needs. This means normalized relationships, decoupled design and so on. Make it as easy to maintain as possible, regardless of the view. This means the same Model can be reused in different applications (a desktop and a web app can use the same model). In your case this is an object with the Name, Address and Description. This is all you need to accomplish your goal.

The View will not always be this simple. Sometimes you will need to aggregate data from multiple models, or do some other manipulation of the data just to show the data on a view. A report for instance, can have lots of data from lots of models. Other times, the view will really need to simplify a lot of complex things that are going on in the model, to give you a high level overview without too many details. In your case, the view is also a bit more complex than your domain model. You need a list to see all your domain models and a form to edit one at the same time.

This is where the ViewModel comes in. The VM will be between the model and the view. It will wrap a model object (or multiple model objects), introduce new properties that are a combination of other properties in the model and so on. The ViewModel is the one that is designed in a way that it makes presentation easier. This means simple properties your view can bind to and similar things. No matter how complex your Model or your View is, your ViewModel sits in between and makes all the necessary transformations of the data that you want to display. The same thing but in reverse happens when a view is sending data back (form submission or other input). The ViewModel is the one that transforms the view data into something that your Model understands. In your case the ViewModel will be rather simple, holding just the list of Models and a Selected Model, but in more complex examples, it might do some calculations or whatnot to accomplish what the view needs.

2

This is how we are implementing MVVM in my company, and naturally also the way I would prefer:

The model should reflect what you need to present on the View, and your database should also adapt to this. This means that your model(and your DB) should contain fields for Name, Address and Description.

I prefer to pass a Model to my ViewModel. Some people would argue that this is wrong according to the MVVM pattern, but it ensures that your model is preserved in the right form, from the View and all the way down to your DAL layer.

So, your ViewModel contains a Model, which has some properties you could bind to. When you hi the Save button, the ViewModel intercepts the command specified, and parses the Model to the Bll layer. The Bll layer uses the DAL layer to persist the Model in the database. You could also make some checks in the Bll layer to ensure you don't make dulicates. You might want to override an existing entry instead of creating a new one.

Edit: Just a quick example:

Example

  • yes wonderful explanation .Currently I dont have any DataAccess Layer I use raw sql like insert into MyTable values(value1,value2...) on the save command as a Method Namely Save() in my ViewModel is it right? – Mussammil Feb 7 '14 at 10:40
  • The Data Access Layer could consist of the raw sql queries. You shouldn't have that in your BLL. You could create a static DAL layer, which handles connection to your SQL database and queries. You should have on DAL object for each Model you are using. – Belicosus Feb 10 '14 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.