Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ahmadpayan/csharp-simple-ioc-container

A very small C# IoC container based on constructor injection.
https://github.com/ahmadpayan/csharp-simple-ioc-container

c-sharp csharp inversion-of-control inversionofcontrol ioc ioc-container learning

Last synced: about 1 month ago
JSON representation

A very small C# IoC container based on constructor injection.

Awesome Lists containing this project

README

        

# CSharp-Simple-IoC-Container
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:
1. Define your types and implementations.
2. Call Resolve method of the container to get desired implementation.
3. Use it!

So let's get started!

**1. Register your types like as follows:**

var container = SimpleIoC.CreateInstance();
container.For().Inject();

Or even simpler use Register method:

container.Register();

**2. Then resolve and get an instance of your type like this:**

var resolvedService = container.Resolve();

**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.