I’ve written before about IoC and dependency injection, but these are older posts and my perspective and experience with these topics has fortunately been growing. I think they’re incredibly important when you’re building complex systems, but the concepts can offer some benefits in all of your programming! When you get in the habit of practicing this kind of thing, you can get some pretty flexible code… for free. We’ll explore how we can have dependency injection with Autofac in this article.
So a quick recap on what I mean by dependency injection here… I’m mostly focused on passing interfaces into constructors (and yes, I’m going to be using C# terminology as I do in most of my programming examples, but these concepts are generally the same in other languages). The benefits here:
- You can write implementations that don’t depend on other implementations… Just an API.
- Not depending on an interface means you can write mockable code for your unit tests. (I’ll follow up with a post on this to help provide examples)
- You can swap out functionality by providing a different implementation of an interface and NOT re-writing core code
- This can be a very powerful refactoring tool
- This can allow the creation of new functionality in a system simply by adding one small class instead of re-writing code
So that’s all good and well… So what do we use Autofac for?
Dependency Injection with Autofac – Article Disclaimer
Woah woah woah! Why does an article need a disclaimer?! Well, that’s because it’s much older than my other content on dependency injection with Autofac. There’s nothing wrong with this one – I feel it’s still informative and helpful. However, I don’t want you to miss out on the other dependency injection with Autofac articles that I have posted on the site.
When you might want to take the leap to Autofac
So you’ve been writing code now using interfaces in your constructor parameters. You’ve got nice modular code using composition. You have unit tests. Things are great.
There comes a point where you decide you need to break open a class in the depths of your system and provide it a new interface as part of the constructor. This is in line with the constructor parameter passing paradigm (nice alliteration, woo!) you’ve been using, so it feels good. You modify your constructor to take the new interface parameter. You change up your method to call this new interface’s API. You update your tests. It works!
Now you need to make the rest of your application work though, and it turns out that because this class is created so deep down in your system, you need to find a way to pass this new interface implementation allllllllll the way down. And suddenly, you find you need to break open 10 other classes to pass this interface into the constructor. It’s a simple change in that it’s the same change in 10 spots… But it’s 10 spots. And it’s tedious. And you got lucky because you own this code and you don’t need to worry about breaking the constructor API for other people.
But it might be time to look into something like Autofac at this point because it can make this problem disappear for you.
Enter Autofac!
Autofac is awesome. The end.
But seriously, Autofac is one example of a dependency container framework. The idea with a framework like this is that programmers can register things to the container and then at a later point, these things can be resolved. So you could:
- Decide to take a particular implementation and register it so that it can be resolved by its interface
- Decide if you want a registration to act like a singleton (and remember, a singleton does NOT have to have global access… it just means literally a single instance)
- Run callbacks when an instance is created
- … and so much more
In my opinion, the two major benefits of Autofac as they relate to this example are:
- You can better organize the top level of your application to wire up specific implementations to use in your code
- … Autofac can magically resolve the dependencies for you so it solves that nasty problem of passing down dependencies via constructors to deep areas of your code
You’ll need to be careful that you don’t abuse the container though! It’s considered an anti-pattern to use the container to manually resolve dependencies across various areas of your application (generally this is referred to as the Service Locator (anti)Pattern, but people go back and forth on why it’s good or bad). The “proper” use case is to resolve your single entry point class in one spot, call the methods you need on your entry point class, and let Autofac do its magic to resolve all of your registered dependencies.
Where Can I Get Autofac?
This is the easy part! You can use your Nuget package manager in Visual Studio to find the right package for your .NET framework dependency. Check it out at the Nuget Gallery!
What’s Next?
I have some examples I’d like to write about next for dependency injection with Autofac including:
- Using Modules for Organizing Code Dependencies
- Patterns for Dynamically Resolving Modules Across Assemblies
- How to use Autofac with Unity3D
But I’d love to hear what you want to know more about! Comment and let me know, and I’ll see what I can do.
Pingback: Part 1 - Exploring Graphs and Trees - Dev Leader Dev Leader
Pingback: Using Autofac With Unity3D - Dev Leader Dev Leader
Pingback: Stitching - Combining Unity3D And Autofac - Dev Leader Dev Leader
Pingback: Autofac Modules and Code Organization - Dev Leader - C# Programming Dev Leader