WPF and MVVM tutorial 03, The user repository.

Before starting to view in depth our model, or to design the views, I want to complete the DAL layer. Now that we have our unit of work implementation and our data context we need to implement a couple of repositories.

If you want to view how it should work a repository, I suggest this interesting article from Martin Fowler. The presenter should be:

A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.

The Flow of our application.

During the time I saw different repositories. One for each entity, one for each View and so on. My approach, usually, is to use a relationship of 1-to-1 from the view and the repository. So in our case we will have:

1 – The user opens the program and has to decide (search a customer, view all customers)

2 – A list will be presented to the user (filtered or not) and he will have to select one customer.

3 – A third view will show the details of the user, with two sub-view (address and order details).

So this is the flow that we will follow. For these reasons, you will easily understand that we need a couple of repositories: A) a user repository, B) an order repository.


The User repository.

The user repository has to give us a way to execute any kind of CRUD operation against our database. So we should have something like:


Here there are some basic operations we must be able to accomplish with our repository.

1) Get a specific Customer or Get all of them.

2) Add, Update or Drop a Customer.

3)Commit the changes to the data-model.



The code is very simple. First of all we need to create a sealed repository, so that nobody will play with it, for this purpose there is the IUnitOfWOrk interface …

  1: public sealed class CustomerRepository {
  2:     //Create an istance (it's static) of our Unit of Work
  3:     private IUnitOfWork _service;
  4:     //but let's give it a read-only access
  5:     public IUnitOfWork Service {
  6:         get { return _service; }
  7:     }
  8:     //create a new istance of the Unit of work
  9:     public CustomerRepository() {
 10:         _service = new UnitOfWork();
 11:     }

Then we need to implement the custom methods to retrieve the information we need from our model. Fortunately we have a generic unit of work so the code behind it’s very very simple!

  1: //Add a new customer
  2: public void AddCustomer(Customer _customer) {
  3:     Service.Add<Customer>(_customer);
  4: }
  5: //Drop an existing one
  6: public void DeleteCustomer(Customer _customer) {
  7:     Service.Delete<Customer>(_customer);
  8: }
  9: //Update an existing dirty customer
 10: public void UpdateCustomer(Customer _customer) {
 11:     Service.Update<Customer>(_customer);
 12: }
 13: Get a specific customer
 14: public Customer GetCustomer(int id) {
 15:     return Service.GetById<Customer>(p => p.CustomerID == id);
 16: }
 17: //Get all of them
 18: public IList<Customer> GetAllCustomers() {
 19:     return Service.GetAll<Customer>();
 20: }

We should also implement a filtered GetAllCustomers but we will use the View Model to do that in our example.

Customer class diagram.

Now we are pretty fine with our Customer model. What we have know is a model, a repository and a CRUD implementation for the customer and the related entities (Address and so on …). The final result in our DAL layer is this one:


Great we can already build now 3 views (CustomersView, CustomerView, FilteredCustomersView) just by using one repository. TO be honest we should be able to do everything but I want to keep separate the Order section.

In the next step I will give an overview of the ViewModel, and the View interaction, then we will start to build the views and then the relative ViewModels.

Ndr. I saw that this series of articles is being read by a nice number of people. Please post any ideas, suggestion or whatever you think about … I will appreciate.

Tags: MVVM Model View ViewModel WPF



re: WPF and MVVM tutorial 03, The user repository.

Thanks so much for sharing your knowledge. I am struggling to complete the solution on my own. Looking forward to your next tutorial. Cheers

6/11/2009 10:52 PM | David McVicar

re: WPF and MVVM tutorial 03, The user repository.

Leave a comment

Your email address will not be published. Required fields are marked *