Инъекционная инъекция кажется хорошей. В общем случае, следует ли вводить зависимости в соответствии с методами, которые их требуют, или они должны быть введены в конструктор класса?
См. примеры ниже, чтобы продемонстрировать два способа введения той же зависимости.
//Inject the dependency into the methods that require ImportantClass
Class Something {
public Something()
{
//empty
}
public void A()
{
//do something without x
}
public void B(ImportantClass x)
{
//do something with x
}
public void C(ImportantClass x)
{
//do something with x
}
}
//Inject the dependency into the constructor once
Class Something {
private ImportantClass _x
public Something(ImportantClass x)
{
this._x = x;
}
public void A()
{
//do something without x
}
public void B()
{
//do something with this._x
}
public void C()
{
//do something with this._x
}
}