top of page
Writer's pictureThe Tech Platform

FromServices Attributes in ASP.NET Core



ASP.NET Core provides an attribute called FromServices to inject the dependencies directly into the controller's action method. In this article, we will learn how to use FromServices Attribute in ASP.NET Core.


When to use it?

When the method needs dependencies and is not used anywhere else, then you should use FromServices attribute.


Create a new controller in ASP.NET Core

Let’s create a new controller and use the FromServices attribute there. Follow the steps outlined below to create a new controller.

  1. Select the Controllers solution folder in the Solution Explorer window.

  2. Click “Add -> Controller…” to create a new controller.

  3. Select the template “API Controller with read/write actions.”

  4. Click Add.

  5. Name the controller SecurityController.

  6. Click Add to complete the process.


Use constructor injection

First, let’s take a quick look at injecting dependencies via constructor injection. Imagine the following interface and its implementation.

public interface IAccountService
{
    bool Validate(int accID);
}
  
public class AccountService : IAccountService
{
    public bool Validate(int accID)
    {
        //ToDo - Validate account
        return true;
    }
}

The service is configured in the ConfigureServices method in the Startup class. Like,

public void ConfigureServices(IServiceCollection services)
{
    services.AddTransient<IAccountService, AccountService>();
}

If you wish to use this service in any of your controllers, you inject the dependency via a constructor. Here, by adding a service type to your controller as a constructor parameter, ASP.NET Core will try to resolve that type using its built-in service container.

public class ValuesController : Controller
{
    private readonly IAccountService _accountService;
      
    public ValuesController(IAccountService accountService)
    {
        _accountService= accountService;
    }
      
    [HttpGet]
    public ActionResult<bool> Get(int accId)
    {
        return _accountService.Validate(accID);
    }
}

Constructor injection is one of the standard way to inject the dependencies and it is suitable in most cases. However, there are situations where you might want to avoid this approach to clean code and reduce rework. Like,

  • What if only a single action method within the controller requires the service?

  • Adding a dependency to the existing controller constructor will result in updating the unit test cases.


In the above cases, you may use FromServices attribute to inject the dependency. Like,

public class ValuesController : Controller
{  
    [HttpGet]
    public ActionResult<bool> Get([FromServices] IAccountService accountService, int accId)
    {
        return accountService.Validate(accID);
    }
}

So when we decorate the parameter with FromServices attribute, this instructs the ASP.NET Core to get it from the services container and inject the matching implementation. This makes the code cleaner and will also reduce the work of modifying the existing unit test cases.



Resource: Microsoft, talkingdotnet


The Tech Platform

0 comments

Comentários


bottom of page