-
-
Save jchannon/2159882 to your computer and use it in GitHub Desktop.
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 class MainEntryPointClass : BaseClass | |
{ | |
private ILogic Logic; | |
private IFileLogger FileLogger; | |
public MainEntryPointClass () : this(new Logic()) | |
{ | |
} | |
public IFileLogger FileLogger { get; set; } // For property injection for TDD or (e.g.) AutoFac | |
public ILogic Logic { get; set; } | |
public SetupProperties() | |
{ | |
this.ConfigProperties.Add(typeof(string), "DirectoryPath", "C:\\"); | |
} | |
public Run() | |
{ | |
ConstructConditionalDependencies(this.ConfigProperties.Find("DirectoryPath").Value()); | |
this.Logic.RandomProperty = this.ConfigProperties.Find(..).Value(); | |
} | |
private void ConstructConditionalDependencies(string directoryPath) | |
{ | |
// FileLogger | |
if (this.FileLogger == null) { | |
this.FileLogger = new FileLogger(directoryPath); | |
} | |
// Logic (which needs FileLogger) | |
if (this.Logic == null) | |
{ | |
this.Logic = new Logic(new Timer(), this.FileLogger); | |
} | |
} | |
} | |
/// From this point downwards we're all good clean DI + ctor injection. | |
public class Logic : ILogic | |
{ | |
private ITimer Timer; | |
private ILogger Logger; | |
public class Logic(ITimer timer, ILogger logger) | |
{ | |
this.Timer = timer; | |
this.Logger = logger; | |
} | |
public SomeMethod() | |
{ | |
this.Logger.Write("Data"); /******* How does DirectoryPath get assigned? *******/ | |
} | |
} | |
public interface IFileLogger | |
{ | |
void WriteData(string data); | |
} | |
public class FileLogger : IFileLogger | |
{ | |
public FileLogger(string directoryPath) | |
{ | |
this.DirectoryPath = directoryPath; | |
} | |
public void WriteData(string data) | |
{ | |
} | |
public string DirectoryPath { get; private set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment