REQUEST A DEMO

Windsor Container Property Setter Injection

 

 

Hammet pointed out in the comments for Creating a simple Windsor facility that the whole premise of my TimeServiceFacility is silly.

 

Sorry, but I still don’t get it why you used a facility to do that. You could get the same behavior by

 

– registering the timeservice as, well, a service

– having writable properties on components, like you do on HasTimeServiceProperty class.

 

it will be “injected” in the same way.

 

Interesting exercise, though.

 

This post is an attempt to correct a wrong. Windsor will do Property setter injection out of the box. Just follow what Hammet said to do a viola you get the same thing.

 

Add the TimeService as service component to the container:

 

      Container.Current.AddComponent('timeservice', typeof(ITimeService), typeof(TimeService));
Here is a test that proves setter injection is working for components resolved from the container having a writeable ITimeService property:
 [Test]
   public void A_container_component_with_a_TimeService_property_should_have_the_TimeService_property_set
_by_the_container()
   {
   Container.Current.AddComponent("has.timeservice", typeof(HasTimeServiceProperty));
    
   HasTimeServiceProperty hasTimeServiceProperty = Container.Resolve();
   
  hasTimeServiceProperty.TimeService.ShouldNotBeNull();
   }
   
   public class HasTimeServiceProperty
   {
       private ITimeService _timeService;
    
       public ITimeService TimeService
       {
         get { return _timeService; }
         set { _timeService = value; }
      }
   }

 

Feeling the love

 

I just wanted to take a moment to thank the community surrounding the Castle Project. You can tell how much people love using this framework by how much they are willing to pitch in to help each other out. Just take a browse around theuser or developer groups and forums or IRC to find committers and users alike taking time to help each other use and make the project better, not to mention taking the time to point out fundamental flaws in blog posts.