When is Dependency Injection too Much?

March 22, 2013 — 2 Comments

I’ve been working on NopCommerce for the past few weeks and I keep finding interesting code. The following constructor signature makes me ask a very simple question with a very complicated answer…

When is dependency injection too much?

    Bottom line, is that when I see code like this, I can’t help myself… I have to ask

Could this have been done differently?

public OrderController(IOrderService orderService, 
    IOrderReportService orderReportService, 
    IOrderProcessingService orderProcessingService,
    IDateTimeHelper dateTimeHelper, 
    IPriceFormatter priceFormatter,
    ILocalizationService localizationService,
    IWorkContext workContext, 
    ICurrencyService currencyService,
    IEncryptionService encryptionService, 
    IPaymentService paymentService,
    IMeasureService measureService, 
    IPdfService pdfService,
    IAddressService addressService, 
    ICountryService countryService,
    IStateProvinceService stateProvinceService,
    IProductService productService,
    IExportManager exportManager, 
    IPermissionService permissionService,
    IWorkflowMessageService workflowMessageService,
    ICategoryService categoryService, 
    IManufacturerService manufacturerService,
    IProductAttributeService productAttributeService, 
    IProductAttributeParser productAttributeParser,
    IProductAttributeFormatter productAttributeFormatter, 
    IShoppingCartService shoppingCartService,
    IGiftCardService giftCardService, 
    IDownloadService downloadService,
    IShipmentService shipmentService,
    CatalogSettings catalogSettings,
    CurrencySettings currencySettings, 
    TaxSettings taxSettings,
    MeasureSettings measureSettings, 
    PdfSettings pdfSettings, 
    AddressSettings addressSettings,
    ICustomerService customerService)

2 responses to When is Dependency Injection too Much?


    Hum… Problem isn’t DI here, but ownership, separation of concerns and single-responsibility



    It obviously violates the Single Responsability Principle.
    A great thing about constructor injection is that we can know how many dependencies our class uses. The large number of dependencies indicates that this class is doing too much things and needs to be refactored (even if it is a controller).

    DI is not the problem here, it just expose a design smell that the developers of nopcommerce chose to ignore (and they should have not !).


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.