Skip to main content

Posts

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 locationThis first approach will 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 methods. I…
Recent posts

Recorded Rubber Ducking

Just thought I'd share a "developing in lock-down" tip, which I inadvertently invented (well, let's say discovered) last week. I'm going to call it recorded rubber ducking. The idea of rubber ducking being that if you talk out a problem, even to yourself, you may hit upon the answer.That wasn't working for me, so instead I felt I needed to ask my colleagues to see if someone could see what I couldn't.Importantly though - and really I was just trying not to waste people's time - I took the trouble to clearly write out the issue. What it was, what I'd found, and what still puzzled me. I planned to then post this in Slack, in one hit with all the information, rather than starting with a "@here, anyone know about X?" type of message and follow up from there.And, as you've probably guessed, the act of doing that was sufficient to see the solution to problem* without even bothering anyone.Clearly the action of writing, and forcing me t…