Your SlideShare is downloading. ×
castle-windsor-ioc-demo
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

castle-windsor-ioc-demo

680
views

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
680
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. AMIR BARYLKO IOC INTRO II Using Castle Windsor Anvil Digital Aug 2010Tuesday, August 17, 2010
  • 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. 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. CONFIGURE WINDSOR • private void SetupContainer() • { • this.Container = new WindsorContainer(); • // add more configuration here • }Tuesday, August 17, 2010
  • 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. ADD STORAGE • Singleton (default) • Component.For<IMediaLibraryStorage>().Instance(new NHMediaLibraryStorage("c:/temp/movies.db")Tuesday, August 17, 2010
  • 7. ADD MEDIALIBRARY • Singleton (default) • Component • .For<IMediaLibrary() • .ImplementedBy<SimpleMediaLibrary>()Tuesday, August 17, 2010
  • 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. MOVIE FACTORY • Create class to handle movie creation • public interface IMovieFactory • { • IMovie Create(string title); • }Tuesday, August 17, 2010
  • 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. CONFIGURE CONTAINER • Component.For<IWindsorContainer>() • .Instance(this.Container) • Component.For<IMovieFactory>() • .ImplementedBy<MovieFactory>(), • Component.For<IMovie>() • .ImplementedBy<Movie>() • .LifeStyle.TransientTuesday, August 17, 2010
  • 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. ADD MOVIE CONSTRUCTOR • public Movie() • { • } • public Movie(string title) • { • this.Title = title; • }Tuesday, August 17, 2010
  • 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

×