Skip to content

WozSoftware/Woz.SimpleIOC

Repository files navigation

Woz.SimpleIOC

Now released under Unlicense (http://unlicense.org)

A compact lightweight thread safe IOC library for .NET compiled against .NETStandard 1

It provides:

  • Multiple named registrations per interface or class.
  • Object lifetime to create instance or singleton objects.

Background

While this might appear to be a new project with few commits it is a recent rewrite of my personal long standing IOC library that has existed for a few years. It has been used in small and large scale projects, the largest being a set of web services that runs a company backend with 500K+ lines of code.

Create container:

var ioc = IOC.Create();

Sample registrations:

Register a singleton for an interface.

ioc.Register<IThing>(ioc => new Thing());

Register a named singleton for an interface.

ioc.Register<IThing>("Name", ioc => new Thing());

Register a named via enum singleton for an interface.

ioc.Register<IThing>(EnumType.Value, ioc => new Thing());

Register an instance for an interface.

ioc.Register<IThing>(ObjectLifetime.Instance, ioc => new Thing());

Register a named Instance for an interface.

ioc.Register<IThing>("Name", ObjectLifetime.Instance, ioc => new Thing());

Register with nested resolution.

ioc.Register<IThing>(ioc => new Thing(ioc.Resolve<IList<int>>()));

Registration with default builder, all registration methods support this style.

ioc.Register<IThing, Thing>();

Sample resolutions

Resolve an instance.

var instance = ioc.Resolve<IThing>();

Resolve a named instance.

var instance = ioc.Resolve<IThing>("Name");

Other operations

Flush the registration list and unfreeze.

ioc.Clear();

Freeze the IOC container to remove most thread locking contentions

ioc.FreezeRegistrations();