Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass constructor arguments with dependency injection using Castle.Windsor?

I am wiring up my first SubSonic 3 application (in an ASP.NET MVC 1.0 front-end) and am looking at Rob's SimpleRepository. I'm using Castle.Windsor as an injection framework.

In my application startup, I configure Castle to bind a SubSonic SimpleRepository to a SubSonic IRepository. Nothing complicated there. However, the SimpleRepository has a ctor overload which takes two values: a connection string name and a set of SimpleRepositoryOptions. Not having dug too deep into Castle in the past, it's not clear if there is a way to specify the ctor arguments via configuration (or some other means).

Right now, I have a custom implementation of the SimpleRepository that explicitly creates a SimpleRepository with those arguments in it's parameterless ctor, but if I want to change these at any point in time, it requires changing the code and recompiling.

Is there a more elegant way to configure Castle to take constructor arguments?

like image 519
nkirkes Avatar asked Jan 23 '23 05:01

nkirkes


1 Answers

If you're configuring Windsor using an XML file, you define your ctor arguments and their values like this:

<component id="repository" service="IRepository" type="SimpleRepository" ...>
    &ltparameters>
        <connectionString>your connection string</connectionString>
        ...
    &lt/parameters>
</component>

See the Windsor configuration reference for more info:

http://www.castleproject.org/container/documentation/v1rc3/manual/windsorconfigref.html

like image 85
Ty. Avatar answered Apr 26 '23 15:04

Ty.