Creating a simple IoC container

Michal Diviš
1 min readFeb 1, 2021

--

Recently, I’ve been considering using an IoC container in my WPF app. It came to a point where the app was getting pretty huge in terms of number of different views, view models and code in general. So to make my life easier, I wanted to use an IoC container to handle all kinds of services differently for production and testing.

However, all IoC libraries I could find were pretty big, containing thousands of lines of code I wouldn’t use and I wanted to keep it simple and small in size. So I’ve decided to make my own simple IoC container.

What should it do?

My requirements for the IoC container were:

  • Registering an implementation of an interface
  • Getting an instance of a service by the interface it implements

The code

Here’s what I’ve got so far

How can I use it?

With the code above, the custom IoC container can be used like this:

That’s it

And that is it. It’s simple, doesn’t do any crazy stuff. It just does what I needed, nothing more. I hope you might find this helpful, too.

Cheers!

--

--

Michal Diviš
Michal Diviš

No responses yet