Dashboard > Castle Contrib > Home > Castle.Facilities.OptionalPropertyInjection
Log In   View a printable version of the current page.
Castle.Facilities.OptionalPropertyInjection
Added by George Mauer, last edited by George Mauer on Aug 02, 2009  (view change)
Labels: 
(None)


Introduction

By default Castle Windsor attempts to resolve and inject any properties with public seters. This is not always a desirable functionality. Castle.Facilities.OptionalPropertyInjection allows you to define policies for which properties which will be injected and which will be ignored.

Building

To build from source:

  • svn co [https://svn.castleproject.org/svn/castlecontrib/facilities/trunk/src/OptionalPropertyInjection /]
  • cd OptionalPropertyInjection
  • Build.bat

The binaries will be placed into the build\bin\ folder.  You must use a versioin of castle that exposes that Castle.Microkernel.Registration namespace

Usage

To enable the facility, you need to add the facilities to your container configuration.

It can be added via xml

<castle>
    <facilities>
        <facility id="OptionalPropertyInjection.Facility"
             type="Castle.Facilities.OptionalPropertyInjection.OptionalPropertyInjectionFacility, Castle.Facilities.OptionalPropertyInjection "
             provideByDefault="true"/>
    </facilities>
</castle>

or via code

container..AddFacility("OptionalPropertyInjection.Facility", new OptionalPropertyInjectionFacility(true));

Where the constructor parameter sets whether properties are injected by default. The default is true and indicates that castle should automatically try to inject all properties like it normally does.

Setting up whether all of a specific component's properties or even specific properties on a component get injected can be done via xml:

<component id="question-to-life"
    type="Castle.Facilities.OptionalPropertyInjection.Tests.QuestionOfLifeUniverseAndEverything, Castle.Facilities.OptionalPropertyInjection.Tests">
        <wire-properties value="false">
            <except propertyName=""TheAnswer"" />
        </wire-properties>
</component>

or the Castle.Microkernel.Registration DSL

container.Register(
  Component.For<QuestionOfLifeUniverseAndEverything>().WireProperties(o=>o.None().Except(x=>x.TheAnswer))
);

or

container.Register(
  Component.For<QuestionOfLifeUniverseAndEverything>().WireProperties(o=>o.None().Except("TheAnswer"))
);

it is also possible to set policies when using the dsl to register many components at the same time

container.Register(
  AllTypes.FromAssemblyContaining<QuestionOfLifeUniverseAndEverything>().Where(x => true)
    .WireProperties(o => o
      .OnType<QuestionOfLifeUniverseAndEverything>().None().Except(x=>x.TheAnswer)
      .AndOnType<QuestionToBeOrNotToBe>().All().Except(x=>x.TheAnswerToTheQuestion))
);


Open Issues

  • Cannot set property injection policies when components are not wired individually ie. container.Register(AllTypes.ForAssemblyContaining<QuestionOfLifeUniverseAndEverything>());
  • If wire-up policies are defined in xml but the facility is added in code policies will not be applied.

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators