1

i know that with standard dependency injection in .net CORE MVC project parameter's constructor are automatically instanciated for controller classes. My question is: is it possible to have the same behaviour with my own classes?

For example, it would be nice is there was a "FromDI" attribute that could be used as follow:

public class MyClass {
   public MyClass([FromDI] IInputType theInput = null) {
     // i would like theInput is populated automatically from DI system 
   }
}

Thanks a lot, Stefano

stefano m
  • 4,094
  • 5
  • 28
  • 27
  • sorry i'm not using fabric, i want to use di in a simple console app, no web app – stefano m Jun 09 '19 at 09:49
  • The easiest way is define a DI container. Once you have defined a DI container (Castle Windsor; Autofac; Unity; MEF...) you should register you classes and interfaces than you will be able to apply DI in you project. – Flavio Francisco Jun 09 '19 at 10:19

2 Answers2

3

is it possible to have the same behaviour with my own classes?

Yes, that is built in.

Your class needs to be registered and injected itself, and can then use constructor injection without the need for any atttributes.

want to use di in a simple console app, no web app

That means you will have to set up a ServiceProvider and a Scope. Those things are already provided by ASP.Net Core.

H H
  • 263,252
  • 30
  • 330
  • 514
  • 1
    This should be the right answer. The docs express this in the first lines: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-2.2 And this article show how to setup the DI to work in a console app: https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/ – rogerdossantos Jun 09 '19 at 12:39
1

For simplicity I create the following scenario to show you how to use dependency injection in a simple console application.

I am using Castle Project - Install the NuGet: Install-Package Castle.Core -Version 4.4.0

I have the following interfaces:

public interface ITranslate
{
    string GetMenu();
}

public interface IFood
{
    string GetFood();
}

Than the classes the implement the interfaces:

public class FrenchCuisine : IFood
{
    public string GetFood()
    {
        return "Soupe à l'oignon";
    }
}

public class ComidaBrasileira : IFood
{
    public string GetFood()
    {
        return "Feijoada";
    }
}

public class FrenchTranslator : ITranslate
{
    private readonly IFood food;

    public FrenchTranslator(IFood food)
    {
        this.food = food;
    }

    public string GetMenu()
    {
        return this.food.GetFood();
    }
}

public class PortugueseTranslator : ITranslate
{
     private readonly IFood food;

    public PortugueseTranslator(IFood food)
    {
        this.food = food;
    }

    public string GetMenu()
    {
        return this.food.GetFood();
    }
}

If I put everything together in my Console Application:

using Castle.Windsor;
using System;
using Component = Castle.MicroKernel.Registration.Component;

namespace StackoverflowSample
{
  internal class Program
  {
    //A global variable to define my container.
    protected static WindsorContainer _container;

    //Resolver to map my interfaces with my implementations
    //that should be called in the main method of the application.
    private static void Resolver()
    {
        _container = new WindsorContainer();

        _container.Register(Component.For<IFood>().ImplementedBy<FrenchCuisine>());
        _container.Register(Component.For<IFood>().ImplementedBy<ComidaBrasileira>());

        _container.Register(
            Component.For<ITranslate>().ImplementedBy<FrenchTranslator>().DependsOn(new FrenchCuisine()));
    }

    private static void Main(string[] args)
    {
        Resolver();

        var menu = _container.Resolve<ITranslate>();
        Console.WriteLine(menu.GetMenu());
        Console.ReadKey();
    }
  }
}

Expected result

Soupe à l'oignon
Flavio Francisco
  • 755
  • 1
  • 8
  • 21