interfacesolid-principlessingle-responsibility-principle

Inject multiples interfaces is against SOLID?


I got the following 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)? I mean, could injecting too many interfaces into a class mean that the class has too many responsibilities?

Thanks in advance.


Solution

  • 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. He devoted 4 pages to the subject. Here is a short quote from there:

    The ideal number of arguments for a function is zero (niladic). Next comes one (monadic), followed closely by two (dyadic). Three arguments (triadic) should be avoided where possible. More than three (polyadic) requires very special justification -- and then shouldn't be used anyway.