Archives For wpf

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
  • System.ComponentModel.Composition

Next I’ll implement the ICustomer and ICustomerRepository interfaces. Here’s what I ended up with:

image

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:

image

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:

image

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:

image

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.

In my previous post I used MEF to decouple my unit tests from runtime data by using the Repository Design Pattern to implement a repository meant for “test time” consumption. In this post, I’ll take the Repository Design Pattern one step further and couple it with MEF to provide design time data.

I’ll start by adding a new class library project named Customer.Repositories.Design to the solution (I don’t think we need a picture for this). I’ll then delete the default Class1.cs file that gets created. Next I’ll add references to the following:

  • The Customer.Contracts project
  • System.ComponentModel.Composition

Next I’ll implement the ICustomer and ICustomerRepository interfaces. Here’s what I came up with:

image

Nothing too fancy. They’re virtually identical to the classes we created for unit testing. I do want to show the implementation of the DesignCustomerRepository:image

As with the test repository, I placed the [Export] attribute on the DesignCustomerRepostiory to make it available to be consumed by MEF. Also pay attention to the first name (“Design”) of each customer in the repository. This will be important to keep in mind when we finally get the runtime repository implemented.

Now we need to consume this repository. To do so, I’ll start by creating a WPF application called Customer.Wpf. I’ll then add a reference to the Customer.Core project. I’m then going to add a class to the project called ViewModelLocator. This class will determine which repository the view model will used based on whether we’re in the designer or if we’re running the application. (This class is based on the Laurent Bugnion’s MVVMLight Toolkit.) There are a couple of things I ‘d like to highlight in the class. The first is how we determine if were in design mode:

image

The implementation for Silverlight is a little bit different, but I’ll cover that in a future post. The next is the constructor for the class:

image

Here I’m passing in the path to the assembly for the AssemblyCatalog to use in the view model. When we’re ready for runtime data we’ll plug in an else statement here.

Next I’ll want to make the ViewModelLocator class a resource that I can use in databinding. To do this, I’ll update App.xaml to look like the following:

image

I’ll then update MainWindow.xaml to bind to the CustomerListViewModel property of the ViewModelLocator class:

image

If we take a peek at the design surface…

image

…we’ll see our “design” customers displayed!

You can download the source code from this post here. In my next post I’ll use MEF to get our runtime customer data.

PDC10 Recap

November 5, 2010

On Thursday, November 4, 2010 Brent Stineman and I recapped PDC10 with an open discussion at the Twin Cities .NET User Group. The discussion had two main areas of focus, one being, what I’m calling, “The HTML5 Incident” and the other being announcements regarding Windows Azure. Below are links to resources from each focus area.

“The HTML5 Incident”

Windows Azure Announcements

Additional Resources

(On a side note, I had originally planned on giving a presentation on the Managed Extensibility Framework. I’ll work with the organizers of the Twin Cities .NET User Group to get that session back on the schedule some time 2011. Between now and then I will be posting a multi-part series on MEF, so stay tuned!)

On Saturday, October 9, 2010 I presented at Twin Cities Code Camp on Multi-Targeting WPF, Silverlight, and Windows Phone 7. Here’s a brief overview of the session:

With WPF, Silverlight, and now Windows Phone 7, all using xaml, multi-targeting applications can be a reality. However, it’s not without its challenges. In this session I’ll talk about which things are easiest to share, and which are hardest. I’ll also show you some recommended approaches on how to structure your project, as well as a comparison of the different ways by which you can share code and xaml. I’ll cover topics such as assembly portability, code sharing using linked files, usercontrol encapsulation and abstraction, and even loading xaml at runtime after tinkering with the source via code.

You can download the source code for the session demos here.