views:

698

answers:

5

Can anyone think of a good solution for getting IOC into a console application?

At the moment we are just using a static class with the following method:

public static T Resolve<T>()
{
    return dependencyResolver.Resolve<T>();
}

I would like the experience to be seamless but cannot think of a way of achieving this from a console application.

+1  A: 

I've used Spring.NET from a console app with no problems. You just need to point it at your config file, and it will hook up all the dependencies. What you then do with those objects depends on what your console app is trying to do, of course.

Jon Skeet
A: 

Checkout Microsoft Unity.

Bruno Shine
+3  A: 

Console application do not have any limitation over a web or form application for IoC. You can use any of your choice (Spring, Ninjector, etc.). Most of them are configurable with XML file outside your console application and some like Ninjector require a configuration inside your application.

Daok
A: 

You miss the point.

I have the iOC all hooked up using Castle Windsor.

But I am having to call it from the static method I illustrated in my code.

I just want to hide the inner details.

I don't want my code littered with IOC.Resolve();

I want an invisible experience.

For example in MVC we use the Windsor Controller Builder that takes care of dependency injection when set.

The whole experience is invisible.

I am trying to somehow recreate this in the Console application but I cannot think of a way.

dagda1
+1  A: 

You will have to make a service locater call (Resolve<T>()) somewhere. The trick is to get it as out-of-the-way as possible. For console applications this bootstrapping happens in the Main() method. Do it there and minimize those Resolve calls elsewhere and you'll be great. For most dependencies, use constructor injection.

Matt Hinze