开发者

Strategies for Organizing Dependencies/IOC Containers in an MVC3 .Net app with Castle Windsor

开发者 https://www.devze.com 2023-04-02 01:32 出处:网络
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

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,
            IT开发者_高级运维witterFeedService 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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号