Mixing external configuration with fluent registration

83 views
Skip to first unread message

pmcg

unread,
Aug 12, 2010, 1:26:10 PM8/12/10
to Castle Project Users
I'm trying to figure out how I can reference external configuration
properties (i.e. Database connection strings, log paths etc.), which
will be constructor parameters for components I am registering with
the fluent registration. When using xml configuration I would simply
have configured them as properties within the configuration file. When
configuring a component I would simply have configured the constructor
parameter using the #{propertyname} type syntax. I have tried this
with Depends and Parameters methods, but neither have worked.
Is it possible to use property values that have been defined within a
configuration file when the container was being created? Or are the
properties just available when constructing the container from the
configuration file?
I have seen examples where a configuration component is configured
within the configuration file and can then be passed as a constructor
parameter to other components that depend on the properties. This
requires one or more configuration types, which I would prefer not to
use. Would be nice to just inject the property values which are very
often distinct configuration items.

See these for examples of such configuration holding types
http://mikehadlow.blogspot.com/2010/02/10-advanced-windsor-tricks-9.html
http://ayende.com/Blog/archive/2008/12/31/didja-know-merging-windsor-configuration-with-automatic-registration.aspx

I can always fall back on programmatically accessing properties from
the app settings section of my application configuration file when
doing my fluent registration, but if someone has managed to use
properties defined in the configuration file while registering the
components using the fluent registration I would appreciate any advice

Thanks in advance
Pat

John Simons

unread,
Aug 12, 2010, 6:20:14 PM8/12/10
to castle-pro...@googlegroups.com
Hi Pat,

This blog post should help you:
http://mikehadlow.blogspot.com/2010/02/10-advanced-windsor-tricks-9.html

Cheers,
John


From: pmcg <pmcg...@gmail.com>
To: Castle Project Users <castle-pro...@googlegroups.com>
Sent: Fri, 13 August, 2010 3:26:10 AM
Subject: Mixing external configuration with fluent registration
--
You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
To post to this group, send email to castle-pro...@googlegroups.com.
To unsubscribe from this group, send email to castle-project-users+unsub...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.


 

pmcg

unread,
Aug 13, 2010, 5:03:28 AM8/13/10
to Castle Project Users
John,
That one was one of the links i included and registered a
configuration component in the xml file, which could then be used by
the components which were registered using the fluent configuration
when those components are being resolved. I would rather not to use a
single configuration component for all these settings as they are not
related i.e. database connection string, max queue length, retry count
etc. Very often a dependant component will only depend on a single
property as a constructor parameter, so using properties from the
external configuration file as ideal.
I could create a number of configuration components where all related
settings are in the same components, but if there was a way to access
properties from the configuration file when doing the fluent
registration, i could avoid creating and registering any of these
configuration components


Pat

On Aug 12, 11:20 pm, John Simons <johnsimons...@yahoo.com.au> wrote:
> Hi Pat,
>
> This blog post should help you:http://mikehadlow.blogspot.com/2010/02/10-advanced-windsor-tricks-9.html
>
> Cheers,
> John
>
> ________________________________
> From: pmcg <pmcgr...@gmail.com>
> To: Castle Project Users <castle-pro...@googlegroups.com>
> Sent: Fri, 13 August, 2010 3:26:10 AM
> Subject: Mixing external configuration with fluent registration
>
> I'm trying to figure out how I can reference external configuration
> properties (i.e. Database connection strings, log paths etc.), which
> will be constructor parameters for components I am registering with
> the fluent registration. When using xml configuration I would simply
> have configured them as properties within the configuration file. When
> configuring a component I would simply have configured the constructor
> parameter using the #{propertyname} type syntax. I have tried this
> with Depends and Parameters methods, but neither have worked.
> Is it possible to use property values that have been defined within a
> configuration file when the container was being created? Or are the
> properties just available when constructing the container from the
> configuration file?
> I have seen examples where a configuration component is configured
> within the configuration file and can then be passed as a constructor
> parameter to other components that depend on the properties. This
> requires one or more configuration types, which I would prefer not to
> use. Would be nice to just inject the property values which are very
> often distinct configuration items.
>
> See these for examples of such configuration holding typeshttp://mikehadlow.blogspot.com/2010/02/10-advanced-windsor-tricks-9.htmlhttp://ayende.com/Blog/archive/2008/12/31/didja-know-merging-windsor-...
>
> I can always fall back on programmatically accessing properties from
> the app settings section of my application configuration file when
> doing my fluent registration, but if someone has managed to use
> properties defined in the configuration file while registering the
> components using the fluent registration I would appreciate any advice
>
> Thanks in advance
> Pat
>
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to
> castle-project-u...@googlegroups.com.

Krzysztof Koźmic

unread,
Aug 13, 2010, 5:07:06 AM8/13/10
to castle-pro...@googlegroups.com
I think if you specify just the component id/ no type/service and the
parameters for the component, and then call:

container.Install(Configuration.FromAppConfig(), // this has to go first
new YourInstallerFromCode()
);

if the IDs of the components registered in code match the ids from the
XML I think Windsor will pick the parameters from XML up.

Krzysztof

Roelof Blom

unread,
Aug 13, 2010, 5:35:39 AM8/13/10
to castle-pro...@googlegroups.com
Something like this should work, and I think this is what Krzysztof  meant:

// The xml config <components> <component id="a_component"> <parameters> <parameter1>#{some_value}</parameter1> <parameter2>#{some_other_value}</parameter2> </parameters> </component> <component id="another_component"> <parameters> <parameter1>#{some_value}</parameter1> <parameter2>#{yet_some_other_value}</parameter2> </parameters> </component> <properties> <some_value>x</some_value> <some_other_value>y</some_other_value> <yet_some_other_value>z</yet_some_other_value> </properties> </components> // The name corresponds to the id in the xml config Component.For<AComponent>.Named("a_component") Component.For<AnotherComponent>.Named("another_component")


2010/8/13 Krzysztof Koźmic <krzyszto...@gmail.com>

pmcg

unread,
Aug 13, 2010, 10:13:33 AM8/13/10
to Castle Project Users
Krzysztof, Roelof

Perfect
Works as follows, so anybody else wanting to avoid the suggested
configuration style components suggested in the links, can do so as
follows

Component
---------
public class AComponent
{
public AComponent(string filePath) { Console.WriteLine(filePath); }
}


Application configuration file
------------------------------
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor" />
</configSections>
<castle>
<properties>
<logFileName>thefile.txt</logFileName>
</properties>

<components>
<component id="theComponent">
<parameters>
<filePath>#{logFileName}</filePath>
</parameters>
</component>
</components>
</castle>
</configuration>

Container usage
---------------
var _container = new WindsorContainer(new XmlInterpreter());
_container.Register(Component.For<AComponent>().Named("theComponent")); //
Normally would be configuring the parameter at this stage, but is now
picked up from the config file
_container.Resolve("theComponent");



Pat




On Aug 13, 10:35 am, Roelof Blom <roelof.b...@gmail.com> wrote:
> Something like this should work, and I think this is what Krzysztof  meant:
>
> // The xml config <components> <component id="a_component"> <parameters>
> <parameter1>#{some_value}</parameter1>
> <parameter2>#{some_other_value}</parameter2> </parameters> </component>
> <component id="another_component"> <parameters>
> <parameter1>#{some_value}</parameter1>
> <parameter2>#{yet_some_other_value}</parameter2> </parameters> </component>
> <properties> <some_value>x</some_value>
> <some_other_value>y</some_other_value>
> <yet_some_other_value>z</yet_some_other_value> </properties> </components>
> // The name corresponds to the id in the xml config
> Component.For<AComponent>.Named("a_component")
> Component.For<AnotherComponent>.Named("another_component")
>
> 2010/8/13 Krzysztof Koźmic <krzysztof.koz...@gmail.com>
> >>> castle-project-u...@googlegroups.com<castle-project-users%2Bun­subs...@googlegroups.com>
> >>> .
> >>> For more options, visit this group athttp://
> >>> groups.google.com/group/castle-project-users?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Castle Project Users" group.
> > To post to this group, send email to castle-pro...@googlegroups.com
> > .
> > To unsubscribe from this group, send email to
> > castle-project-u...@googlegroups.com<castle-project-users%2Bun­subs...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/castle-project-users?hl=en.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages