I have a Controller in ASP Core MVC. I'm trying to trim down the dependency injected services in the constructor so I can start building unit tests more easily. However, I have some services being injected that are only used in one or two controller actions. For example, I inject ILocationService
because in a couple of my actions, I need to lookup a country Id
number and get a ISO Alpha-2 country code using a database (eg mapping ID number 1 to "CA", mapping 2 to "US", etc.)
Asp Core supports the [FromServices]
attribute, so I have the option to inject ILocationService
directly into two of my actions instead of injecting them in the controller constructor. The advantage for this is I don't need to always mock/inject ILocationService
into my controller from every unit test and its more clear when writing unit tests which services each function depends on.
The obvious disadvantage is now its not completely obvious and clear what services my controller depends on since they are not all grouped in the constructor.
Are there any other specific challenges, issues, or points of confusion that may arise from using the [FromServices]
attribute?
Dependency injection helps to develop testable code, allowing developers to write unit tests easily. You can use mock databases with dependency injection, and test your application without affecting the actual database.
The FromServices attribute, when used in your controller, specifies that a parameter in an action method should be bound to a service from the services container. In other words, when you use this attribute in an action method, the services container is used to resolve dependencies at runtime.
Disadvantages of Dependency Injection: Dependency injection creates clients that demand configuration details to be supplied by construction code. This can be difficult when obvious defaults are available. Dependency injection can make code difficult to trace (read) because it separates behaviour from construction.
There are a few unfortunate downsides to this type of method injection that you should consider:
[FromServices]
attribute can be easily forgotten, and you will only find out when the action is invoked (instead of finding out at application start-up -or during a unit test- where you can verify the application's configuration)So instead of hiding the root problem with the use of method injection, I advise the use of constructor injection as sole injection pattern here and make your controllers smaller. This might mean, however, that your routing scheme becomes different from your class structure, but this is perfectly fine, and completely supported by ASP.NET Core.
From a testability perspective, btw, it shouldn’t really matter if there sometimes is a dependency that isn’t needed. There are effective test patterns that fix this problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With