I have a bit of trouble sorting the way I manage automatic resolved and manual dependencies in my classes.
Suppose I have two classes for calculating prices: one calculates how much I will charge for delivery, and the other how much I will charge for the entire order. The second uses the first to summarize the cost of delivery for the entire price of the order.
Both classes have a dependency on the third class, which I will call ExchangeRate, which gives me the exchange rate that I have to use to calculate the price.
So far, we have this chain of dependencies:
OrderCalculator → ShippingCalculator → ExchangeRate
I use Ninject to resolve these dependencies, and this has worked so far. Now I have a requirement that the speed returned by the ExchangeRate class will vary depending on the parameter that will be provided in the constructor (since the object will not work without it, therefore, to force an explicit dependency to be placed in the constructor) with user input. Because of this, I can no longer resolve my dependencies automatically.
Whenever I want OrderCalculator or any other classes that depend on ExchangeRate, I cannot ask the Ninject container to allow it to me, since I need to provide a parameter in the constructor.
What do you suggest in this case?
Thank!
The EDIT: . Add code
This chain of objects is used by the WCF service, and I use Ninject as a DI container.
public class OrderCalculator : IOrderCalculator
{
private IExchangeRate _exchangeRate;
public OrderCalculator(IExchangeRate exchangeRate)
{
_exchangeRate = exchangeRate;
}
public decimal CalculateOrderTotal(Order newOrder)
{
var total = 0m;
foreach(var item in newOrder.Items)
{
total += item.Price * _exchangeRate.GetRate();
}
return total;
}
}
public class ExchangeRate : IExchangeRate
{
private RunTimeClass _runtimeValue;
public ExchangeRate(RunTimeClass runtimeValue)
{
_runtimeValue = runtimeValue;
}
public decimal GetRate()
{
//returns the rate according to _runtimeValue
if(_runtimeValue == 1)
return 15.3m;
else if(_runtimeValue == 2)
return 9.9m
else
return 30m;
}
}
//WCF Service
public decimal GetTotalForOrder(Order newOrder, RunTimeClass runtimeValue)
{
//I would like to pass the runtimeValue when resolving the IOrderCalculator depedency using a dictionary or something
//Something like this ObjectFactory.Resolve(runtimeValue);
IOrderCalculator calculator = ObjectFactory.Resolve();
return calculator.CalculateOrderTotal(newOrder);
}