Title: Introduction to Unity 1'1 Application Block
1Introduction to Unity 1.1 Application Block
- Alex Thissen
- Class-Aalex.thissen_at_class-a.nl
Session Code NE.14
2Agenda
- 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
3What is Unity?
- Lightweight inversion of control container
- Application block by PatternsPractices
- Also used in EntLib 4.0
- Based on ObjectBuilder2 block
4It 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
5Small demo
- Some examples on coupling and dependencies
6Take 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
7Useful design patterns for IoC
- Factory
- Dependency injection
- Inversion of control
- Decorators
- Static gateway
8Dependency injection
- Make internal dependencies accessible to outside
world - Three common ways to inject a dependency
- Constructor
- Setter property
- (Interface) method call
9Inversion 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
10Services 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
11Example 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
12Resolving 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
()
13Resolving 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
14Registering 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
-
15Sample registration in code
IUnityContainer container new
UnityContainer() container  .RegisterTypeltTVWithS
omePlayer, TVWithSomePlayergt() Â .RegisterTypeltIVid
eoSource, DVDPlayergt() .RegisterInstanceltILoggt(so
meLogObject)
16Sample 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
17Unique unity features
- Comes as a separate IoC container
- Other IoCs are part of larger framework
- No need to register concrete types (per se)
18Managing 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
19Demo real world applications
- ASP.NET MVC using Unity
- "Normal" ASP.NET application
20Beyond 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
21Future 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
22Other IoC containers
- Autofac
- Ninject
- PicoContainer.NET
- Spring.NET
- StructureMap
- Castle Windsor
- Managed Extensibility Framework
- Not really an IoC container, but has similar
features
23Summary
- 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
24Evaluation 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