I'm trying to follow the MVVM pattern in a reporting / statistics application that I'm making in C# / WPF.
I have made many model classes to hold properties as a starting point. Some of these models are made to encapsulate different statistics so that I can present data that can be cross refrenced.
I'm thinking the next step is to create a data access layer with classes and methods to connect to a database and grab the values to populate these models.
I was thinking of creating a connection and query passing class. Then other classes for each specific set of data that I will need for the models. (constructing queries to pass)
From what I understand I will be calling these data access layer methods from the view model to populate instances of my models. Which I will present with the view.
Is my approach in theory the correct standard way to approach this problem? Any suggestions? Or Reference Material?
-
1It seems like quite a logical approach, its good to keep the data access seperate from the view models.Alex Hope O'Connor– Alex Hope O'Connor2012年01月16日 02:28:04 +00:00Commented Jan 16, 2012 at 2:28
-
1@AlexHopeO'Connor If you could expand that in an answer, it may very well be a good answer. But you need to explain why it's a logical approach.yannis– yannis2012年01月16日 10:45:57 +00:00Commented Jan 16, 2012 at 10:45
2 Answers 2
You're definitely on the right track in making a Data Access Layer, although I would suggest making it more like a mysterious black box whose inner workings are unknown.
What I mean by this is your objects should be able to use the DAL to get/save objects, but they should have no idea how the data is actually obtained.
For example, instead of your ViewModel saying DAL.GetDataSet(someConnection, someQuery)
, you should call DAL.GetAccounts()
which returns a list of AccountModel
objects. The ViewModel should not care how or where the data is obtained from, only that it can magically get the objects it needs from the mysterious DAL object.
This keeps your data layer entirely separate, so if you ever need to change it out or adjust the queries, its all in one central location.
As a side note, your ViewModels should be the layer accessing the DAL. Models are simply dumb objects which contain data. They should have no advanced functionality, such as data access.
-
1+1 This. Further to the "mysterious black box", we typically abstract our DAL behind an IRepository interface and inject an implementation of the repository into the ViewModels. The Repository might access a database, remote services, or mock data, depending on your configuration; and the ViewModels are none the wiser.Dan J– Dan J2012年01月25日 00:04:14 +00:00Commented Jan 25, 2012 at 0:04
-
@djacobson I agree, but didn't want to make things too confusing :)Rachel– Rachel2012年01月25日 00:35:20 +00:00Commented Jan 25, 2012 at 0:35
-
(upped) for answer, particularly "mysterious black box"whytheq– whytheq2019年05月06日 13:36:46 +00:00Commented May 6, 2019 at 13:36
From what I have learned with MVVM you should only allow the Model to access the data layer and then the ViewModel can interact with the Model to get the information it needs. the ViewModel can then pass on this information through to the View.
Have you considered using any MVVM frameworks to lighten the load of developing?
If not, may I suggest using Caliburn.Micro with Unity?
These will both help you get started with MVVM and are very useful.
-
1Nooooooo don't do that! Models are dumb objects meant for holding data, they should not perform any kind of Data Access!Rachel– Rachel2012年01月24日 19:53:33 +00:00Commented Jan 24, 2012 at 19:53
-
@Rachel should I have a data access layer then that either the Model/ViewModel interacts with. I can see where you are coming from with the "dumb" comment and it makes sense. I am unsure of who interacts with the Data access layer then?stuartmclark– stuartmclark2012年01月25日 08:33:54 +00:00Commented Jan 25, 2012 at 8:33
-
1The ViewModel is usually the one to interact with the DAL. Sometimes the DAL is hidden behind another layer, the Repository layer, which is usually the "black box" that does all the data access calls. I've used both methods before (calling the DAL directly from my VM, or working with a Repository object from my VM), and which one I use is usually based on my project's size.Rachel– Rachel2012年01月25日 13:09:36 +00:00Commented Jan 25, 2012 at 13:09
-
@Rachel--you really do have this backwards--the Model is NOT "dumb objects"--it is the model of your business domain, which can very well have logic and will probably be persistent in some manner. Now you probably want to segregate the means of persistence using DAOs or something, but that would be underneath the Model. The purpose of the ModelView is simply to map/bind the View (UI) to the model. The ModelView should not even care about persistence.Matthew Flynn– Matthew Flynn2012年06月14日 15:45:24 +00:00Commented Jun 14, 2012 at 15:45
-
@MatthewFlynn check out this article msdn.microsoft.com/en-us/magazine/dd419663.aspx the ViewModel talks to the Repository.whytheq– whytheq2019年05月06日 17:07:16 +00:00Commented May 6, 2019 at 17:07