Skip to content

jsmunroe/SimpleIoc

Repository files navigation

SimpleIOC

This is a simple to use universal inversion of control container.

##Basic Usage

    public interface IContract { ... }
    public class MyService : IContract { ... }

    var container = new Container();
    container.Register<IContract, MyService>();
    
    IContract service = container.Resolve<IContract>();

    Assert.IsTrue(service is MyService);

##Constructor Injection

    public interface IContract { ... }
    public class MyDependency { ... }
    public class MyService : IContract 
    {
        public MyService(MyDependency dependency) { ... } 
    }

    var container = new Container();
    container.Register<IContract, MyService>();
    container.Register<MyDependency, MyDependency>();

    IContract service = container.Resolve<IContract>();

    Assert.IsTrue(service is MyService);

##Function-Based Service Registry

    public interface IContract { ... }
    public class MyService : IContract 
    {
        public MyService(String name, int age) { ... } 
    }
    
    var someService = new MyService("My Service", 3);
    var container = new Container();
    container.Register<IContract>(() => someService);

    IContract service = container.Resolve<IContract>();
    
    Assert.AreSame(service, someService);

##Property Injection

    public interface IContract { ... } 
    public MyDependency { ... }
    public class MyService : IContract
    {
        // Must have a public setter
        [Import]
        public MyDependency Dependency { get; set; }
    }
    
    var container = new Container();
    container.Register<IContract, MyService>();
    container.Register<MyDependency, MyDependency>();
    
    IContract service = container.Resolve<IContract>();    
    
    Assert.IsInstanceOfType((service as MyService).Dependency, typeof(MyDependency));

##Module Loader

    public MyModule : IModule 
    {
    	public void Bootstrap(Container a_container)
        {
            // Bootstrap all local services.
        }
    }
    
    var modules = ModuleLoader.Discover();
    
    Assert.IsInstanceOfType(modules.First(), typeof(MyModule));
    
    var container = new Container();
    modules.Bootstrap(container);

About

Simple to use universal inversion of control container.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages