AMIR BARYLKO                    ADVANCED IOC                      WINDSOR                       CASTLE                    ...
FIRST STEPS                                Why IoC containers                              Registering Components         ...
WHY USE IOC  • Manage           creation and disposing objects  • Avoid        hardcoding dependencies  • Dependency      ...
REGISTRATION  • In   order to resolve instances, we need to register them  Container.Register(         // Movie class regi...
GENERICS  •What If I want to register a generic class?  container.Register(         Component                .For(typeof(I...
DEPENDENCIES        Component          .For<IMovieFactory>()          .ImplementedBy<NHMovieFactory>(),        Component  ...
LIFESTYLE  • Singleton      vs Transient  • Which       one is the default? And for other IoC tools?  container.Register( ...
RELEASING  • Do     I need to release the instances?Amir Barylko - Advanced IoC                   MavenThought Inc.
NAMING  • Who’s       the one resolved?  container.Register(         Component            .For<IMovie>()            .Imple...
NAMING II  • Assign     unique names to registration  container.Register(      ...         Component             .For<IMov...
JOGGING                                   Installers                              Using Conventions                       ...
INSTALLERS  • Where        do we put the registration code?  • Encapsulation  • Partition      logic  • Easy    to maintai...
INSTALLER EXAMPLE  container.Install(   new EntitiesInstaller(),   new RepositoriesInstaller(),   // or use FromAssembly! ...
XML CONFIG      var res = new AssemblyResource("assembly://.../      ioc.xml")  container.Install(     Configuration.FromA...
CONVENTIONS  Classes       .FromAssemblyContaining<IMovie>()       .BasedOn<IMovie>()       .WithService.Base() // Registe...
CONFIGURE COMPONENTS  Classes     .FromAssemblyContaining<IMovie>()     .BasedOn<IMovie>()      .LifestyleTransient()     ...
DEPENDS ON  var rtKey = @"the key goes here";  container.Register(     Component      .For<IMovieFactory>()      .Implemen...
SERVICE OVERRIDE  container.Register(    Component        .For<IMovieFactory>()        .ImplementedBy<IMDBMovieFactory>() ...
RUN FOREST! RUN!                                   Startable Facility                              Interface Based Factori...
STARTABLE FACILITY  • Allows      objects to be started when they are created  • And     stopped when they are released  •...
STARTABLE CLASS  public interface IStartable  {      void Start();      void Stop();  }  var container = new WindsorContai...
FACTORIES  • Common          pattern to create objects  • But    the IoC is some kind of factory too...  • Each     factor...
TYPED FACTORIES  • Create      a factory based on an interface  • Methods        that return values are Resolve methods  •...
REGISTER FACTORY  Kernel.AddFacility<TypedFactoryFacility>();  Register(      Component         .For<IMovie>()         .Im...
CASTLE AOP  • Inject    code around methods  • Cross     cutting concerns  • Avoid      mixing modelling and usage  • Avoi...
INTERCEPTORS  Register(      Component          .For<LoggingInterceptor>()          .LifeStyle.Transient,      Component  ...
LOGGING  public class LoggingInterceptor : IInterceptor  {      public void Intercept(IInvocation invocation)      {      ...
NOTIFY PROPERTY CHANGED  Register(      Component          .For<NotifyPropertyChangedInterceptor>()          .LifeStyle.Tr...
QUESTIONS?Amir Barylko - TDD                MavenThought Inc.
RESOURCES  • Contact: amir@barylko.com, @abarylko  • Code      & Slides: http://www.orthocoders.com/presentations  • Castl...
Upcoming SlideShare
Loading in...5
×

ioc-castle-windsor

1,184

Published on

Presentation done at the .net UG about advanced features of IoC using Castle Windsor

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,184
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ioc-castle-windsor

  1. 1. AMIR BARYLKO ADVANCED IOC WINDSOR CASTLE DOT NET UG NOV 2011Amir Barylko - Advanced IoC MavenThought Inc.
  2. 2. FIRST STEPS Why IoC containers Registering Components LifeStyles NamingAmir Barylko - Advanced IoC MavenThought Inc.
  3. 3. WHY USE IOC • Manage creation and disposing objects • Avoid hardcoding dependencies • Dependency injection • Dynamically configure instances • Additional featuresAmir Barylko - Advanced IoC MavenThought Inc.
  4. 4. REGISTRATION • In order to resolve instances, we need to register them Container.Register( // Movie class registered Component.For<Movie>(), // IMovie implementation registered Component.For<IMovie>().ImplementedBy<Movie>() );Amir Barylko - Advanced IoC MavenThought Inc.
  5. 5. GENERICS •What If I want to register a generic class? container.Register( Component .For(typeof(IRepository<>) .ImplementedBy(typeof(NHRepository<>) );Amir Barylko - Advanced IoC MavenThought Inc.
  6. 6. DEPENDENCIES Component .For<IMovieFactory>() .ImplementedBy<NHMovieFactory>(), Component .For<IMovieRepository>() .ImplementedBy<SimpleMovieRepository>() public class SimpleMovieRepository : IMovieRepository { public SimpleMovieRepository(IMovieFactory factory) { _factory = factory; } }Amir Barylko - Advanced IoC MavenThought Inc.
  7. 7. LIFESTYLE • Singleton vs Transient • Which one is the default? And for other IoC tools? container.Register( Component.For<IMovie>() .ImplementedBy<Movie>() .LifeStyle.Transient );Amir Barylko - Advanced IoC MavenThought Inc.
  8. 8. RELEASING • Do I need to release the instances?Amir Barylko - Advanced IoC MavenThought Inc.
  9. 9. NAMING • Who’s the one resolved? container.Register( Component .For<IMovie>() .ImplementedBy<Movie>(), Component .For<IMovie>() .ImplementedBy<RottenTomatoesMovie>() );Amir Barylko - Advanced IoC MavenThought Inc.
  10. 10. NAMING II • Assign unique names to registration container.Register( ... Component .For<IMovie>() .ImplementedBy<RottenTomatoesMovie>() .Named("RT") ); container.Resolve<IMovie>("RT");Amir Barylko - Advanced IoC MavenThought Inc.
  11. 11. JOGGING Installers Using Conventions Depend OnAmir Barylko - Advanced IoC MavenThought Inc.
  12. 12. INSTALLERS • Where do we put the registration code? • Encapsulation • Partition logic • Easy to maintainAmir Barylko - Advanced IoC MavenThought Inc.
  13. 13. INSTALLER EXAMPLE container.Install( new EntitiesInstaller(), new RepositoriesInstaller(), // or use FromAssembly! FromAssembly.This(), FromAssembly.Named("MavenThought...."), FromAssembly.Containing<ServicesInstaller>(), FromAssembly.InDirectory(new AssemblyFilter("...")), FromAssembly.Instance(this.GetPluginAssembly()) );Amir Barylko - Advanced IoC MavenThought Inc.
  14. 14. XML CONFIG var res = new AssemblyResource("assembly://.../ ioc.xml") container.Install( Configuration.FromAppConfig(), Configuration.FromXmlFile("ioc.xml"), Configuration.FromXml(res) );Amir Barylko - Advanced IoC MavenThought Inc.
  15. 15. CONVENTIONS Classes .FromAssemblyContaining<IMovie>() .BasedOn<IMovie>() .WithService.Base() // Register the service .LifestyleTransient() // Transient lifestyleAmir Barylko - Advanced IoC MavenThought Inc.
  16. 16. CONFIGURE COMPONENTS Classes .FromAssemblyContaining<IMovie>() .BasedOn<IMovie>() .LifestyleTransient() // Using naming to identify instances .Configure(r => r.Named(r.Implementation.Name))Amir Barylko - Advanced IoC MavenThought Inc.
  17. 17. DEPENDS ON var rtKey = @"the key goes here"; container.Register( Component .For<IMovieFactory>() .ImplementedBy<RottenTomatoesFactory>()    .DependsOn(Property.ForKey("apiKey").Eq(rtKey)) ); .DependsOn(new { apiKey = rtKey } ) // using anonymous class .DependsOn( new Dictionary<string,string>{ {"APIKey", twitterApiKey}}) // using dictionaryAmir Barylko - Advanced IoC MavenThought Inc.
  18. 18. SERVICE OVERRIDE container.Register(   Component .For<IMovieFactory>() .ImplementedBy<IMDBMovieFactory>() .Named(“imdbFactory”)   Component .For<IMovieRepository>() .ImplementedBy<SimpleMovieRepository>()      .DependsOn(Dependency.OnComponent("factory", "imdbFactory")) );Amir Barylko - Advanced IoC MavenThought Inc.
  19. 19. RUN FOREST! RUN! Startable Facility Interface Based Factories Castle AOPAmir Barylko - Advanced IoC MavenThought Inc.
  20. 20. STARTABLE FACILITY • Allows objects to be started when they are created • And stopped when they are released • Start and stop methods have to be public, void and no parameters • You can use it with POCO objects specifying which method to use to start and stopAmir Barylko - Advanced IoC MavenThought Inc.
  21. 21. STARTABLE CLASS public interface IStartable { void Start(); void Stop(); } var container = new WindsorContainer() .AddFacility<StartableFacility>() .Register( Component .For<IThing>() .ImplementedBy<StartableThing>() );Amir Barylko - Advanced IoC MavenThought Inc.
  22. 22. FACTORIES • Common pattern to create objects • But the IoC is some kind of factory too... • Each factory should use the IoC then.... • Unless we use Windsor!!!!Amir Barylko - Advanced IoC MavenThought Inc.
  23. 23. TYPED FACTORIES • Create a factory based on an interface • Methods that return values are Resolve methods • Methods that are void are Release methods • Collection methods resolve to multiple componentsAmir Barylko - Advanced IoC MavenThought Inc.
  24. 24. REGISTER FACTORY Kernel.AddFacility<TypedFactoryFacility>(); Register( Component .For<IMovie>() .ImplementedBy<NHMovie>() .LifeStyle.Transient, Component.For<IMovieFactory>().AsFactory() );Amir Barylko - Advanced IoC MavenThought Inc.
  25. 25. CASTLE AOP • Inject code around methods • Cross cutting concerns • Avoid mixing modelling and usage • Avoid littering the code with new requirementsAmir Barylko - Advanced IoC MavenThought Inc.
  26. 26. INTERCEPTORS Register( Component .For<LoggingInterceptor>() .LifeStyle.Transient, Component .For<IMovie>() .ImplementedBy<NHMovie>() .Interceptors(InterceptorReference .ForType<LoggingInterceptor>()).Anywhere .LifeStyle.Transient );Amir Barylko - Advanced IoC MavenThought Inc.
  27. 27. LOGGING public class LoggingInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { Debug.WriteLine("Before execution"); invocation.Proceed(); Debug.WriteLine("After execution"); } }Amir Barylko - Advanced IoC MavenThought Inc.
  28. 28. NOTIFY PROPERTY CHANGED Register( Component .For<NotifyPropertyChangedInterceptor>() .LifeStyle.Transient, Component .For<IMovieViewModel>() .ImplementedBy<MovieViewModel>() .Interceptors(InterceptorReference .ForType<NotifyPropertyChangedInterceptor>()) .Anywhere .LifeStyle.Transient );Amir Barylko - Advanced IoC MavenThought Inc.
  29. 29. QUESTIONS?Amir Barylko - TDD MavenThought Inc.
  30. 30. RESOURCES • Contact: amir@barylko.com, @abarylko • Code & Slides: http://www.orthocoders.com/presentations • Castle Project Doc: http://docs.castleproject.orgAmir Barylko - Advanced IoC MavenThought Inc.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×