Introduction to Unity 1'1 Application Block - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Introduction to Unity 1'1 Application Block

Description:

The 'What' and 'Why' of Unity. Understanding dependencies and injection ... Smaller stuff: bug fixes, improvements to tooling. Other IoC containers. Spring.NET ... – PowerPoint PPT presentation

Number of Views:219
Avg rating:3.0/5.0
Slides: 24
Provided by: alexth
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Unity 1'1 Application Block


1
Introduction to Unity 1.1 Application Block
  • Alex Thissen
  • Class-Aalex.thissen_at_class-a.nl

Session Code NE.14
2
Agenda
  • The "What" and "Why" of Unity
  • Understanding dependencies and injection
  • Inversion of control containers
  • Registering and resolving dependencies
  • Object lifetimes
  • Using Unity in real applications

3
What is Unity?
  • Lightweight inversion of control container
  • Application block by PatternsPractices
  • Also used in EntLib 4.0
  • Based on ObjectBuilder2 block

4
It depends
  • Software components depend on each other
  • Not necessarily a bad thing
  • Coupling
  • How rigid is the dependency?
  • Is it a useful/desired dependency?
  • Can you control who creates and calls who?

Example TV with built-in DVD player
5
Small demo
  • Some examples on coupling and dependencies

6
Take control of dependencies
  • Pluggable architecture
  • Reduce number of dependencies
  • Be selective not every dependency has to go
  • Configuration of concrete types
  • Dynamic change of components at runtime
  • Unit testing
  • Decorate types with other types

7
Useful design patterns for IoC
  • Factory
  • Dependency injection
  • Inversion of control
  • Decorators
  • Static gateway

8
Dependency injection
  • Make internal dependencies accessible to outside
    world
  • Three common ways to inject a dependency
  • Constructor
  • Setter property
  • (Interface) method call

9
Inversion of control
  • Have other code call into your code
  • An object does not (always) choose or make its
    own dependent objects

Hollywood Principle "Don't call us, we'll call
you"
Bedtime story The girl that wants to bake a
cake
Richard Sweet
10
Services of an IoC container
  • Service locator
  • "Finds" objects based on type
  • Managing lifetime of depended-on objects
  • Automatic injection of dependencies
  • Configuration
  • other than just in code XML, script or database

11
Example of IoC based injection
  • TV depends on an object of some type
  • Ask container to resolve that type
  • IoC containercreates object
  • Object gets injected into TV
  • TV uses object

Inject object
ResolveltIVideoSourcegt
12
Resolving types using Unity
  • Unity container creates object instances during
    resolution of types
  • Container must be asked to resolve a type

IUnityContainer container new
UnityContainer() ... // Resolve unnamed instance
for IVideoSource IVideoSource source
container.ResolveltIVideoSourcegt() // Resolve
all types for IVideoSource IEnumerableltIVideoSourc
egt sources container.ResolveAllltIVideoSourcegt
()
13
Resolving dependencies
  • Dependencies are wired up for resolved types
  • Properties marked as Dependency
  • Constructor marked InjectionConstructor (or
    longest)
  • Methods marked InjectionMethod
  • Unity can also wire up dependencies on objects
    not created by container

14
Registering types and objects
  • Container has registration for object resolution
  • Maps interfaces and abstract base classes to
  • Concrete types
  • Existing object instances
  • Performed only once per container instance
  • Use code or configuration
  • Register multiple types based on names

15
Sample registration in code
IUnityContainer container new
UnityContainer() container  .RegisterTypeltTVWithS
omePlayer, TVWithSomePlayergt()  .RegisterTypeltIVid
eoSource, DVDPlayergt() .RegisterInstanceltILoggt(so
meLogObject)
16
Sample registration using config
// Configure using web.config information IUnityCo
ntainer container new UnityContainer() UnityCon
figurationSection section (UnityConfigurationS
ection)ConfigurationManager .GetSection("unity")
section.Containers.Default.Configure(container)

ltunitygt ltcontainersgt   ltcontainergt   lttypesgt  
  lttype type"ICustomerRepository"
mapTo"CustomerRepository" /gt    lt/typesgt  
lt/containergt lt/containersgt lt/unitygt
17
Unique unity features
  • Comes as a separate IoC container
  • Other IoCs are part of larger framework
  • No need to register concrete types (per se)

18
Managing lifetime
  • Control the lifetime of objects created by Unity
  • Use LifetimeManager derived classes during
    registration
  • Container controlled singleton
  • Transient new every time
  • Externally managed keeps weak references
  • RegisterInstance method
  • Singleton object not constructed by container

19
Demo real world applications
  • ASP.NET MVC using Unity
  • "Normal" ASP.NET application

20
Beyond the basics
  • Child containers
  • Register different mappings
  • Multiple singletons of same type
  • Extensions
  • Extra functionality beyond container's
    capabilities
  • E.g. "Static factory extension" delegates object
    construction to your factory

21
Future plans
  • Unity 1.2
  • Container extension for interception
    mechanism(AOP style programming)
  • Include integration with Policy Injection
    application block
  • Port for Silverlight
  • Smaller stuff bug fixes, improvements to tooling

22
Other IoC containers
  • Autofac
  • Ninject
  • PicoContainer.NET
  • Spring.NET
  • StructureMap
  • Castle Windsor
  • Managed Extensibility Framework
  • Not really an IoC container, but has similar
    features

23
Summary
  • Control your dependencies
  • Learn the design patterns
  • Design for loose coupling where necessary
  • IoC containers can help loosening dependencies
  • Unity is a standalone IoC container API

24
Evaluation form
Vul je evaluatieformulier in en maak kans op een
van de prachtige prijzen!! Fill out your
evaluation form and win one of the great
prizes!! Session Code NE.14
Write a Comment
User Comments (0)
About PowerShow.com