Is a nontrivial constructor in the IoC paradigm bad?

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:

//dependencies
interface IMyFooDependency
{
   string GetBuzz();
   int DoOtherStuff();
}

interface IMyBarDependency
{
  void CrunchMe();
}

//consumer
class MyNiceConsumer
{
  private readonly IMyFooDependency foo;
  private readonly  IMyBarDependency bar;
  private /*readonly*/ string buzz;//<---question here
  MyNiceConsumer(IMyFooDependency foo, IMyBarDependency bar)
  { 
     //omitting null checks
     this.foo = foo;
     this.bar = bar;
     //OR
     this.buzz = foo.GetBuzz();//is this a bad thing to do? 
  }  
}

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 ( , )

?

+3
3

IMyFooDependency buzz, :

class MyNiceConsumer
{
  private readonly IMyBarDependency bar;
  private readonly string buzz;

  MyNiceConsumer(string buzz, IMyBarDependency bar)
  { 
     this.buzz = buzz;
     this.bar = bar;
  }  
}

:

new MyNiceConsumer(foo.GetBuzz(), bar);

buzz . . , .

UPDATE: . winforms InitializeComponent, , .

SRP ( ) . . :

, . ( ! !)

+1

, - , . - . - " ".

. , , . , - , , , .

, , " " . , , .

- , , . .

, File , : keyboard.filename_from_keyboard(). , factory () , , File . , ? , IMO. , " " . ...

, foo.GetBuzz() . GetBuzz() , , . GetBuzz() -, , , , .

+1

, ! !

(, MyNiceCreator) foo MyNiceConsumer, MyNiceCreator. , MyNiceConsumer, . : MyNiceCreator "" MyNiceConsumer. , MyNiceConsumer .

0

All Articles