Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - Where to put DAL in 3-tier architecture design with MVVM?

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?

like image 215
Rachel Avatar asked Jul 06 '10 19:07

Rachel


2 Answers

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);
    }
} 
like image 77
STO Avatar answered Nov 15 '22 17:11

STO


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

like image 39
Charles Bretana Avatar answered Nov 15 '22 17:11

Charles Bretana