Skip to content

Instantly share code, notes, and snippets.

@mikehadlow
Created February 8, 2011 10:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikehadlow/816212 to your computer and use it in GitHub Desktop.
Save mikehadlow/816212 to your computer and use it in GitHub Desktop.
My common service locator replacement
using System;
namespace Suteki.Common.Windsor
{
public class IoC
{
private static Func<Type, object> resolve;
private static Action<object> release;
public static T Resolve<T>()
{
return (T)Resolve(typeof (T));
}
public static object Resolve(Type type)
{
if (resolve == null)
{
throw new SutekiCommonException(
"No resolve function has been set. Call IoC.SetResolveFunction(Func<Type, object> resolveFunction) first");
}
return resolve(type);
}
public static void Release(object instance)
{
if (release == null)
{
throw new SutekiCommonException(
"No release action has been set. Call IoC.SetReleaseAction(Action<object> releaseAction) first");
}
release(instance);
}
public static void SetResolveFunction(Func<Type, object> resolveFunction)
{
if (resolveFunction == null)
{
throw new ArgumentNullException("resolveFunction");
}
resolve = resolveFunction;
}
public static void SetReleaseAction(Action<object> releaseAction)
{
if (releaseAction == null)
{
throw new ArgumentNullException("releaseAction");
}
release = releaseAction;
}
public static void Reset()
{
resolve = null;
release = null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment