我应该最小化控制器中的接口数量吗?

时间:2011-04-15 14:31:56

标签: asp.net-mvc-3 domain-driven-design ninject-2

在下面的剪辑中,我的控制器有三个接口。这些是通过Ninject连线的。好吧,一切都很好,肯定是朝着正确方向迈出的一步。我的问题是这个吗?

1。)最好将3个接口包装到一个接口和一个工具中,从而减少传递给控制器​​ctor的params数量? 2.)别管它,它有效吗?

我一直在寻找从一切事物中抽象出来的方法。 想法?

public class RegistrationController : Controller
{
    private readonly ICategoriesService _categoriesService;
    private readonly IAuthenticationService _authenticationService;
    private readonly IRegistrationService _registrationService;

    // Ctor
    public RegistrationController(ICategoriesService categoriesService, 
        IAuthenticationService authenticationService,
        IRegistrationService registrationService)
    {
        _categoriesService = categoriesService;
        _authenticationService = authenticationService;
        _registrationService = registrationService;
    }

}

1 个答案:

答案 0 :(得分:1)

拥有一个巨大的接口(或一个巨大的类,这是你需要实现一个巨大的接口)因为它“方便”被广泛认为是antipattern。根据您当前界面的名称,它们看起来很好地和逻辑地围绕它们提供的操作类型,我建议您保持这种方式(这也提供了更高的灵活性,因为可能有其他地方你只有需要一些接口。)

顺便说一句:如果你有适当的单元测试和集成测试,“不管它,它正在工作”是一个永远不需要的短语。 ; - )