15

Question mentions it all.

In spring boot I am able to use the AutoWired annotation to automagically inject a dependency into my controller.

class SomeController extends Controller {
    @AutoWired
    private SomeDependency someDependency;
}

For I am curious as to if it has this annotation, currently the way to do it is by adding it as an argument to the constructor

[Route("api/[controller]")]
public class SomeController : Controller
{
    private SomeContext _someContext;

    public SomeController(SomeContext someContext)
    {
        _someContext = someContext;
    }
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
BossmanT
  • 559
  • 1
  • 6
  • 17

3 Answers3

7

There is no annotation.

You just need to make sure you register the dependency with the DI container at the composition root which is usually Startup.ConfigureServices

public void ConfigureServices(IServiceCollection services) {

    //...

    services.AddScoped<SomeContext>();

    //...
}

If in your case SomeContext is a DbContext derived class then register it as such

var connection = @"some connection string";
services.AddDbContext<SomeContext>(options => options.UseSqlServer(connection));

When resolving the controller the framework will resolve known explicit dependencies and inject them.

Reference Dependency Injection in ASP.NET Core

Reference Dependency injection into controllers

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • The default DI container basically only allows constructor injection, so all dependencies should be made constructor parameters. – juunas Jan 22 '18 at 18:25
  • @juunas is that comment meant for me or the OP? – Nkosi Jan 22 '18 at 18:26
  • An improvement suggestion for your answer :) – juunas Jan 22 '18 at 18:32
  • Since `@Autowired` in Spring does property injection, it might be significant to mention the difference. – juunas Jan 22 '18 at 18:32
  • @juunas that is why I was asking for confirmation. Your statement is not entirely accurate as it (the framework) allows *Action Injection* as well. Check the second link. I specifically made no mention of constructor injection because it was not the only option available. – Nkosi Jan 22 '18 at 18:33
  • Ahh, right that is true! You can inject into controller actions with `[FromServices]` and you can inject into middleware Invoke functions. You can ignore my comment :) – juunas Jan 22 '18 at 18:39
  • Yes I am aware of registering the context and have done so, I am mostly inquiring on ridding the controller with the constructors boiler plate code, the [FromService] is what I'm looking for but at a class member level – BossmanT Jan 23 '18 at 22:10
3

You can use NAutowired,the field injection

FatTiger
  • 647
  • 5
  • 14
1

Out of the box, Microsoft.Extensions.DependencyInjection doesn't provide property setter injection (only constructor injection). However, you can achieve this by using the Quickwire NuGet package, which does all the necessary plumbing for you. It extends the ASP.NET core built-in dependency injection container to allow service registration using attributes.

To use it, first add these two lines to your ConfigureServices method:

public void ConfigureServices(IServiceCollection services)
{
    // Activate controllers using the dependency injection container
    services.AddControllers().AddControllersAsServices();
    // Detect services declared using the [RegisterService] attribute
    services.ScanCurrentAssembly();

    // Register other services...
}

Then simply decorate your controller with the [RegisterService] attribute, and decorate any property to autowire with [InjectService]:

[Route("api/[controller]")]
[RegisterService(ServiceLifetime.Transient)]
public class SomeController : Controller
{
    [InjectService]
    private SomeContext SomeContext { get; init; }
}

Now SomeContext will automagically get injected with the corresponding registered service without having to go through the constructor song and dance.

For more information, you can also check out this table that maps out which Quickwire attribute corresponds to which Spring Boot annotation.

Flavien
  • 7,497
  • 10
  • 45
  • 52