Our project is using constructor-based Dependency Injection (we're using Unity as our container) and we have a number of constructors which have acquired a large number of parameters.
For example:
public FooRequestService(
ITransactionDataBuilder transactionDataBuilder,
IMapper<SaveFooRequest, FooRequestMessage> saveFooRequestToFooRequestMapper,
IFooRequestedThingRepository fooRequestedThingRepository,
IFooRequestedThingBuilder fooRequestedThingBuilder,
IMapper<IEnumerable<FooRequestedThing>, IEnumerable<FoundFooRequestedThing>> fooRequestedThingsToFoundFooRequestedThingsMapper,
IAutomatedFooWcfService automatedFooService,
IFooArrivalMessageProvider fooArrivalMessageProvider,
ISeatCancellation cancellation,
IPostSalesService postSalesService,
ITransactionEnquiryService transactionService,
IMapper<IEnumerable<FooRequestedThing>, List<ThingArrivedForFoo>> thingArrivalRequestDocumentMapper,
IMapper<SaveFooRejected, FooRejectedMessage> saveFooRejectedToFooRejectedMapper,
IPartialFooConfiguration partialFooConfiguration,
ICurrentDateTimeProvider currentDateTimeProvider,
IMapper<FooRequestMessage, List<FooRequested>> tracsFooRequestMapper,
IThreeWayMerger<Transaction, TransactionService.Contract.Transaction, NotifyFooRequestedRequest, SaveFooRequest> saveFooRequestMerger,
KioskWebServiceSoap kioskServiceGateway)
{
// code here to set injected values to member variables
}
This class has a total of around 370 lines of code and represents a core part of the system, hence the large number of dependencies.
- From a DI perspective, is is usual to have so many parameters passed to a constructor?
- The general approach concerns me from an encapsulation and scoping aspect. All dependencies can be used from anywhere within a class, even if they should (strictly speaking) only be used from, for example, a single method