Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOC for a Console Application?

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.

like image 713
dagda1 Avatar asked Nov 13 '08 19:11

dagda1


People also ask

How do I run a console application?

Run the appPress Ctrl + F5 to run the program without debugging. A console window opens with the text "Hello World!" printed on the screen. Press any key to close the console window.

What is IoC in ASP.NET Core?

ASP.NET Core supports the dependency injection (DI) software design pattern, which is a technique for achieving Inversion of Control (IoC) between classes and their dependencies.

How do I run a .NET Core console application?

In . NET Core, it runs from the dll, so you have to just run the application by running the command prompt and using the command - dotnet run. Open your command prompt and go to that folder where your application persists.


1 Answers

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.

like image 138
Matt Hinze Avatar answered Oct 14 '22 01:10

Matt Hinze