static void OldWay() { // this is what is normally done, but not very good ClassA instanceA = new ClassA(); // DynamicTYping ClassC instanceC = new ClassC(instanceA); int sum = instanceC.GetSum(2, 3); }
public void TestGetSum() { //Create an mock of interface Mock<IMyInterface> mock = new Mock<IMyInterface>(); // Create mock implementation of ImyInterface mock.Setup(r => r.AddTwoNumbers(2, 3)).Returns(1000); ClassC instanceC = new ClassC(mock.Object); Assert.AreEqual(instanceC.GetSum(2, 3), 2000); }
static void DI() { //Create container UnityContainer container = new UnityContainer(); container.RegisterType<IMyInterface, ClassB>(); // Following code does not have depdency on ClassA or ClassB IMyInterface implement = container.Resolve<IMyInterface>(); ClassC instanceC = new ClassC(implement); int sum = instanceC.GetSum(2, 3); }