Udostępnij za pośrednictwem


Tales from the Trenches: Using Unity in a Windows Store app

patterns & practices Developer Center

On this page: Download:
Case study provided by David Britch | AdventureWorks Shopper | References

Download code

Download PDF File

Order Paperback

Case study provided by David Britch

AdventureWorks Shopper

The AdventureWorks Shopper reference implementation is a Windows Store business app, which uses Prism for the Windows Runtime to accelerate app development. It provides guidance to developers who want to create a Windows Store business app using C#, Extensible Application Markup Language (XAML), the Windows Runtime, and modern development practices.

The reference implementation shows how to:

  • Implement pages, controls, touch, navigation, settings, suspend/resume, search, tiles, and tile notifications.
  • Implement the Model-View-ViewModel (MVVM) pattern.
  • Validate user input for correctness.
  • Manage application data.
  • Test your app and tune its performance.

Prism for the Windows Runtime accelerates app development by providing core services commonly required by a Windows Store app. These core services include providing support for bootstrapping MVVM apps, state management, validation of user input, navigation, event aggregation, data binding, commands, Flyouts, settings, and search.

While Prism for the Windows Runtime does not require you to use a dependency injection container, for AdventureWorks Shopper we chose to use the Unity dependency injection container. AdventureWorks Shopper uses the MVVM pattern, and in the context of a Windows Store app that uses the MVVM pattern, there are specific advantages to using Unity:

  • It can be used to register and resolve view models and views.
  • It can be used to register services, and inject them into view models.
  • It can be used to create view models and inject the views.

In AdventureWorks Shopper we used Unity to manage the instantiation of view model and infrastructure service classes. Only one class holds a reference to the Unity dependency injection container. This class instantiates the UnityContainter object and registers instances and types with the container.

The main reason for using Unity is that it gave us the ability to decouple our concrete types from the code that depends on those types. During an object's creation, the container injects any dependencies that the object requires into it. If those dependencies have not yet been created, the container creates and resolves them first.

Using Unity has resulted in several advantages for AdventureWorks Shopper:

  • It removed the need for a component to locate its dependencies and manage their lifetime.
  • It allowed mapping of implemented dependencies without affecting the components.
  • It facilitated testability by allowing dependencies to be mocked.
  • It increased maintainability by allowing new components to be easily added to the system.

Overall, using Unity in a Windows Store business app has resulted in a much cleaner architecture and has helped to further our separation of concerns.

References

For further information about the AdventureWorks Shopper reference implementation, and Prism for the Windows Runtime, see:

Next Topic | Previous Topic | Home | Community