Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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

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.

Checkout Microsoft Unity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top