I use dependency injection in my C # projects and, as a rule, everything is fine. However, I often hear the rule "the constructor should consist only of trivial operations - assign dependencies and do nothing" Ie:
interface IMyFooDependency
{
string GetBuzz();
int DoOtherStuff();
}
interface IMyBarDependency
{
void CrunchMe();
}
class MyNiceConsumer
{
private readonly IMyFooDependency foo;
private readonly IMyBarDependency bar;
private string buzz;
MyNiceConsumer(IMyFooDependency foo, IMyBarDependency bar)
{
this.foo = foo;
this.bar = bar;
this.buzz = foo.GetBuzz();
}
}
UPD : suppose it IMyFooDependencycannot be replaced by GetBuzz(), since in this case the answer is obvious: "does not depend on foo".
UPD2 . Please understand that this question is not about eliminating the dependency on foo in hypothetical code, but about understanding the principle of a good constructor constructor.
, : bad pattern (.. buzz, .)
, , foo.GetBuzz() , .
, : , , - , IoC ( , )
?