Skip to content

Instantly share code, notes, and snippets.

@felipegtx
Last active October 19, 2018 23:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipegtx/596dfc55131359ec354f to your computer and use it in GitHub Desktop.
Save felipegtx/596dfc55131359ec354f to your computer and use it in GitHub Desktop.
public abstract class InstanceResolverFor<SomeType>
{
public static Func<SomeType> InstanceBuilder = () =>
{
throw new Exception(string.Format("The type '{0}' does not have a valid factory.", typeof(SomeType).FullName));
};
public static SomeType Instance { get { return InstanceBuilder(); } }
}
public abstract class InstanceResolverFor<SomeType, TConstructorParam>
{
public static Func<TConstructorParam, SomeType> InstanceBuilder =
param => throw new Exception(string.Format("The type '{0}' does not have a valid factory.", typeof(SomeType).FullName));
public static SomeType GetInstance(TConstructorParam param) => InstanceBuilder(param);
}
[TestMethod]
public void Get_Instance_From_Valid_Object()
{
InstanceResolverFor<ISomeInterface>.InstanceBuilder =
() => new InterfaceImplementation();
var instance = InstanceResolverFor<ISomeInterface>.Instance;
Assert.IsNotNull(instance);
}
[TestMethod]
public void Get_Instance_From_Invalid_Object()
{
try
{
var a = InstanceResolverFor<object>.Instance;
Assert.Fail("Object should not have being returned.", a);
}
catch { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment