Partager via


Developer's Guide to Dependency Injection Using Unity

patterns & practices Developer Center

On this page: Download:
Contents | About This Guide | Who This Book Is For | What Do You Need to Get Started? | Who’s Who | The Team Who Brought You This Guide | The Unity 3 Development Team

Download code

Download PDF File

Order Paperback

Contents

About This Guide

This guide is one of the resources available with the Unity v3 release to help you to learn about Unity, learn about some of the problems and issues that Unity can help you to address, and get started using Unity in your applications. Unity is primarily a dependency injection container and so the guide also contains an introduction to dependency injection that you can read in isolation even if you don’t plan to use Unity, although we hope you will.

The chapters are designed to be read in order, each one building on the previous one, and alternating chapters that cover some of the conceptual background material with chapters that address the specifics of using Unity in your own applications. If you’re already familiar with concepts such as dependency injection and interception, you can probably focus on Chapter 3, “Dependency Injection with Unity,” Chapter 5, “Interception with Unity,” and Chapter 6, “Extending Unity.”

The first two chapters introduce the conceptual background and explain what dependency injection is, what are its benefits and drawbacks, and when you should consider using it. Chapter 3 then applies this theoretical knowledge to the use of the Unity container and provides examples and guidance on how to use it in a variety of scenarios.

Chapter 4 describes interception as a technique to dynamically insert code that provides support for crosscutting concerns into your application.

Chapter 5 discusses advanced topics of interception and policy injection, along with alternatives, and offers some suggestions about when you should use it.

The final chapter introduces some of the ways that you can extend Unity such as creating container extensions or creating custom lifetime managers.

All of the chapters include references to additional resources such as books, blog posts, and papers that will provide additional detail if you want to explore some of the topics in greater depth. For your convenience, there is a bibliography online that contains all the links so that these resources are just a click away.

This guide also includes several case studies, called Tales from the Trenches, of developers adapting and customizing Unity. If you want to share your story with the developer community at large, send it to ourstory@microsoft.com.

The majority of the code samples in the chapters come from a collection of sample applications that you can download and play with.

This guide does not include detailed information about every Unity feature or every class in the Unity assemblies. For that information, you should look at the Unity Reference Documentation and the Unity API Documentation.

Who This Book Is For

This book is intended for any architect, developer, or information technology (IT) professional who designs, builds, or operates applications and services and who wants to learn how to realize the benefits of using the Unity dependency injection container in his or her applications. You should be familiar with the Microsoft .NET Framework, and Microsoft Visual Studio to derive full benefit from reading this guide.

What Do You Need to Get Started?

The system requirements and prerequisites for using Unity are:

  • Supported architectures: x86 and x64.
  • Operating systems: Microsoft Windows 8, Microsoft Windows 7, Windows Server 2008 R2, Windows Server 2012.
  • Supported .NET Frameworks: Microsoft .NET Framework 4.5, .NET for Windows Store Apps (previously known as WinRT).
  • Rich development environment: Microsoft Visual Studio 2012, Professional, Ultimate, or Express editions.

You can use the NuGet package manager in Visual Studio to install the Unity assemblies in your projects.

Who’s Who

The guide includes discussions and examples that relate to the use of Unity in a variety of scenarios and types of application. A panel of experts provides a commentary throughout the book, offering a range of viewpoints from developers with various levels of skill, an architect, and an IT professional. The following table lists the various experts who appear throughout the guide.

Dn223671.9B88E0AA824EE55D6EDEBA29CC377173(en-us,PandP.30).png

Markus is a software developer who is new to Unity. He is analytical, detail-oriented, and methodical. He's focused on the task at hand, which is building a great LOB application. He knows that he's the person who's ultimately responsible for the code.

"I want to get started using Unity quickly, so I want it to be simple to incorporate into my code and be easy to configure with plenty of sensible defaults."

Dn223671.978938FF49D8B7C34E84E6ABF6499EBE(en-us,PandP.30).png

Beth is a developer who used Unity some time ago but abandoned it for her more recent projects.

"I'm happy using libraries and frameworks but I don't want to get tied into dependencies that I don't need. I want to be able to use just the components I need for the task in hand."

Dn223671.58074E3B4A85A72AF482381FA8A3E586(en-us,PandP.30).png

Jana is a software architect. She plans the overall structure of an application. Her perspective is both practical and strategic. In other words, she considers not only what technical approaches are needed today, but also what direction a company needs to consider for the future. Jana has worked on many projects that have used Unity as well as other dependency injection containers. Jana is comfortable assembling a solution using multiple libraries and frameworks.

"It's not easy to balance the needs of the company, the users, the IT organization, the developers, and the technical platforms we rely on while trying to ensure component independence."


Dn223671.E456CDB53F60546AE997C3FE679B7BB5(en-us,PandP.30).png

Carlos is an experienced software developer and Unity expert. As a true professional, he is well aware of the common crosscutting concerns that developers face when building line-of-business (LOB) applications for the enterprise. His team is used to relying on Unity and he is happy to see continuity in Unity releases. Quality, support, and ease of migration are his primary concerns.

"Our existing LOB applications use Unity for dependency and interception. This provides a level of uniformity across all our systems that make them easier to support and maintain. We want to be able to migrate our existing applications to the new version with a minimum of effort."

Dn223671.3B6A1E6EE364981BD427041F9C239600(en-us,PandP.30).png

Poe is an IT professional who's an expert in deploying and managing LOB applications. Poe has a keen interest in practical solutions; after all, he's the one who gets paged at 3:00 AM when there's a problem. Poe wants to be able to tweak application configuration without recompiling or even redeploying them in order to troubleshoot.

"I want a consistent approach to configuration for all our applications both on-premises and in the cloud."

The Team Who Brought You This Guide

Vision

Grigori Melnik

Main Author

Dominic Betts

Contributing Authors

Grigori Melnik, Fernando Simonazzi, and Mani Subramanian

Foreword Author

Chris Tavares

Technical Reviewers

Jesus Aguilar, Garrett Besser, Nicholas Blumhardt, Nuno Centeno, Steven van Deursen, Julian Dominguez, Alex Homer, Rob Jarratt, Rino Johansen, Mike Lanzetta, Randy Levy, Thomas Mueller, Ksenia Mukhortova, Tyler Ohlson, Dan Piessens, Peter Ritchie, Luke Sigler, Chris Tavares, and Jon Wagner.

Graphic Artist and Book Layout

Chris Burns

Editor

RoAnn Corbisier

Production and Release

Nelly Delgado and Richard Burte

The Unity 3 Development Team

Product/Program Management

Grigori Melnik (Microsoft Corporation).

Architecture/Development

Julian Dominguez and Bob Brumfield (Microsoft Corporation), Fernando Simonazzi and Edgardo Rossetto (Clarius Consulting), and Hernan de Lahitte (Digit Factory)

Testing

Mani Subramanian (Microsoft Corporation), Mariano Grande (Digit Factory), Rathi Velusamy, Lavanya Selvaraj, and Shebu Kollam Valappil (Infosys Technologies Ltd.)

Performance/Security Testing

Carlos Farre (Microsoft Corporation), Naveen Pitipornvivat and Soranai Chantarayotha (Adecco)

Documentation

Dominic Betts (Content Master Ltd)

Editing/Pre- and Post Production

RoAnn Corbisier, Nelly Delgado, Handan Selamoglu and Jane Sinyagina (Microsoft Corporation), Chris Burns (Linda Werner & Associates)

Release Management

Nelly Delgado and Grigori Melnik (Microsoft Corporation) and Richard Burte (ChannelCatalyst.com, Inc.)

Administrative Support

Kerstin Scott (Microsoft Corporation)

Advisory Council

Fabian Fernandez Bargas (TellMi), Joseph M. Blank (Online Business Systems), Bryan Clark (Ethos Solutions), Gabriele Giuseppini (Optiver Holding B.V.), Deon Heyns, Matt Kean (Caterpillar Inc), Ercenk Keresteci and Trent Swanson (Full Scale 180, Inc.), Randy Levy, Jeremy Likness (Wintellect), Andrei Marukovich (AB SCIEX), Ksenia Mukhortova (Intel), Peter Nilsson (Volvo Information Technology AB), Dan Piessens (Centare), Peter Ritchie (Peter Ritchie Inc.), Luke G. Sigler (Qualcomm Life), Jon Wagner (eMoney Advisor), Sebastian Weber, Bill Wilder (DevPartners), Matias Woloski (Auth0), Walter Wu (Royal Bank of Canada), Nuno Centeno, Jason Hogg, Tom Hollander, Robert Jarratt, Michael Lanzetta, Valery Mizonov, Rafael Fernandez Moctezuma, Tyler Ohlsen, Timothy Stockstill, Christopher Tavares, Michael Thomassy, and Rob Vettor (Microsoft Corporation)

Community

Attendees at patterns & practices summits and symposia, //Build, TechReady, and TechEd conferences who provided informal feedback; and Unity users who commented on this guide on CodePlex, through our blogs, surveys and via e-mail.

Thank you!

Next Topic | Home | Community