I'm currently trying to find a better way to call a function of a service in the constructor of another class service. I have a bad feeling about what I'm doing and think I'm fighting the foundation of ASP .Net Core. My question is, am I fighting the .Net way? Is there a better way where I'm not creating a new instance of CryptionService?
//this singleton has a function called GetSQLitePassword that returns a string
services.AddSingleton<ICryptionService, CryptionService>();
// Use value returned from cryptionService
services.AddScoped<ISQLiteSettingsRepo,
SQLiteSettingsRepo>(x => new SQLiteSettingsRepo(
"./Settings.db",
new CryptionService().GetSQLitePassword()
));
I'd like to think that I could do something like this:
services.AddScoped<ISQLiteSettingsRepo, SQLiteSettingsRepo>(x => new SQLiteSettingsRepo(
"./Settings.db",
service.GetService<ICryptionService>().GetSQLitePassword()
));
CodePudding user response:
Why do you need the part: x => new SQLiteSettingsRepo(...
?
You should inject the ICryptionService
service on the constructor of SQLiteSettingsRepo