[Windsor] Passing a custom argument to a sub-dependency

106 views
Skip to first unread message

Daniel Hölbling

unread,
Feb 11, 2009, 12:26:01 PM2/11/09
to castle-pro...@googlegroups.com
Hello,

I am currently facing a interesting problem with my Windsor object creation.

I have a service that calculates something and then calls a Writer service to save this calculation.

So I have Calculator depend on IWriter through it's ctor.

This all works.
But now I have a special case where I need get a Calculator that is hooked up to a decorated IWriter that has one of it's dependencies supplied at runtime.
In more concrete terms I try to apply a filter through the decorator, so I need to resolve a Calculator object that depends on a Filtered IWriter (with a runtime-supplied filter argument) and the Filter-Decorator then passes the call on to the "real" IWriter that then goes into the DB.

Now, container.Resolve<Calculator>(
arguments) would work if I need to pass an argument to the calculator.
But how do I supply the arguments to the subsequent IWriter lookup?

Is there some way to pass arguments to subsequent resolves?

The obvious fallback would be to simply construct that object by hand. But that's not really ideal imo.

Any help would be greatly appreciated.
greetings Daniel Hoelbling

(PS: Sorry if this went to the list twice, I may have forgotten to change my from field to the one registered at google)

Germán Schuager

unread,
Feb 11, 2009, 1:24:23 PM2/11/09
to castle-pro...@googlegroups.com
Acording to this post: http://groups.google.com/group/castle-project-devel/browse_thread/thread/296dd82d5decd226/e5106c9ef7671710?hl=en&lnk=gst&q=MicroKernel+additionalArgs+behavior+definition#e5106c9ef7671710

This should work:

var sender = container.Resolve<Calculator>(new {parameterNameOfIWriter = value} );

Jason Meckley

unread,
Feb 11, 2009, 1:29:03 PM2/11/09
to Castle Project Users
you wouldn't pass the iwriter's dependency through the calculator, the
calculator doesn't know the type of the iwriter.

if you want to create writer at runtime, you could pass the kernel to
calculator and have the calculator pull the iwriter from the kernel
based on a parameter

var writer = kernel.Resolve<IWriter>(key of writer to select);
or you could create a factory object to create the calculator

public class CalculatorFactory
{
public ctor(IWriterRegistery registry) {...}

public Calculator Create(criteria for writer)
{
foreach(var writer in registry)
{
if(!writer.meets(critiera) continue;
return new Calculator(writer.implementation);
}
}
}

or you could resolve the writer and then resolve the calculator
var writer = container.Resolve<IWriter>(key to writer);
var calculator = container.Resolve<ICalculator >(writer);

you could register an IWriter which is just a composite of the actual
resolvers and place the logic there
class AllWriters : IWriter
{
ctor(IWriter first, IWriter second){...}

void dosomething()
{
if(should use first) first.dosomething();
else second.dosomething();
}
}

or you could implement your own subdependencyresolver to resolve the
type of writer needed.


Daniel Hölbling wrote:
> Hello,
>
> I am currently facing a interesting problem with my Windsor object creation.
>
> I have a service that calculates something and then calls a Writer service
> to save this calculation.
>
> So I have Calculator depend on IWriter through it's ctor.
>
> This all works.
> But now I have a special case where I need get a Calculator that is hooked
> up to a decorated IWriter that has one of it's dependencies supplied at
> runtime.
> In more concrete terms I try to apply a filter through the decorator, so I
> need to resolve a Calculator object that depends on a Filtered IWriter (with
> a runtime-supplied filter argument) and the Filter-Decorator then passes the
> call on to the "real" IWriter that then goes into the DB.
>
> Now, container.Resolve<Calculator>(arguments) would work if I need to pass

G. Richard Bellamy

unread,
Feb 11, 2009, 2:12:47 PM2/11/09
to castle-pro...@googlegroups.com
You could look at the Castle.Facilities.Logging.LoggerResolver for an example of an implementation of the ISubDependencyResolver approach. This uses a combination of Factory and Resolver.
 
- rb

Daniel Hölbling

unread,
Feb 12, 2009, 5:28:56 AM2/12/09
to castle-pro...@googlegroups.com
Thank you all very much. 
I'll look at the source.
Reply all
Reply to author
Forward
0 new messages