I have the the following code in my Ninject modules repeated several times over. What methods and techniques can I use to reduce this repeated code?
public override void Load()
{
Bind<IDataReader<IList<Price>>>()
.To<PricesDataReader>().Named("ValDatePrices");
Bind<IDataConnection<IList<PricesCsvRecord>>>()
.To<PricesXLConnection>().WhenParentNamed("ValDatePrices")
.Named("ValDatePricesXLConnection");
Bind<IDirectoryBuilder>()
.ToMethod(DefaultValDatePricesDirectory)
.WhenParentNamed("ValDatePricesXLConnection");
Bind<IDataReader<IList<Price>>>()
.To<PricesDataReader>().Named("EDDatePrices");
Bind<IDataConnection<IList<PricesCsvRecord>>>()
.To<PricesXLConnection>().WhenParentNamed("EDDatePrices")
.Named("EDDatePricesXLConnection");
Bind<IDirectoryBuilder>()
.ToMethod(DefaultEDDatePricesDirectory)
.WhenParentNamed("EDDatePricesXLConnection");
}
The main differences occur when requesting an IDirectoryBuilder
whose main function is to determine the location of a file based on configuration settings through the use of an IDirectory
implementation.
In the example above, I return a DefaultDirectoryBuilder
, however I have several other implementations of these (see the EdNrrDirectoryBuilder
method) below.
public IDirectoryBuilder DefaultValDatePricesDirectory(IContext arg)
{
return new DefaultDirectoryBuilder(
ConfigurationManager.AppSettings["VALDATE_PRICES_DIR"],
ConfigurationManager.AppSettings["VALDATE_PRICES_FILENAME"]);
}
public IDirectoryBuilder DefaultEDDatePricesDirectory(IContext arg)
{
return new DefaultDirectoryBuilder(
ConfigurationManager.AppSettings["EDDATE_PRICES_DIR"],
ConfigurationManager.AppSettings["EDDATE_PRICES_FILENAME"]);
}
public IDirectoryBuilder EdNrrDirectoryBuilder(IContext arg)
{
return new ExternalDirectoryBuilder(
ValuationDate,
ConfigurationManager.AppSettings["NRRDATE_DIR"],
ConfigurationManager.AppSettings["NRRDATE_PRICES_FILENAME"]);
}
My issue is that I need values from my configuration files. Right now, all configuration related requests are limited in my Ninject modules.
If I use a Ninject Factory approach to create IDirectoryBuilder
s, the way I see it is that I will need to have ConfigurationManager
related calls scattered throughout my codebase.
If I use a Ninject Provider approach, I will need providers for all implementations of IDirectoryBuilder
s, and also updated my constructors and implementations of IDataConnection
. My code now also looks like (not very DRY and similar to my current approach).
Bind<IDirectoryBuilder>().ToProvider<DefaultDirectoryBuilderProvider>()
.WhenParentNamed("EDDatePricesXLConnection")
.WithConstructorArgument("baseDir", "someConfigValue")
.WithConstructorArgument("fileName", "someOtherConfigValue");
My code has a very consistent dependency chain at the moment (using NamedArguments): ICalculator
->IDataReader
->IDataConnection
->IDirectoryBuilder
- this leads me to believe that there must be some way to create this chain repeatedly without having to repeat the setup code - which I can not seem to figure out. There is the added limitation, in that I often require two instances of the same dependency chain - the only difference being different configuration values.