Codemash-advanced-ioc-castle-windsor
Upcoming SlideShare
Loading in...5
×
 

Codemash-advanced-ioc-castle-windsor

on

  • 531 views

Presentation done at Codemash about using Castle Windsor IoC container

Presentation done at Codemash about using Castle Windsor IoC container

Statistics

Views

Total Views
531
Slideshare-icon Views on SlideShare
531
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Codemash-advanced-ioc-castle-windsor Codemash-advanced-ioc-castle-windsor Presentation Transcript

    • AMIR BARYLKO ADVANCED IOC WINDSOR CASTLEAmir Barylko - Advanced IoC MavenThought Inc.
    • FIRST STEPS Why IoC containers Registering Components LifeStyles NamingAmir Barylko - Advanced IoC MavenThought Inc.
    • WHY USE IOC • Manage creation and disposing objects • Avoid hardcoding dependencies • Dependency injection • Dynamically configure instances • Additional featuresAmir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • 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.
    • 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.
    • 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.
    • 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>() .ImplementedBy<Movie>(), Component .For<IMovie>() .ImplementedBy<RottenTomatoesMovie>() );Amir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • JOGGING Installers Using Conventions Depend OnAmir Barylko - Advanced IoC MavenThought Inc.
    • INSTALLERS • Where do we put the registration code? • Encapsulation • Partition logic • Easy to maintainAmir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • 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.
    • CONVENTIONS Classes .FromAssemblyContaining<IMovie>() .BasedOn<IMovie>() .WithService.Base() // Register the service .LifestyleTransient() // Transient lifestyleAmir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • 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.
    • 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.
    • RUN FOREST! RUN! Startable Facility Interface Based Factories Castle AOPAmir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • 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.
    • 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.
    • 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.
    • REGISTER FACTORY Kernel.AddFacility<TypedFactoryFacility>(); Register( Component .For<IMovie>() .ImplementedBy<NHMovie>() .LifeStyle.Transient, Component.For<IMovieFactory>().AsFactory() );Amir Barylko - Advanced IoC MavenThought Inc.
    • 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.
    • 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.
    • 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.
    • 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.
    • QUESTIONS?Amir Barylko - TDD MavenThought Inc.
    • 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.