Welcome to the IoCWrap project!

NOTE: This project is no longer maintained here. Please visit IoCWrap@Bitbucket.

The IoCWrap project provides a set of interfaces and boiler plate base classes to provide a standard way to wrap to the various IoC container implementations.

Unlike the CommonServiceLocator, IoCWrap includes a fluent interface, similar to that found in Autofac and StructureMap, for the configuration of the container.
It is therefore possible to configure and use an inversion of control container without be tied into a specific implementation in your code and provides the ability to switch to a different container without any impact.

Supported Implementations

Usage

Configuration via the fluent interface, where <ContainerProvider> is the provider of the desired container implementation, as follows:

IContainerBuilder builder = <ContainerProvider>.CreateBuilder();

builder.Register<Instance>()
  .As<IInstance>()
  .FactoryScoped();

builder.Register<SingleInstance>()
  .As<ISingleInstance>()
  .SingletonScoped();

builder.Register<NamedInstance>()
  .Named<INamedInstance>("NamedInstance")
  .FactoryScoped();

builder.Register<NamedSingleInstance>()
  .Named<INamedSingleInstance>("NamedSingleInstance")
  .SingletonScoped();

builder.Register<ICustomService>(c => new CustomService())
  .FactoryScoped();

In addition, registrations can be done by implementing the IRegistry interface:

public class TestRegistry : IRegistry
{
  public void Configure(IContainerBuilder builder)
  {
    builder.Register<Instance>()
      .As<IInstance>()
      .FactoryScoped();
  }
}

The builder can load implementations of the IRegistry interface, and invoke the Configure() method to perform registrations.

// loads the given registry
builder.AddRegistry<TestRegistry>();

// loads the given registry
builder.AddRegistry(new TestRegistry());

// loads registries found in the given assembly
builder.AddRegistriesFromAssembly(Assembly.GetExecutingAssembly());

// loads registries found in a list of assemblies
builder.AddRegistriesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());

// loads registries found in the given type's assembly
builder.AddRegistriesFromAssemblyOf<Program>();

Once the required types have been registered with the builder, the container can be created, and services resolved via the various Resolve() method overloads:

IContainer container = builder.Build();

var service = container.Resolve<Instance>();

Implementing you own ContainerProvider

To implement your own container provider, there are four interfaces that will need to be implemented.

The IContainerProvider provides an instance of the builder for your container.

public interface IContainerProvider
{
  IContainerBuilder CreateBuilder();
}

The IContainerBuilder interface provides the means to register types and to ultimately build the container.

public interface IContainerBuilder
{
  IContainer Build();
  IContainerRegistration Register(Type type);
  IContainerRegistration Register<TType>();
  IContainerRegistration Register<TType>(TType instance) where TType : class;
  IContainerRegistration Register<TType>(ContainerComponentActivator<TType> activator);
}

For fluent configuration of the container, the IContainerRegistration interface provides the most common requirements.

public interface IContainerRegistration
{
  IContainerRegistration As<TService>();
  IContainerRegistration As(Type serviceType);
  IContainerRegistration As(params Type[] serviceTypes);

  IContainerRegistration Named<TService>(string serviceName);
  IContainerRegistration Named(string serviceName, Type serviceType);

  IContainerRegistration SingletonScoped();
  IContainerRegistration FactoryScoped();
  IContainerRegistration ContainerScoped();

  IContainerRegistration ExternallyOwned();
  IContainerRegistration OwnedByContainer();

  IContainerRegistration WithExtendedProperty(string key, object value);
}

And the IContainer provides type resolution for the registered types.

public interface IContainer
{
  TType Resolve<TType>();
  TType Resolve<TType>(string serviceName);
  TType Resolve<TType>(Type serviceType);
  bool TryResolve<TType>(out TType instance);
  bool TryResolve<TType>(Type serviceType, out TType instance);
  bool TryResolve<TType>(string serviceName, out TType instance);
}

Checkout the IoCWrap.Autofac project in the source code, for an example implementation of the Autofac IoC framework.

Last edited Mar 20, 2013 at 11:53 PM by VirtualStaticVoid, version 7