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

风流意气都作罢 提交于 2019-11-30 11:59:06
Bittercoder

You can use the Castle.Core.DoNotWireAttribute attribute to stop a property from being wired up by the IoC container (this is in the Castle.Core assembly, which means your library only needs to take a dependency on the lightweight Castle.Core assembly - if for example you want to use the code without an inversion of control container altogether, or in a different IoC container).

I don't believe there's any way to prevent wiring from occurring in the Xml configuration, but it would be reasonably easy to add support for this - if I had to do this I would probably:

  1. Introduce some kind of attribute on the property declaration in the xml: <myprop wire="false" />
  2. Inherit from PropertiesDependenciesModelInspector, overriding the InspectProperties method to apply some additional logic to identifying which properties should be added as dependencies to the components model (inspecting the model.Configuration for the wire="false" attribute/value pair).
  3. Inherit from DefaultComponentModelBuilder and override the InitializeContributors to include your replacement PropertiesDependenciesModelInspector - or just remove the existing properties contributor and add your own at run time via the AddContributor/RemoveContributor methods.
  4. Replace the ComponentModelBuilder service instance assigned to the kernel of your container.

Another approach which could work for you is to just manually remove the dependencies from the model before any instances of the service are requested ie.

kernel.GetHandler(typeof(MyComponent)).ComponentModel.Dependencies.RemoveAll(d => d.DependencyKey == "PropertyThatShouldNotBeWired");

YMMV with that approach though - especially if you have startable services or other facilities which may be eagerly instantiating your component after it's registered.

I created a facility to help with this:

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'.

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)

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!