0

I'm currently learning about Unity and so far, everything I tried is working fine and as expected. However, what really bugs me is the injection of dependencies into the constructor.

Consider the following service:

public class ReallyComplexService : IReallyComplexService {

    public ReallyComplexService(INeedThisRepo repo1, INeedThisRepo2 repo2, INeedThisRepo3 repo3, IAlsoNeedThisDependency dependencyX, INeedAUnitOfWork uow, INeedCoffeeNow coffee, IShouldBetterAskJonSkeet skeet) 
    {
        this.Repo1 = repo1;
        this.Repo2 = repo2;
        this.Repo3 = repo3;
        this.AdditionalDependency = dependencyX;
        this.UnitOfWork = uow;
        this.Coffee = coffee;
        this.JonSkeet = skeet;

        skeet.Ask();
    }

}

Services can get reeeeeally big, especially when they're more than just a service layer for some website that is completely functional with writing stuff into / reading from a database. E.g. a Windows service that serves complex tasks (please record that Videostream from this IP camera and notify me if the camera is offline). This may turns constructors into a completely unreadable wall of code.

Is there an alternative way to satisfy the dependencies of a class?

Acrotygma
  • 2,375
  • 3
  • 23
  • 51
  • Check out [SRP](http://en.wikipedia.org/wiki/Single_responsibility_principle) - if your class have too many dependencies maybe it have way too many responsibilities and need to be split. – Alexei Levenkov Feb 14 '14 at 09:47
  • @AlexeiLevenkov Wow, great principle. However, this may not work in the example I gave above. (Let's say a recorder service needs to be aware of the IP cameras (to connect via RTSP), needs to be aware of media controls (to start / stop configured streams), needs to be aware of decoder information (to decode the streams), needs to be aware of a recording configuration (where should I write the recordings and in what format?) and also has additional features like timed recording, event recording (needs a service endpoint to listen for events).. – Acrotygma Feb 14 '14 at 10:01
  • Not clear what you trying to prove to yourself - if class is too complicated and have way too many dependencies you need to deal with it somehow... Either split, or be happy with constructor with 100 parameters or use property injection (i.e. http://stackoverflow.com/questions/14439284/looking-for-various-way-of-implemention-dependency-injection-using-unity?rq=1)... – Alexei Levenkov Feb 14 '14 at 10:19
  • @AlexeiLevenkov Property injection! I wasn't aware of this and it's exactly what I asked in my question. Thank you. – Acrotygma Feb 14 '14 at 11:24
  • Related: http://stackoverflow.com/a/2420245/126014 – Mark Seemann Mar 22 '14 at 22:06

0 Answers0