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?”
- Is there a better approach?
- Could MEF (Managed Extensibility Framework) have been used instead?
- Does this code violate the SOLID Principles?
- How hard will it be to maintain this solution in the long run?
- 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) {
Hum… Problem isn’t DI here, but ownership, separation of concerns and single-responsibility
LikeLike
Hello,
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 !).
LikeLike