Archives

Archives / 2012 / March
  • Getting Started with Prism (aka Composite Application Guidance for WPF and Silverlight)

    Overview

    Prism is a framework from the Microsoft Patterns and Practice team that allow you to create WPF and Silverlight in a modular way. It is especially valuable for larger projects in which a large number of developers can develop in parallel.

    Prism achieves its goal by supplying several services:

    · Dependency Injection (DI) and Inversion of control (IoC): By using DI, Prism takes away the responsibility of instantiating and managing the life time of dependency objects from individual components to a container. Prism relies on containers to discover, manage and compose large number of objects. By varying the configuration, the container can also inject mock objects for unit testing. Out of the box, Prism supports Unity and MEF as container although it is possible to use other containers by subclassing the Bootstrapper class.

    · Modularity and Region: Prism supplies the framework to split application into modules from the application shell. Each module is a library project that contains both UI and code and is responsible to initialize itself when loaded by the shell. Each window can be further divided into regions. A region is a user control with associated model.

    · Model, view and view-model (MVVM) pattern: Prism promotes the user MVVM. The use of DI container makes it much easier to inject model into view. WPF already has excellent data binding and commanding mechanism. To be productive with Prism, it is important to understand WPF data binding and commanding well.

    · Event-aggregation: Prism promotes loosely coupled components. Prism discourages for components from different modules to communicate each other, thus leading to dependency. Instead, Prism supplies an event-aggregation mechanism that allows components to publish and subscribe events without knowing each other.

    Architecture

    In the following, I will go into a little more detail on the services provided by Prism.

    Bootstrapper

    In a typical WPF application, application start-up is controls by App.xaml and its code behind. The main window of the application is typically specified in the App.xaml file. In a Prism application, we start a bootstrapper in the App class and delegate the duty of main window to the bootstrapper. The bootstrapper will start a dependency-injection container so all future object instantiations are managed by the container.

    Out of box, Prism provides the UnityBootstrapper and MefUnityBootstrapper abstract classes. All application needs to either provide a concrete implementation of one of these bootstrappers, or alternatively, subclass the Bootstrapper class with another DI container.

    A concrete bootstrapper class must implement the CreateShell method. Its responsibility is to resolve and create the Shell object through the DI container to serve as the main window for the application.

    The other important method to override is ConfigureModuleCatalog. The bootstrapper can register modules for the application. In a more advance scenario, an application does not have to know all its modules at compile time. Modules can be discovered at run time. Readers to refer to one of the Open Modularity Quick Starts for more information.

    Modules

    Once modules are registered with or discovered by Prism, they are instantiated by the DI container and their Initialize method is called. The DI container can inject into a module a region registry that implements IRegionViewRegistry interface. The module, in its Initialize method, can then call RegisterViewWithRegion method of the registry to register its regions.

    Regions

    Regions, once registered, are managed by the RegionManager. The shell can then load regions either through the RegionManager.RegionName attached property or dynamically through code.

    When a view is created by the region manager, the DI container can inject view model and other services into the view. The view then has a reference to the view model through which it can interact with backend services.

    Service locator

    Although it is possible to inject services into dependent classes through a DI container, an alternative way is to use the ServiceLocator to retrieve a service on demard. Prism supplies a service locator implementation and it is possible to get an instance of the service by calling:

    ServiceLocator.Current.GetInstance<IServiceType>()

    Event aggregator

    Prism supplies an IEventAggregator interface and implementation that can be injected into any class that needs to communicate with each other in a loosely-coupled fashion. The event aggregator uses a publisher/subscriber model.

    A class can publishes an event by calling eventAggregator.GetEvent<EventType>().Publish(parameter) to raise an event.

    Other classes can subscribe the event by calling eventAggregator.GetEvent<EventType>().Subscribe(EventHandler, other options).

    Getting started

    The easiest way to get started with Prism is to go through the Prism Hands-On labs and look at the Hello World QuickStart. The Hello World QuickStart shows how bootstrapper, modules and region works.

    Next, I would recommend you to look at the Stock Trader Reference Implementation. It is a more in depth example that resemble we want to set up an application.

    Several other QuickStarts cover individual Prism services. Some scenarios, such as dynamic module discovery, are more advanced.

    Apart from the official prism document, you can get an overview by reading Glen Block’s MSDN Magazine article.

    I have found the best free training material is from the Boise Code Camp.

    To be effective with Prism, it is important to understands key concepts of WPF well first, such as the DependencyProperty system, data binding, resource, theme and ICommand. It is also important to know your DI container of choice well. I will try to explorer these subjects in depth in the future.

    Testimony

    Recently, I worked on a desktop WPF application using Prism. I had a wonderful experience with Prism. The Prism is flexible enough even in the presence of third party controls such as Telerik WPF controls. We have never encountered any significant obstacle.

  • Getting started with ASP.NET Web API quickly

    Web API is a feature of ASP.NET MVC 4. It is an API for writing REST web services. One might ask why we need another API. After all, we already have WCF Rest Service a few years ago. It is also fairly easy to return JSON from an ASP.NET MVC controller using JsonResult. For a long answer, it is best read ScottGu;s blog or the WCF site. The short answer is that Microsoft want a way to make it extremely easy to create REST services and to provide lots of features. ASP.NET MVC is the best place. Currently, the .Net REST/Json story is fragmented and there are several Json serializers in .Net. ASP.NET Web API represents the future, consolidated API for this feature.

    It is fairly easy to get started with ASP.NET Web API. For those who likes to watch video, Jon Galloway’s 6 short videos provides a very quick start and it takes only 24 minutes to watch them all. For those who like read and type, the ASP.NET site has 7 short chapters that can be read in an hour or two. Many bloggers are blogging the Web API. I would recommend Stephen Walter’s blog for a quick introduction. Finally, I strongly recommend looking into some of the samples.

  • Experimenting with ASP.NET 4.5 beta

    The next version of ASP.NET is 4.5. The official information is at http://www.asp.net/vnext.

    There are two ways to experiment with ASP.NET 4.5 beta. The first is to install Visual Studio 11 beta. The second way is to install ASP.NET MVC 4 beta on Visual Studio 2010 to experiment with a subset of the features. Visual Studio 11 beta contains .Net Framework 4.5 beta. .Net Framework 4.5 is a highly compatible in-place update of .Net Framework 4.0. If you uninstall .Net Framework 4.5 beta, you will have to reinstall .Net Framework 4.0. For this reason, I would suggest you to experiment with Visual Studio 11 beta on an experimental machine, perhaps on a Windows 8 consumer preview VHD.

    On the other hand, ASP.NET MVC 4 beta consists of a set of new assemblies that can run side-by-side with MVC 3. It also has a new set of templates for MVC 4 projects. The only thing that affects MVC 3 work is that it changes the intelligence for web pages (.cshtml and .vbhtml) to support web pages 2 features. You need to build your web application before you edit any .cshtml or .vbhtml pages to overcome a bug.

    The following features can be experimented with ASP.NET MVC 4 beta on VS 2010:

    Not part of ASP.NET MVC 4 beta, there is an exciting open source framework from the ASP.NET team called SignalR that be experimented in VS 2010. SignalR allows developing long polling Comet type of applications using .NET. The ASP.NET team has also created an open source chat application called JabbR as a case study.

    In future blogs, I will blog my experiment with each of these exciting new areas.