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:

image 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:

image

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 …

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!

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:

image

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: