Windsor Newbie Questions

2 views
Skip to first unread message

ksc654

unread,
Nov 5, 2009, 10:01:01 AM11/5/09
to Castle Project Users
I just started using Windsor in my application:

internal static class Program
{
private static MainPresenter _mainPresenter;
private static readonly WindsorContainer Container = new
WindsorContainer();

private static void Main()
{
Initialize();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
_mainPresenter = Container.Resolve<MainPresenter>();
_mainPresenter.Show();
}

private static void Initialize()
{
Container.AddComponent<IMainView, MainView>();
Container.AddComponent<IAccountRepository,
AccountRepository>();
Container.AddComponent<MainPresenter>();
}
}

So this is pretty straightforward. MainPresenter depends on
implementations of IMainView and IAccountRepository. Load the
container with the necessary types. Call resolve on a type you want to
use and Windsor automagically fills it's dependencies.

My question's are:
1. What's the best way to get Windsor where its needed? Pass around
references to the container? Create a static gateway? Use a <cough>
singleton?
2. How does Windsor handle multiple types implementing the same
interface? How do I ensure the proper type is created?

The majority of my application is implemented as plugins. The plugins
vary in purpose. Some implement services the program can use such as
databases and data import, many implement domain entities. So I need
some dependencies to resolve to the same object while others will
create a new object each time. If I can figure out how, I would like
to have my plugin manager fill the Windsor container then let the
contain handle the rest.

Any help figuring all this out would be greatly appreciated.

Jason Meckley

unread,
Nov 5, 2009, 10:50:29 AM11/5/09
to Castle Project Users
1. you should try to limit Windsors exposure in objects. if you do
need the container inject IKernel into the object. windsor is just a
wrapper around the micro kernel. the kernel is where all the work is
really done.

2. the great thing about the micro kernel are all the options for
configuration. however the problematic thing about the micro kernel
are all the options for configuration :) the kernel registers
components by type and key. the default key is the full name of the
type [namespace].[name of object]. this of course can be overridden. I
believe the default order for resolution from the kernel is:
1. if there is 1 implementation in the kernel resolve it.
2. if there are multiple implementations and the name of the parameter
matches a key resolve the component for that key
3. if there are multiple implementations and the name of the parameter
does not match any keys resolve the 1st component registered
since the kernel is so expendable, you can implement your own
resolution logic, but this an advanced feature.

when I have multiple instances on an interface in my system I utilize
#2 above.

I didn't see where you are registering your objects above? are you
using xml configuration? if so I believe you need to pass a new
XmlInterpreter() into the WindsorContainer. i find the xml
configuration extremely annoying. I use programmatic configuration
along with my own AbstractFacility implementations to encapsulate the
registration. I haven't needed a plug-in architecture though. You can
mix and match the programmatic with xml configs as well to reduce the
angle brackets required.


> 2. How does Windsor handle multiple types implementing the same
> interface? How do I ensure the proper type is created?

ksc654

unread,
Nov 5, 2009, 11:13:38 AM11/5/09
to Castle Project Users
For the moment I'm using programmatic configuration (see Initialize()
in orginal post).

I have one other question. If I have a hierarchy of interfaces:

IEntity -> IAccount -> IEmployer -> Employer

and I have two classes that depend on Employer but use different
levels of interfaces
how does MicroKernel/Windsor resolve them?

class1
{
class1(IAccount){}
}

class2
{
class2(IEmployer){}
}

Do I need to add Employer to the container twice? Once for each
interface?

Krzysztof Koźmic (2)

unread,
Nov 5, 2009, 11:21:55 AM11/5/09
to Castle Project Users
Yes, you must be explicit about this:
you do
Container.Register(Component.For<IAccount>().ImplementedBy<Employer>
().Forward<IAccount>())

Krzysztof Koźmic (2)

unread,
Nov 5, 2009, 11:24:55 AM11/5/09
to Castle Project Users
eeeeer I mean:
Container.Register(Component.For<IEmployer>().ImplementedBy<Employer>
().Forward<IAccount>());


obviously :)
Reply all
Reply to author
Forward
0 new messages