views:

91

answers:

3

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:

  • The View, or UI layer, which is the xaml file
  • The Model, which is a custom class containing properties and methods that "models" the data object
  • The ViewModel, which is the "adapter" between the View and the Model
  • A WCF Server which is supposed to handle Database Access among other things
  • SQL Database for storing data

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?

A: 

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...

Charles Bretana
+4  A: 

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);
    }
} 
STO
Thank you, that clarified it for me
Rachel
A: 

There are tons of very lengthy blog posts and descriptions on organizing all this. Here is the one I read most recently (today):

[link text][1]

[1]: http://dotnetslackers.com/articles/data-management/About-layers-separation-and-Entity-Framework.aspx "Dino Esposito on EF and Layers

fdfrye