Как мне внедрить и создать объект во время выполнения, основываясь на некоторой обработке?
В приведенном ниже коде основной калькулятор (GridCalculator
) (сокращенный для краткости) зависит от PricesCalculator
.Тем не менее, некоторая обработка должна выполняться в основном калькуляторе до создания PriceCalculator.Свойство GridPortfolios
PricesCalculator
будет всегда и может быть известно только во время выполнения.Я не уверен, как установить значение этого свойства во время выполнения (меня не интересуют другие два аргумента конструктора valDatePrices
& edDatePrices
).
Я хотел бы использовать Ninject для обработки созданияэтого объекта, так как есть зависимости, которые были правильно подключены.
Моя установка Ninject выглядит следующим образом:
Load
Метод моего модуля Ninject
public override void Load()
{
Bind<IFileReader<IList<Prices>>>().To<PricesReader>().Named("ValDatePrices");
Bind<IFileReader<IList<Prices>>>().To<PricesReader>().Named("EDDatePrices");
Bind<IFileReader<IEnumerable<GridRow>>>().ToMethod(GridReader);
/* other declarations*/
Bind<ICalculator<PriceSet>>().To<PricesCalculator>(); // this is probably not correct
Bind<ICalculator<IList<GridExposure>>>().To<GridCalculator>();
}
EDGridCalculator
public class GridCalculator : ICalculator<IList<GridExposure>>
{
public ICalculator<PriceSet> PricesCalculator { get; set; }
// not sure if constructor injection is the way to go
public GridCalculator(
ICalculator<PriceSet> pricesCalculator
, IFileReader<IEnumerable<GridRow>> gridReader)
{
this.PricesCalculator = pricesCalculator;
this.GridReader = gridReader;
}
public void Calculate()
{
var gdResults = GridReader().Read();
var pivot = new Pivot(gdResults);
IList<string> keys = pivot.Keys();
// Need my PricesCalculator object to be constructed
// with the "keys" variable and the other dependencies noted in Load()
// Continue to use "pivot" at later stages of processing.
}
}
PriceCalculator
public class PriceCalculator: ICalculator<PriceSet>{
[Inject] public IList<string> GridPortfolios;
PriceSet _priceSet;
public PricesCalculator(
[Named("ValDatePrices")]IFileReader<IList<Prices>> valDatePrices
, [Named("EDDatePrices")]IFileReader<IList<Prices>> edDatePrices
/* we could do constructor injection */
)
{
this.ValDatePricesReader = valDatePrices;
this.EDDatePricesReader = edDatePrices;
_priceSet = new PriceSet();
}
public void Calculate()
{ // calcs happen here
Result = new PriceSet();
}
}