Last active
August 27, 2022 12:22
-
-
Save mu88/184d4112bac50776ca20a0dfabd378c5 to your computer and use it in GitHub Desktop.
Refactor Dependency Injection
This file contains hidden or 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 class CarFactory | |
| { | |
| public CarFactory() | |
| : this(ServiceLocator.Resolve<IEngine>(), | |
| ServiceLocator.Resolve<IChassis>()) | |
| { | |
| } | |
| private CarFactory(IEngine engine, IChassis chassis) | |
| { | |
| Engine = engine; | |
| Chassis = chassis; | |
| } | |
| public IEngine Engine { get; private set; } // public for testing and a very very very very very long comment | |
| public IChassis Chassis { get; private set; } // public for testing | |
| public ICar ConstructCar() | |
| { | |
| return new Car(Engine, Chassis); | |
| } | |
| } |
This file contains hidden or 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 class CarFactory | |
| { | |
| public CarFactory(IEngine engine, IChassis chassis) | |
| { | |
| Engine = engine; | |
| Chassis = chassis; | |
| } | |
| public IEngine Engine { get; private set; } // public for testing | |
| public IChassis Chassis { get; private set; } // public for testing | |
| public ICar ConstructCar() | |
| { | |
| return new Car(Engine, Chassis); | |
| } | |
| } |
This file contains hidden or 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 class CarFactory | |
| { | |
| public ICar ConstructCar() | |
| { | |
| return new Car(ServiceLocator.Resolve<IEngine>(), | |
| ServiceLocator.Resolve<IChassis>()); | |
| } | |
| } | |
| public static class ServiceLocator | |
| { | |
| public static T Resolve<T>() | |
| { | |
| // Resolve instance from registered service registrations | |
| } | |
| } |
This file contains hidden or 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
| [TestClass] | |
| public class CarFactoryTests | |
| { | |
| [TestMethod] | |
| public void ConstructCar() | |
| { | |
| // Arrange | |
| var autoMocker = new AutoMocker(); | |
| autoMocker.Setup<IEngine,string>(x=>x.Type).Returns("V6"); | |
| var testee = autoMocker.CreateInstance<CarFactory>(true); | |
| // Act | |
| var result = testee.ConstructCar(); | |
| // Assert | |
| result.Should().NotBeNull(); | |
| result.Engine.Type.Should().Be("V6"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment