简体   繁体   English

注入多个接口是否违反 SOLID?

[英]Inject multiples interfaces is against SOLID?

I got the following class:我得到以下 class:

public MealService( IFoodRepository foodRepository, 
                    IOrderRepository orderRepository,
                    IDishListRepository dishListRepository)
{
    _inputValidator = inputValidator;
    _foodRepository = foodRepository;
    _orderRepository = orderRepository;
    _dishListRepository = dishListRepository;
}

... then, some code here ...然后,这里有一些代码

at the end of the process, I do:在流程结束时,我会:

private async Task<Order> CreateOrderAsync(int dayTime, List<Item> items)
{
    Order order = new Order();
    DishList dl;
    Food food;

    foreach (Item it in items)
    {
        dl = await _dishListRepository.GetAsync(dayTime, it.DishType);
        food = await _foodRepository.GetAsync(dl.FoodId);
        it.Food = food.Name;
        order.Items.Add(it);
    }
    await _orderRepository.AddAsync(order);
    return order;
}

Am I going against the Single responsability principle (the 'S' in SOLID)?我是否违反单一职责原则(SOLID 中的“S”)? I mean, could injecting too many interfaces into a class mean that the class has too many responsibilities?我的意思是,向 class 注入太多接口是否意味着 class 有太多责任?

Thanks in advance.提前致谢。

The answer is most probably yes.答案很可能是肯定的。 Usually too many parameters is a typical code smell.通常过多的参数是典型的代码异味。 Please see chapter #3 in Uncle Bob 's Clean Code .请参阅Uncle Bob 's Clean Code中的第 3 章。 He devoted 4 pages to the subject.他用了 4 页的篇幅来阐述这个主题。 Here is a short quote from there:这是那里的简短引述:

The ideal number of arguments for a function is zero (niladic). function 的理想数字 arguments 是零 (niladic)。 Next comes one (monadic), followed closely by two (dyadic).接下来是一个(单子),紧随其后的是两个(二元)。 Three arguments (triadic) should be avoided where possible.应尽可能避免使用三个 arguments(triadic)。 More than three (polyadic) requires very special justification -- and then shouldn't be used anyway.超过三个(多元)需要非常特殊的理由——无论如何都不应该使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM