Last active
October 19, 2018 23:35
-
-
Save felipegtx/596dfc55131359ec354f to your computer and use it in GitHub Desktop.
Minimalistic DI class for .Net - https://www.nuget.org/packages/InstanceResolverFor?NormalizedVersion=1.0.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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