Playing with LightInject Constructor Injection
using LightInject; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
namespace UnitTestProject1 | |
{ | |
[TestClass] | |
public class UnitTest1 | |
{ | |
public interface IMyService | |
{ | |
string CalledConstructor { get; } | |
} | |
public class MyService : IMyService | |
{ | |
public MyService() | |
{ | |
CalledConstructor = "MyService()"; | |
} | |
public MyService(string parameterOne) | |
{ | |
CalledConstructor = string.Format("MyService(parameterOne: {0}", parameterOne); | |
} | |
public MyService(string parameterOne, string parameterTwo) | |
{ | |
CalledConstructor = string.Format("MyService(parameterOne: {0}, parameterTwo: {1}", parameterOne, parameterTwo); | |
} | |
public string CalledConstructor { get; private set; } | |
} | |
public class MyInheritedService : MyService | |
{ | |
public MyInheritedService(string parameterOne) : base(parameterOne) { } | |
} | |
[TestMethod] | |
public void Resolve_should_use_default_constructor() | |
{ | |
var container = new ServiceContainer(); | |
container.Register<IMyService, MyService>(); | |
var service = container.GetInstance<IMyService>(); | |
Assert.AreEqual("MyService()", service.CalledConstructor); | |
} | |
[TestMethod] | |
public void FAILS_Resolve_should_use_constructor_with_one_parameter_but_chooses_the_one_with_two() | |
{ | |
var container = new ServiceContainer(); | |
container.RegisterInstance("FirstValue", "parameterOne"); | |
container.Register<IMyService, MyService>(); | |
var service = container.GetInstance<IMyService>(); | |
Assert.AreEqual( | |
string.Format("MyService(parameterOne: {0}", "FirstValue"), | |
service.CalledConstructor); | |
} | |
[TestMethod] | |
public void Resolve_should_use_constructor_with_two_parameter2() | |
{ | |
var container = new ServiceContainer(); | |
container.RegisterInstance("FirstValue", "parameterOne"); | |
container.RegisterInstance("SecondValue", "parameterTwo"); | |
container.Register<IMyService, MyService>(); | |
var service = container.GetInstance<IMyService>(); | |
Assert.AreEqual( | |
string.Format("MyService(parameterOne: {0}, parameterTwo: {1}", "FirstValue", "SecondValue"), | |
service.CalledConstructor); | |
} | |
[TestMethod] | |
public void Solution_1_hide_constructor() | |
{ | |
var container = new ServiceContainer(); | |
container.RegisterInstance("FirstValue", "parameterOne"); | |
container.Register<IMyService, MyInheritedService>(); | |
var service = container.GetInstance<IMyService>(); | |
Assert.AreEqual( | |
string.Format("MyService(parameterOne: {0}", "FirstValue"), | |
service.CalledConstructor); | |
} | |
[TestMethod] | |
public void Solution_2_prevent_resolveing_of_default_service() | |
{ | |
var container = new ServiceContainer(); | |
container.RegisterInstance("FirstValue", "parameterOne"); | |
container.RegisterInstance("dummy", "dummy"); | |
container.Register<IMyService, MyInheritedService>(); | |
var service = container.GetInstance<IMyService>(); | |
Assert.AreEqual( | |
string.Format("MyService(parameterOne: {0}", "FirstValue"), | |
service.CalledConstructor); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment