我目前正在嘗試找到一種更好的方法來在另一個類服務的建構式中呼叫服務的函式。我對我正在做的事情有一種不好的感覺,并認為我正在與 ASP .Net Core 的基礎作斗爭。我的問題是,我是在與 .Net 斗爭嗎?有沒有更好的方法讓我不創建 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()
));
我想我可以做這樣的事情:
services.AddScoped<ISQLiteSettingsRepo, SQLiteSettingsRepo>(x => new SQLiteSettingsRepo(
"./Settings.db",
service.GetService<ICryptionService>().GetSQLitePassword()
));
uj5u.com熱心網友回復:
為什么需要零件:x => new SQLiteSettingsRepo(...?您應該ICryptionService在建構式上注入服務SQLiteSettingsRepo
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/397982.html
