.net - Reducing number of dependencies in coordination classes -
.net - Reducing number of dependencies in coordination classes -
in class coordination work of dependent services, there strategy cut down number of dependencies?
for example, consider taking donation. coordinating class might like:
public class donationservice { public donationservice( idonationvalidator validator, icreditcardgateway creditcardgateway, iservicebus servicebus, idatabasesession dbsession, ilogger logger ) { … } public donationresult handledonation(donationrequest request) { // 1. validate // 2. charge credit card // 3. record in database // 4. publish message service bus } }
if there 1 more step in process, see 6th dependency arise. perhaps logging cross-cutting concern, that's debatable.
there number of steps need taken within single responsibility of taking donation. i'm not including steps "send email" because i'm relying on separate process when receiving donation message via service bus.
i'm not sure if create facade service on subset of dependencies in grouping , not sure else should do. suggestions on removing code smell appreciated.
i've run scenarios similar 1 , turns out (always) due poor design...actually, couple senior architects pointed out me in past. usually, because of giving much responsibility single object. now, know full-leg re-design not smartest or affordable way address issue, should consider in first place when have design issue. can avoid bloated constructor? here options:
1- provide default implementation, add together default constructor initializes default implementation of these dependencies...perhaps using mill object don't end initializing concrete object implementations. overload constructor having these overloads phone call default constructor
2- delegate responsibility kind of "intermediary" object, illustration might want move servicebus , database responsibility a separate object , move logging these dependencies
.net dependency-injection
Comments
Post a Comment