Skip to main content


Showing posts from June, 2020

Dependency Injection in ASP.NET Core Attributes

When working with ASP.NET core one of the features now front and centre is dependency injection, built into the framework. Whether you're familiar with the pattern from using an IoC container from an external library, or are new to it with .NET core, the situations where you can't seem to make use of it start to stick out. In the post I'm going to share a few ways of tackling dependency injection with attributes and filters . These require a parameterless constructor and as such you can't use the typical pattern of constructor injection. I've prepared a tiny GitHub repository with the three examples discussed here. 1. Using service location This first approach isn't for the purists, and you'll risk other developers muttering "anti-pattern" at you, but it'll work. The idea is to use service location, in the constructor to retrieve the required services and set them to class level variables, where they can be used in the various method