AutoFac or Unity integration with Prism

When creating a WPF modular application in using Prism, your main application entry point, may need to be injected with some dependent services. Assume your main application entry page is called Shell.xaml and it needs to be constructed with an IRibbonService implementation. This dependency needs to be passed using an IoC container such as Microsoft Unity or AutoFac. This blog post provides minimal Boostrapper implementations for both IoC containers.

First Order of Business: Remove StartupUri=”Shell.xaml” in App.xaml.

This is often a stumbling block and would waste you hours of troubleshooting.

Imagine the constructor of your main Shell.xaml.cs looking like this:

  public Shell(IRibbonService ribbonService)
        {
            InitializeComponent();

            //.. do something with this ribbonService
        }

In other for this to work the following must hold true:

1. An implementation of IRibbonService has been registered with the IoC container.
2. You use the appropriate means to initialize the Shell so that its dependencies are constructed.

You implement the above steps by sub-classing an appropriate Boostrapper base class.

Using Microsoft Unity, after installing NuGet package Prism.UnityExtensions, your Boostrapper should be implemented similar to this:

 public class UBootstrapper : UnityBootstrapper
    {
        protected override void InitializeShell()
        {
            base.InitializeShell();
            // hook the main application window with your Shell
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
            // ensure an implementation of IRibbonService is registered with the container
            RegisterTypeIfMissing(typeof(IRibbonService), typeof(RibbonService), true);
        }

        protected override DependencyObject CreateShell()
        {
            // here allow the ServiceLocator to create an instance of Shell, passing it dependencies
            return ServiceLocator.Current.GetInstance<Shell>();
        }            
    }

If you want to use AutoFac instead, you will need to install Prism.AutocExtension first. Then your Boostrapper implementation will look like this:

 public class UBootstrapper : AutofacBootstrapper
    {
        protected override void InitializeShell()
        {
            base.InitializeShell();
            // hook the main application window with your Shell
            Application.Current.MainWindow = (Window)Shell;
            Application.Current.MainWindow.Show();
        }

        protected override void ConfigureContainer(ContainerBuilder builder)
        {
            builder.RegisterType<RibbonService>().SingleInstance().As<IRibbonManager>();
            builder.RegisterType<MainWindow>().SingleInstance();
 
            base.ConfigureContainer(builder);
        }

        protected override DependencyObject CreateShell()
        {
            // here allow the ServiceLocator to create an instance of Shell, passing it dependencies
              return Container.Resolve<MainWindow>();
        }            
    }

Differences are subtle but you need to get each of those parts right.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s