Windsor Container: How to specify a public property should not be filled by the container?

后端 未结 7 1877
小鲜肉
小鲜肉 2021-01-01 23:32

When Instantiating a class, Windsor by default treats all public properties of the class as optional dependencies and tries to satisfy them. In my case, this creates a rath

相关标签:
7条回答
  • 2021-01-01 23:41

    Posted this on the google groups forum too here: http://groups.google.com/group/castle-project-devel/browse_thread/thread/43aa513817bd057a

    0 讨论(0)
  • 2021-01-01 23:43

    I created a facility to help with this:

    • Castle.Facilities.OptionalPropertyInjection
    0 讨论(0)
  • 2021-01-01 23:46

    I do not know which version of Castle you guys were using at that time, but none of the solution mentioned were working. Plus, there is a lot of dead links.

    With castle 3.1, here the solution I came up with (thanks to some castle source code digging):

    container.Register(Component.For(type)
                                            .LifestyleTransient()
                                            .Properties( propertyInfo => propertyInfo.PropertyType != typeof(MyOtherType)));
    

    The 'Properties' function adds a property filter used by castle when constructing the ComponentModel. In my case, all properties dependency will be satisfied except the property type 'MyOtherType'.

    0 讨论(0)
  • 2021-01-01 23:49

    This can be achieved by the following code:

    var container = new WindsorContainer();
    
    // We don't want to inject properties, only ctors
    var propInjector = container.Kernel.ComponentModelBuilder
                             .Contributors
                             .OfType<PropertiesDependenciesModelInspector>()
                             .Single();
    container.Kernel.ComponentModelBuilder.RemoveContributor(propInjector);
    

    Source Castle Windsor Documentation

    0 讨论(0)
  • 2021-01-01 23:55

    DoNotWireAttribute

    • Class: http://svn.castleproject.org:8080/svn/castle/trunk/Core/Castle.Core/Attributes/DoNotWireAttribute.cs
    • Test: http://svn.castleproject.org:8080/svn/castle/trunk/InversionOfControl/Castle.Windsor.Tests/IgnoreWireTestCase.cs
    0 讨论(0)
  • 2021-01-02 00:01

    Maybe it will be helpful for someone. In Windsor 4.1 there is PropertiesIgnore method during registration.

    Component.For<Role>().LifestyleTransient().PropertiesIgnore((model, propertyInfo) => true)
    
    0 讨论(0)
提交回复
热议问题