Skip to content

Instantly share code, notes, and snippets.

@ianbattersby
Created March 22, 2012 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ianbattersby/2159466 to your computer and use it in GitHub Desktop.
Save ianbattersby/2159466 to your computer and use it in GitHub Desktop.
public class MainEntryPointClass : BaseClass
{
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