A very small IoC container in C#.NET. Just created for learning purpose.
This is a simple C# Console Application that shows how you can create your own custom IoC Container in the easiest way.
WARNING: Please do not use this as your project IoC Container, It lacks many features and functionalities.
Usage
It's simple, Here are the steps:
- Define your types and implementations.
- Call Resolve method of the container to get desired implementation.
- Use it!
So let's get started!
1. Register your types like as follows:
var container = SimpleIoC.CreateInstance();
container.For<IMessageService>().Inject<MessageService>();
Or even simpler use Register method:
container.Register<ILogger, Logger>();
2. Then resolve and get an instance of your type like this:
var resolvedService = container.Resolve<IMessageService>();
3. It's done! Enjoy it.
resolvedService.SendMessage("This is a simple IoC Container!");
You can use Singleton in order to prevent instantiating classes more than once. To do so, add the UseSingleton() into your container:
var container = SimpleIoC.CreateInstance();
container.UseSingleton();
By doing this, All of the objects just creating once.