I'm fairly new to the whole n-tier architecture thing, and I had some questions about using MVVM with a 3-tier application.
From my understanding, we have:
My question is, how do I put this all together using the Data Access Layer? With MVVM, I would have the models contain the methods to Load/Update themselves. Instead should this be something that happens on the WCF Server? If so, should the reference to the server be stored in the Model or the ViewModel? And how should it be called?
Strictly, DAL is not a part of MVVM pattern. DAL is somewhere 'behind' model, and view and view model should know nothing about DAL.
For example, expose entities as properties of your model which are loaded at first access.
public class ProductListModel
{
public List<Product> AllProducts
{
get
{
if (_AllProducts == null)
_AllProducts = MyServiceProxy.LoadAllProducts(...)
return _AllProducts;
}
}
public void SaveChanges()
{
if (_AllProducts != null)
MyServiceProxy.SaveProducts(AllProducts);
}
}
Data Access is a separate and independant issue... You can implement it in a number of different ways and patterns, but in all cases, the end result is a model that will be used by your MVVM classes.
The WCF may return the classes used in your model, or it may return simpler classses that are designed just as data transfer objects, in which cxase you will have transform these objects into instances of the classes defined in your model...
Actual data access (to-from the DataBase itself is of course coded on the server side of the WCF...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With