Prism 6 for Windows 10 UWP: Autofac

A new IoC container for Prism

I’m happy to announce that you can now use Autofac as IoC container for your Windows 10 UWP applications written with Prism. As the major part of the necessary functionality is moved into the PrismApplication and PrismAutofacApplication base classes, you only need a few lines of code to get started.

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
sealed partial class App : PrismAutofacApplication
{
    /// <summary>
    /// Initializes the singleton application object.  This is the first line of authored code
    /// executed, and as such is the logical equivalent of main() or WinMain().
    /// </summary>
    public App()
    {
        this.InitializeComponent();
    }

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate("Main", null);
        return Task.FromResult<object>(null);
    }

    protected override void ConfigureContainer(ContainerBuilder builder)
    {
        base.ConfigureContainer(builder);

        RegisterTypeIfMissing<IDataRepository, DataRepository>(builder, true);
    }
}

The code sample shows the few steps needed to get your application up and running:

  • Derive your app from the PrismApplication class and make sure the declaration in XAML derives from this base class as well.
  • Override the OnLaunchApplicationAsync method to plug in your initial navigation.
  • Override the ConfigureContainer method to register any extra dependencies in your Autofac container.

We currently only have a small sample app to show you how to get started, but you can expect more advanced scenarios in the upcoming months.

Licensed under CC BY-NC-SA 4.0; code samples licensed under MIT.
comments powered by Disqus
Built with Hugo - Based on Theme Stack designed by Jimmy