Introduzione ai framework ioc
-
Upload
andrea-balducci -
Category
Documents
-
view
1.427 -
download
1
description
Transcript of Introduzione ai framework ioc
14° Workshop – DotNetMarche :: Castelfidardo, 16 Aprile 2010
Introduzione ai framework per IoC / DI e AOP
Andrea Balducci
mtb.snowboard @ gmail.com
http://www.ienumerable.it
Riferimenti
andreabalducci
Framework
Tipo Comportamento
Unity http://unity.codeplex.com/Wikipagehttp://msdn.microsoft.com/en-us/library/dd203101.aspx
Ninject http://ninject.org/
Autofac http://code.google.com/p/autofac/
Spring http://www.springframework.net/
Castle project http://www.castleproject.org/container/
Unity
“The Unity Application Block (Unity) is a lightweight extensible dependency injection container with support for constructor, property, and method call injection.”
http://unity.codeplex.com/
Definiamo un contratto
public interface IOperazioneMatematica{
int Esegui(int a, int b);}
Implementazione
public class OperazioneSomma : IOperazioneMatematica{ public int Esegui(int a, int b) { return a + b; }}
Configurazione del container
// Creazione containerIUnityContainer container = new UnityContainer();
// Registrazione componenticontainer.RegisterType<
IOperazioneMatematica, // contrattoOperazioneSomma // implementazione
>();
Utilizzo del container
// Richiesta del servizio di calcolovar operazione =
container.Resolve<IOperazioneMatematica>();
// Utilizzo del servizioint risultato = operazione.Esegui(1, 2);
Injection
[InjectionConstructor]Per marcare un costruttore da utilizzare esplicitamente. In assenza viene usato il costruttore con il maggior numero di parametri.
[Dependency]Marca il setter della property
[InjectionMethod]Marca un metodo di inizializzazione
{demo}
Castle Windsor“Castle Project offers two Inversion of Control Containers. The MicroKernel and the Windsor Container.”
“The Castle MicroKernel is an inversion of control container that was designed towards extensibility. It combines facilities to grow orthogonally.”
Castle Windsor aggregates the MicroKernel and exposes a powerful configuration support. It is suitable for common enterprise application needs. It is able to register facilities and components based on the configuration and adds support for interceptors
http://www.castleproject.org/container/index.html
Windsor - ComponentLifecycle
Tipo Comportamento
LifestyleType.Custom http://www.primordialcode.com/blog/post/castle-windsor-transient-objects-and-release-policies
LifestyleType.PerWebRequest L’istanza è condivisa nella stessa request request(richiede HttpModule)
LifestyleType.Pooled Le istanze transienti possono essere riciclate
LifestyleType.Singleton L’istanza è unica (default)
LifestyleType.Thread L’istanza è condivisa nello stesso thread
LifestyleType.Transient L’istanza viene creata ad ogni richiesta
http://www.castleproject.org/container/documentation/v21/usersguide/lifestyles.html
{demo}
Castle - FacilitiesIntegration Basic Services Semantic
ActiveRecord Logging EventiWiring
Aspect# Automatic Transaction FactorySupport
iBatisNet TypedFactory
NHibernate Startable
Prevalence OnCreate
Db4o
Remoting
WCF Integration
http://www.castleproject.org/container/facilities/trunk/index.html
{demo}
{grazie}
andreabalducci