2

So, I'm new to using Castle Windsor and I'm struggling with how ugly my Controllers are becoming. I've got IOC working in my project which seems to be at least half the problem for most people. Now I'm finding that I'm declaring a ton of dependencies in my controller constructors as below. Are there any good patterns for managing these so I'm not copying/pasting this into each new controller and/or section of the site I create?

public HomeController(ILocalizationService localizationService,  // ugly
            INewsService newService, 
            IAnswerService answerService,
            ITwitterFeedService twitterService,
            IFacebookService facebookService,
            ISettingsService settingsService,
            IExternalDataService externalDataService,
            IUserService userService,
            IInstantMessageService instantMessageService,
            ICalendarService calendarService,
            ILogger logger)
        {
// do some stuff to link these up
}

Hope this makes sense. I can add more details if necessary to clarify.

longda
  • 9,445
  • 6
  • 44
  • 66
  • 1
    possible duplicate of [Dependency Injection Constructor Madness](http://stackoverflow.com/questions/2420193/dependency-injection-constructor-madness) – Mark Seemann Sep 01 '11 at 20:43
  • 1
    another similar posting: http://stackoverflow.com/questions/4603555/how-to-deal-with-constructor-over-injection-in-net – longda Sep 02 '11 at 06:18
  • 1
    @Mark Seemann - Thanks for the link... I like the Aggregate Service solution you mention and I'm going to try to incorporate that into my project. I also clicked through to your blog and saw your book. I pre-ordered a copy as it looks very good. Cheers! – longda Sep 02 '11 at 06:25

2 Answers2

1

It seems as though your controllers are doing too much. Try to make controllers more specialised, so with the exception of really common stuff like ILogger they don't need too many dependencies.

Review the action methods on the controllers, and see which ones seem to have similar behaviour and dependencies - they're candidates for moving to their own controller.

Neil Barnwell
  • 38,622
  • 28
  • 141
  • 213
  • Thanks for the reply. I'm pretty new to MVC as well so I very well may be overloading the controller. We're using strongly-typed view models for our views/partials and I'm feeling like they're not structured correctly. This is leading to the extra dependencies to populate view models for the partials within the action (and the view). I'm going to give your suggestion a shot. – longda Sep 02 '11 at 06:23
1

It seems you have a number of services that are basically doing similar things, such as facebook and twitter. Why not create an ISharingService that handles all your social networking stuff in a single interface?

Then you have an IUserService, which I take to be a repository of some type? If so, you might make better use of an Unit Of Work pattern that would condense all data repositories into one interface.

Erik Funkenbusch
  • 90,480
  • 27
  • 178
  • 274
  • Thanks for suggestions. Something like the ISharingService was one of my latest considerations. I'm still a bit fuzzy on how it should work... would it be an aggregate service solution similar to what Mark recommends? So if I needed to pull a twitter feed and a facebook feed, I'd make one call to the ISharingService and get back the results in a combined object? Or would I create Twitter and Facebook services and some how invert the control into them? Like I mentioned, I'm still new at this concept and I'm confusing myself! Need to look up the Unit of Work pattern too... – longda Sep 02 '11 at 06:33