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
}
}

Sunday, 25 August 2013

Ninject Generic Container Interface Implementation

An implementation of the IIoCGenericContainer interface for the Ninject container.

using System;
using Ninject;
using System.Collections.Generic;

namespace IoC
{
public class NinjectGenericContainer : IIoCGenericContainer
{
IKernel kernel;

public NinjectGenericContainer(IKernel kernel)
{
this.kernel = kernel;
}



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

public T Resolve<T>()
{
return this.kernel.Get<T>();
}

public T TryResolve<T>()
{
return this.kernel.TryGet<T>();
}

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

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

public void RegisterType<T>()
{
this.kernel.Bind<T>().ToSelf();
}

public void RegisterType<T>(bool singleton)
{
if (singleton)
{
this.kernel.Bind<T>().ToSelf().InSingletonScope();
}
else
{
this.RegisterType<T>();
}
}

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

public void RegisterType<TFrom, TTo>(bool singleton) where TTo : TFrom
{
if (singleton)
{
this.kernel.Bind<TFrom>().To<TTo>().InSingletonScope();
}
else
{
RegisterType<TFrom, TTo>();
}
}

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

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

public void RegisterInstance<TInterface>(TInterface instance, bool singleton)
{
throw new NotImplementedException();
}
}
}

IoC Generic Container Interface

A generic interface that can be implemented for different IoC containers. By using the interface switching between container implementations would be easy, particularly helpfull when testing the use of a container.

using System;
using System.Collections.Generic;

namespace IoC
{
public interface IIoCGenericContainer
{
object Resolve(Type t);

T Resolve<T>();

T TryResolve<T>();

IEnumerable<object> ResolveAll(Type t);

IEnumerable<T> ResolveAll<T>();

void RegisterType<T>();

void RegisterType<T>(bool singleton);

void RegisterType<TFrom, TTo>() where TTo : TFrom;

void RegisterType<TFrom, TTo>(bool singleton) where TTo : TFrom;

void RegisterInstance(object instance);

void RegisterInstance<TInterface>(TInterface instance);

void RegisterInstance<TInterface>(TInterface instance, bool singleton);
}
}

Monday, 30 April 2012

Use Windows Phone GPS Emulator with a desktop application

Windows phone GPS emulator as the name suggests is a GPS emulator desktop application that helps you debug your windows phone 7 gps client application. You can get hold of the GPS Emulator here.