AMIR BARYLKO                            IOC INTRO II                            Using Castle Windsor                      ...
WINDSOR TESTS    •   Register and Retrieve    •   Register a particular instance    •   Register Transient    •   Construc...
ADD WINDSOR CONFIG    • Modify               Global.asax         • //     Setup IoC container         • this.SetupContaine...
CONFIGURE WINDSOR    • private void SetupContainer()    •      {    •         this.Container = new WindsorContainer();    ...
ADD CONTROLLERS    • Use          Name to identify them    • They            have to be transient         • Component.For<...
ADD STORAGE    • Singleton            (default)         • Component.For<IMediaLibraryStorage>().Instance(new             N...
ADD MEDIALIBRARY    • Singleton            (default)         • Component              • .For<IMediaLibrary()              ...
REFACTORING HARD                             DEPENDENCIES    • MoviesController         • Hard            dependency from ...
MOVIE FACTORY    • Create               class to handle movie creation         •          public interface IMovieFactory  ...
MOVIES CONTROLLER    •   Write test    •   Inject factory in the constructor    •   Change creation by using the factory  ...
CONFIGURE CONTAINER    •   Component.For<IWindsorContainer>()              •   .Instance(this.Container)    •   Component....
IMPLEMENT FACTORY    •          public MovieFactory(IWindsorContainer container)    •          {    •              _contai...
ADD MOVIE CONSTRUCTOR    •                 public Movie()    •                 {    •                 }    •              ...
RESOURCES    • Src: http://github.com/amirci/IoC-Intro-2    • Windsor: http://using.castleproject.org/display/IoC/Fluent  ...
Upcoming SlideShare
Loading in...5
×

castle-windsor-ioc-demo

712

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
712
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

castle-windsor-ioc-demo

  1. 1. AMIR BARYLKO IOC INTRO II Using Castle Windsor Anvil Digital Aug 2010Tuesday, August 17, 2010
  2. 2. WINDSOR TESTS • Register and Retrieve • Register a particular instance • Register Transient • Constructor resolution • Retrieve with parameters • Retrieve instance by name • Retrieve all instancesTuesday, August 17, 2010
  3. 3. ADD WINDSOR CONFIG • Modify Global.asax • // Setup IoC container • this.SetupContainer(); • // Register the factory for the controllers • ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(this.Container));Tuesday, August 17, 2010
  4. 4. CONFIGURE WINDSOR • private void SetupContainer() • { • this.Container = new WindsorContainer(); • // add more configuration here • }Tuesday, August 17, 2010
  5. 5. ADD CONTROLLERS • Use Name to identify them • They have to be transient • Component.For<MoviesController>().Named ("MoviesController").LifeStyle.Transient, • Component.For<HomeController>().Named ("HomeController").LifeStyle.Transient,Tuesday, August 17, 2010
  6. 6. ADD STORAGE • Singleton (default) • Component.For<IMediaLibraryStorage>().Instance(new NHMediaLibraryStorage("c:/temp/movies.db")Tuesday, August 17, 2010
  7. 7. ADD MEDIALIBRARY • Singleton (default) • Component • .For<IMediaLibrary() • .ImplementedBy<SimpleMediaLibrary>()Tuesday, August 17, 2010
  8. 8. REFACTORING HARD DEPENDENCIES • MoviesController • Hard dependency from MoviesController to create a movie • public ActionResult Create(string title) •{ • this._library.Add(new Movie { Title = title }); • return Redirect("Index"); •}Tuesday, August 17, 2010
  9. 9. MOVIE FACTORY • Create class to handle movie creation • public interface IMovieFactory • { • IMovie Create(string title); • }Tuesday, August 17, 2010
  10. 10. MOVIES CONTROLLER • Write test • Inject factory in the constructor • Change creation by using the factory • [AcceptVerbs(HttpVerbs.Post)] • public ActionResult Create(string title) • { • this._library.Add(this._factory.Create(title)); • return Redirect("Index"); • }Tuesday, August 17, 2010
  11. 11. CONFIGURE CONTAINER • Component.For<IWindsorContainer>() • .Instance(this.Container) • Component.For<IMovieFactory>() • .ImplementedBy<MovieFactory>(), • Component.For<IMovie>() • .ImplementedBy<Movie>() • .LifeStyle.TransientTuesday, August 17, 2010
  12. 12. IMPLEMENT FACTORY • public MovieFactory(IWindsorContainer container) • { • _container = container; • } • public IMovie Create(string title) • { • return this._container.Resolve<IMovie>(new Dictionary<string, object>() • { • { "title", title } • }); • }Tuesday, August 17, 2010
  13. 13. ADD MOVIE CONSTRUCTOR • public Movie() • { • } • public Movie(string title) • { • this.Title = title; • }Tuesday, August 17, 2010
  14. 14. RESOURCES • Src: http://github.com/amirci/IoC-Intro-2 • Windsor: http://using.castleproject.org/display/IoC/Fluent +Registration+API • BDD Intro: http://www.orthocoders.com/Tuesday, August 17, 2010
  1. A particular slide catching your eye?

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

×