Introduzione ai framework ioc

16
14° Workshop – DotNetMarche :: Castelfidardo, 16 Aprile 2010 Introduzione ai framework per IoC / DI e AOP

description

Seconda sessione del 14° workshop dotnetmarche.org

Transcript of Introduzione ai framework ioc

Page 1: Introduzione ai framework ioc

14° Workshop – DotNetMarche :: Castelfidardo, 16 Aprile 2010

Introduzione ai framework per IoC / DI e AOP

Page 2: Introduzione ai framework ioc

Andrea Balducci

mtb.snowboard @ gmail.com

http://www.ienumerable.it

Riferimenti

andreabalducci

Page 3: Introduzione ai framework ioc

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/

Page 4: Introduzione ai framework ioc

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/

Page 5: Introduzione ai framework ioc

Definiamo un contratto

public interface IOperazioneMatematica{

int Esegui(int a, int b);}

Page 6: Introduzione ai framework ioc

Implementazione

public class OperazioneSomma : IOperazioneMatematica{ public int Esegui(int a, int b) { return a + b; }}

Page 7: Introduzione ai framework ioc

Configurazione del container

// Creazione containerIUnityContainer container = new UnityContainer();

// Registrazione componenticontainer.RegisterType<

IOperazioneMatematica, // contrattoOperazioneSomma // implementazione

>();

Page 8: Introduzione ai framework ioc

Utilizzo del container

// Richiesta del servizio di calcolovar operazione =

container.Resolve<IOperazioneMatematica>();

// Utilizzo del servizioint risultato = operazione.Esegui(1, 2);

Page 9: Introduzione ai framework ioc

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

Page 10: Introduzione ai framework ioc

{demo}

Page 11: Introduzione ai framework ioc

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

Page 12: Introduzione ai framework ioc

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

Page 13: Introduzione ai framework ioc

{demo}

Page 14: Introduzione ai framework ioc

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

Page 15: Introduzione ai framework ioc

{demo}

Page 16: Introduzione ai framework ioc

{grazie}

andreabalducci