Constructor Assignment (Injection without boilerplate) #4824
Answered
by
HaloFour
LukeSamkharadze
asked this question in
Language Ideas
-
Imagine we have a container with injectables and one service that depends on that other services
public class AuthService {
private AppDbContext appDb;
private EmailAuthService emailAuthService;
private ILogger<AuthService> logger;
private IOptions<JwtSettings> jwtSettings;
private ScopeTrackerService scopeTrackerService;
private LocationService locationService;
private MessageService messageService;
private BlobService blobService;
private MyProfileService myProfileService;
private UserKeysService userKeysService;
// Constructor that just stores injected objects
// Boilerplate isn't it?
public AuthService(
AppDbContext appDb,
ScopeTrackerService scopeTrackerService,
ILogger<AuthService> logger,
EmailAuthService emailAuthService
ScopeTrackerService scopeTrackerService.
LocationService locationService,
MessageService messageService,
BlobService blobService,
MyProfileService myProfileService,
UserKeysService userKeysService
) {
this.emailAuthService = emailAuthService;
this.logger = logger;
this.jwtSettings = jwtSettings;
this.appDb = appDb;
this.scopeTrackerService = scopeTrackerService;
this.locationService = locationService;
this.messageService = messageService;
this.blobService = blobService;
this.myProfileService = myProfileService;
this.userKeysService = userKeysService;
}
...
} What if we had constructor syntax the same as we have in Typescript Constructor Assignment We would have
public class AuthService {
public AuthService(
private AppDbContext appDb;
private EmailAuthService emailAuthService;
private ILogger<AuthService> logger;
private IOptions<JwtSettings> jwtSettings;
private ScopeTrackerService scopeTrackerService;
private LocationService locationService;
private MessageService messageService;
private BlobService blobService;
private MyProfileService myProfileService;
private UserKeysService userKeysService) { }
...
} It would automatically store injected variables |
Beta Was this translation helpful? Give feedback.
Answered by
HaloFour
Jun 12, 2021
Replies: 1 comment 2 replies
-
See: #4025 |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
LukeSamkharadze
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See: #4025