Categories
askquestion

Castle Windsor: How to specify a runtime value as a parameter (E.g. value returned from static function call)

Castle Windsor: How to specify a runtime value as a parameter (E.g. value returned from static function call)

Ask Question

Asked
11 years, 1 month ago

Active
2 years, 10 months ago

Viewed
2k times

.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}

2

I want to perform this CODE equivlant in the castle xml config file.

// Foo(string name)

IFoo f = new Foo(StaticBarClass.Name);

XML

Now for the XML, I know everything (e.g. the blah) except for the stuff inside the parameter part.

What would the parameter part look like?

<component id=”blah”
service=”blah”
type=”blah”>
<parameters>
<name>StaticBarClas.Name_THAT_I_NEED_HELP_WITH</name>
</parameters>

runtime castle-windsor parameters

share|improve this question

edited Dec 19 ’08 at 18:22

James Curran

90.7k3131 gold badges164164 silver badges248248 bronze badges

asked Dec 19 ’08 at 18:09

ClintClint

add a comment
 | 

2 Answers
2

active

oldest

votes

1

One approach you could use is to replace the configuration parameters inspector with your own variant that can introduce some additional behaviour – here’s a quick prototype:

public class ExtendedConfigurationParametersInspector : IContributeComponentModelConstruction
{
#region IContributeComponentModelConstruction Members

public virtual void ProcessModel(IKernel kernel, ComponentModel model)
{
if (model.Configuration == null) return;

IConfiguration parameters = model.Configuration.Children[“parameters”];

if (parameters == null) return;

foreach (IConfiguration parameter in parameters.Children)
{
String name = parameter.Name;
String value = parameter.Value;

if (value == null && parameter.Children.Count != 0)
{
IConfiguration parameterValue = parameter.Children[0];
model.Parameters.Add(name, parameterValue);
}
else
{
if (parameter.Attributes[“type”] == “static”)
{
int lastIndex = parameter.Value.LastIndexOf(“.”);
string typeName = parameter.Value.Substring(0, lastIndex);
string field = parameter.Value.Substring(lastIndex + 1);
Type ownerType = Type.GetType(typeName);
FieldInfo valueField = ownerType.GetField(field);
value = (string) valueField.GetValue(null);
}

model.Parameters.Add(name, value);
}
}

foreach (ParameterModel parameter in model.Parameters)
{
if (parameter.Value == null || !ReferenceExpressionUtil.IsReference(parameter.Value))
{
continue;
}

String newKey = ReferenceExpressionUtil.ExtractComponentKey(parameter.Value);

model.Dependencies.Add(new DependencyModel(DependencyType.ServiceOverride, newKey, null, false));
}
}

#endregion
}

public class ExtendedComponentBuilder : DefaultComponentModelBuilder
{
public ExtendedComponentBuilder(IKernel kernel) : base(kernel)
{
}

protected override void InitializeContributors()
{
AddContributor(new GenericInspector());
AddContributor(new ConfigurationModelInspector());
AddContributor(new ExtendedConfigurationParametersInspector());
AddContributor(new LifestyleModelInspector());
AddContributor(new ConstructorDependenciesModelInspector());
AddContributor(new PropertiesDependenciesModelInspector());
AddContributor(new LifecycleModelInspector());
AddContributor(new InterceptorInspector());
AddContributor(new ComponentActivatorInspector());
AddContributor(new ComponentProxyInspector());
}
}

public class ExtendedWindsorContainer : WindsorContainer
{
public ExtendedWindsorContainer(IConfigurationInterpreter interpreter)
: base(CreateKernel(), new Castle.Windsor.Installer.DefaultComponentInstaller())
{
if (interpreter == null) throw new ArgumentNullException(“interpreter”);

interpreter.ProcessResource(interpreter.Source, Kernel.ConfigurationStore);

RunInstaller();
}

private static IKernel CreateKernel()
{
DefaultKernel kernel = new DefaultKernel();
kernel.ComponentModelBuilder = new ExtendedComponentBuilder(kernel);
return kernel;
}
}

You could then wire up the properties in your container like so, where specifying a type of “static” for the parameter would cause the value to be replaced with the static field that was referenced by the parameters value.

<castle>
<components>
<component id=”test”
type=”SomeNamespace.TestComponent,Example”>
<parameters>
<value type=”static”>SomeNamespace.SomeClass.TheStaticFieldValue</value>
</parameters>
</component>
</components>
</castle>

Unfortunately you generally can’t do this through simpler means (such as the model created kernel event) due to the fact that parameters in the component model are immutable.

share|improve this answer

edited Jan 21 ’09 at 0:38

answered Dec 21 ’08 at 11:00

BittercoderBittercoder

10k99 gold badges5151 silver badges7575 bronze badges

add a comment
 | 

1

You can’t set it up like that from xml alone. But you could use the factory facility to do this.

share|improve this answer

edited Apr 10 ’17 at 20:58

craigdfrench

67877 silver badges1616 bronze badges

answered Dec 19 ’08 at 19:20

Mauricio SchefferMauricio Scheffer

94.2k1818 gold badges184184 silver badges271271 bronze badges

add a comment
 | 

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Submit

Post as a guest

Name

Email
Required, but never shown

Post Your Answer

Discard

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Blog

Podcast: Make my Monolith a Micro

When laziness is efficient: Make the most of your command line

Featured on Meta

TLS 1.0 and TLS 1.1 removal for Stack Exchange services

Did Stack Exchange cut the number of negative comments nearly in half between…

An account of my meeting with the Stack Overflow management team

Linked

0

Castle Windsor web.config parameters

1

How to setup Windsor component that needs to depend on a dynamic string?

Related

576How do you pass a function as a parameter in C?1Castle-Windsor swapping service at runtime2316Set a default parameter value for a JavaScript function1Castle Windsor: auto-register types that have dependency in constructor1How to register a uri dependency to return HttpContext.Current.Request.Url using Castle Windsor?593Get url parameter jquery Or How to Get Query String Values In js1Castle Windsor – List of complex types as parameters to component in xml15How can I get started with ASP.NET (5) Core and Castle Windsor for Dependency Injection?2How to register all WCF service implementations in Castle Windsor2How to implement proper Di with WPF app using Castle Windsor

Hot Network Questions

How does the Contingency spell with Cure Wounds as the contingent spell work with incoming damage?

In the UK parliament, how much of Prime Minister’s Questions is pre-planned?

Long crack in windshield: What could have been the cause and what can I do?

How do I avoid a temporary short circuit from destroying the PI?

On The Subject of Blind Alleys

Did Lucille Ball have a creative influence on Star Trek?

Why is DNS apparently involved in issuing “dir” on Windows 10?

How do helicopters avoid detection from enemies by using IR suppressors?

Writing a build script with if statements and function calls

Are mechanical walking vehicles useful in combat?

Is there any benefit to partioning hdd data-only disk?

Who does the money go to if “everyone” in a family dies?

Does Iran’s sanction play a pivotal role in rejecting papers from a journal?

Does coiling and straightening a wire change its resistance?

Article of Abbreviations

Is this a triangle?

Lonely Multiplication

What does “D.” stand for?

Recovering from stumbling over words

How to avoid or mitigate heavy science lingo and “technobabble” in a science fiction story?

Flexure of a Grid

Did Nancy Pelosi say “We’ll have to pass the bill to find out what’s in it”?

At what point does an aircraft become an airliner?

Does the Clone Bay regenerate your crew’s health when you “wait” at The Last Stand?

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Leave a Reply

Your email address will not be published. Required fields are marked *