Resolve Open Generics with ASP.NET Cores DI-Container

Image for post
Image for post
Photo by Christopher Gower on Unsplash

ASP .NET Core already has quite a decent di-container built in. However, there are some quirks you might need to be aware of. In one of my applications, I’ve been using a factory pattern to register my views by using open generics. To be more clear about that: I have an Interface , and I want to register all of this interfaces implementations.

As I found out, Microsofts DI-Container currently doesn’t support that by default (text me if I’m wrong!), but there’s a solution for this problem. Scrutor is a package that adds more complex di-features — specifically . This means that, using this library, you now have the ability to scan through your assemblies and perform registrations based on your findings.

Now, for my case, this was quite simple: All I had to do was to add the following snippet to my startup-class:

And that’s it! Now, all the implementations of the interface find their way into my di container and I can easily retrieve new instances by utilizing the following factory class:

If needed, you could easily extend this by a second type parameter to specify e.g. arguments to pass to the view. Those would then be passed to the -Method, which in turn would forward them to the builders -Method.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store