In my last post I used coupled MEF with the Repository Design Pattern to provide design time data to my application. This enabled me to see what my application would like with “real” data while using the Visual Studio designer. In this post I’ll use MEF and the Repository Design Pattern to provide runtime data to the application.
I’ll start by adding a new class library project named Customer.Repositories to the solution. I’ll then delete the default Class1.cs file that get created. Next I’ll add references to the following:
- The Customer.Contracts project
Next I’ll implement the ICustomer and ICustomerRepository interfaces. Here’s what I ended up with:
As you might expect, the implementations are pretty much the same as the ones I created for unit testing and design time data. I do want to show the implementation of the CustomerRepository:
As with the test and design repositories, I placed the [Export] attribute on the CustomerRepository to make it available to be consumed by MEF. Also pay attention to the first name (“Runtime”) of each customer in the repository. This will be important to remember when we run the app so that we can verify that we’re seeing the data we expect.
To use this repository I simply need to update the constructor of the ViewModelLocator class in the Customer.Wpf project as follows:
The result of this update is the MEF will use our newly created Customer.Repositories assembly at runtime. We can verify by running the Customer.Wpf application:
Ta-da! Our runtime customers are displayed.
You can download the source code from this post here. In my next post I’ll look at how we can eliminate some of the tight-coupling (yes there is some) in this sample solution.