Showing posts with label Unity. Show all posts
Showing posts with label Unity. Show all posts

Monday, 26 August 2013

Unity Generic Container Interface Implementation

An implementation of the IIoCGenericContainer interface for the Unity container.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;

namespace IoC
{
public class UnityGenericContainer : IIoCGenericContainer
{
private IUnityContainer container;

public UnityGenericContainer(IUnityContainer container)
{
this.container = container;
}

#region IGenericContainer Members

public TType Resolve<TType>()
{
return this.container.Resolve<TType>();
}

public TType TryResolve<TType>()
{
TType result;

try
{
result = this.container.Resolve<TType>();
}
catch (Exception)
{
result = default(TType);
}

return result;
}

public void RegisterType<T>()
{
this.container.RegisterType<T>();
}

public void RegisterType<T>(bool singleton)
{
if (singleton)
{
this.container.RegisterType<T>(new ContainerControlledLifetimeManager());
}
else
{
this.container.RegisterType<T>();
}
}

public void RegisterType<TFrom, TTo>(bool singleton) where TTo : TFrom
{
if (singleton)
{
this.container.RegisterType<TFrom, TTo>(new ContainerControlledLifetimeManager());
}
else
{
this.container.RegisterType<TFrom, TTo>();
}
}

public void RegisterInstance<TInterface>(TInterface instance, bool singleton)
{
if (singleton)
{
this.container.RegisterInstance<TInterface>(instance, new ContainerControlledLifetimeManager());
}
else
{
this.container.RegisterInstance<TInterface>(instance);
}
}

public void RegisterInstance<TInterface>(TInterface instance)
{
this.container.RegisterInstance<TInterface>(instance);
}


public object Resolve(Type t)
{
return this.container.Resolve(t);
}

public IEnumerable<object> ResolveAll(Type t)
{
return this.container.ResolveAll(t);
}

public IEnumerable<T> ResolveAll<T>()
{
return this.container.ResolveAll<T>();
}

public void RegisterType<TFrom, TTo>() where TTo : TFrom
{
this.container.RegisterType<TFrom, TTo>();
}

public void RegisterInstance(object instance)
{
this.container.RegisterInstance(instance);
}

#endregion
}
}