Guys I need your help I am using Castle Project Windsor IoC container in my C# Winforms application. I have services classes which has DI by constructor passing in implementing Repositories. I used Windsor to register them all Component.Register(...etc but everytime I use my service class I am calling resolve. for example:
var employeeService = container.Resolve....etc
is there any other way not having to call resolve all the time?
in ASP.NET you can just register them all then set the Controller factory:
ControllerBuilder.Current.SetControllerFactory(new WindsorIoCFactory());
with this I can just use my controllers(using services) directly without calling resolve. how do you do this in winforms?
Asked
Active
Viewed 626 times
0

Sven Grosen
- 5,616
- 3
- 30
- 52

Patrick87
- 237
- 1
- 2
- 10
-
I think this [answer](http://stackoverflow.com/a/4840907/1346943) should help you out – Sven Grosen Dec 03 '13 at 16:08
1 Answers
1
You should use a Composition Root approach.
In short, you should use Resolve only once at the App-StartUp basically resolving the MainView and through that you should be able to obtain all views(most likely through a TypedFactory) and their dependencies with no explicit Resolve call.
Service locator approach, as per comment link, is a deprecable anti-pattern specially when Composition Root can be used instead.
On the windsor wiki you'll find a sample based on a Silverlight app, very close to a Winform scenario.

Crixo
- 3,060
- 1
- 24
- 32
-
-
Hi Cristiano.. if I have many services say employee, customer ..etc and register them all in start up of or main method in winforms which acts as a composition root i dont have a problem there but where should I put my container instance? it must be a place like main or it must be static where i can call it globally for resolves right? oh and another thing is there such as a Resolve factory? So i wont have to reference(using [namespace]) in every .cs file I call resolve? – Patrick87 Dec 03 '13 at 17:03
-
Read this http://mookid.dk/oncode/archives/1854 you should get all your answers... plus the silverlight sample from the wiki as above. But yes, your container should live buried as a private variable within "Main"/StartUp Class and no one from outside should access it directly or through a ServiceLocator aka "the evil"(cit. Krzysztof Koźmic) :-) – Crixo Dec 03 '13 at 17:55