DB服务的依赖注入
Dependency injection of DB service
我对 DI 有疑问。我有自定义 class,应该通过服务将一些数据导入数据库。如果我在 Blazor 组件中使用此服务,它与 @injection 配合使用效果很好。
但我无法在我的自定义中使用它 class。我试过这样
public class ImportXml
{
private readonly AccountService _accountService;
public ImportXml(AccountService accountService)
{
_accountService = accountService;
}
public ImportXml(List<Transaction> transactions, Account account)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
_accountService.UpdateAccountsAsync(account);
}
}
在startup.cs中是这样注册的服务。
services.AddScoped<AccountService>();
如果我调用 ImportXml _accountService 为空。
我目前的解决方法是将服务作为参数的一部分发送。但我想有 DI 的工作解决方案。
public class ImportXml
{
public ImportXml(List<Transaction> transactions, Account account, AccountService accountService)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
accountService.UpdateAccountsAsync(account);
}
}
非常感谢
你有几个问题。
- 为了让ImportXml通过DI接收AccountService,那么ImportXml也需要DI容器提供。
- 您正在使用第二个 构造函数 来尝试执行操作,而不是方法。构造函数应该只用于设置 class 起来,而不是执行任何操作,尤其是长 运行 操作,例如更新数据库。另外,请注意构造函数不是异步的。
更改您的 ImportXml class
public class ImportXml
{
private readonly AccountService _accountService;
// This it the constructor, so just set the object up
public ImportXml(AccountService accountService)
{
_accountService = accountService;
}
// This is the method to perform the update
public async Task DoAccountUpdateAsync(
List<Transaction> transactions,
Account account)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
await _accountService.UpdateAccountsAsync(account);
}
}
同时注册 AccountService 和 ImportXml
services.AddScoped<AccountService>();
services.AddScoped<ImportXml>();
从客户端注入 ImportXml,然后使用该服务。
ImportXml注入时,其构造函数参数(AccountService)会自动由DI提供并注入ImportXml。
@inject ImportXML ImportXML
<button @onclick=@HandleClick>Perform Account Update</button>
@code {
List<Transactions> _transactionsForMethod;
Account _accountForMethod;
async Task HandleClick()
{
await ImportXML.DoAccountUpdateAsync(
_transactionsForMethod,
_accoountForMethod);
}
}
我对 DI 有疑问。我有自定义 class,应该通过服务将一些数据导入数据库。如果我在 Blazor 组件中使用此服务,它与 @injection 配合使用效果很好。
但我无法在我的自定义中使用它 class。我试过这样
public class ImportXml
{
private readonly AccountService _accountService;
public ImportXml(AccountService accountService)
{
_accountService = accountService;
}
public ImportXml(List<Transaction> transactions, Account account)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
_accountService.UpdateAccountsAsync(account);
}
}
在startup.cs中是这样注册的服务。
services.AddScoped<AccountService>();
如果我调用 ImportXml _accountService 为空。
我目前的解决方法是将服务作为参数的一部分发送。但我想有 DI 的工作解决方案。
public class ImportXml
{
public ImportXml(List<Transaction> transactions, Account account, AccountService accountService)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
accountService.UpdateAccountsAsync(account);
}
}
非常感谢
你有几个问题。
- 为了让ImportXml通过DI接收AccountService,那么ImportXml也需要DI容器提供。
- 您正在使用第二个 构造函数 来尝试执行操作,而不是方法。构造函数应该只用于设置 class 起来,而不是执行任何操作,尤其是长 运行 操作,例如更新数据库。另外,请注意构造函数不是异步的。
更改您的 ImportXml class
public class ImportXml
{
private readonly AccountService _accountService;
// This it the constructor, so just set the object up
public ImportXml(AccountService accountService)
{
_accountService = accountService;
}
// This is the method to perform the update
public async Task DoAccountUpdateAsync(
List<Transaction> transactions,
Account account)
{
if(account.Transactions==null)
{
account.Transactions = new List<Transaction>();
}
account.Transactions.AddRange(transactions);
await _accountService.UpdateAccountsAsync(account);
}
}
同时注册 AccountService 和 ImportXml
services.AddScoped<AccountService>();
services.AddScoped<ImportXml>();
从客户端注入 ImportXml,然后使用该服务。 ImportXml注入时,其构造函数参数(AccountService)会自动由DI提供并注入ImportXml。
@inject ImportXML ImportXML
<button @onclick=@HandleClick>Perform Account Update</button>
@code {
List<Transactions> _transactionsForMethod;
Account _accountForMethod;
async Task HandleClick()
{
await ImportXML.DoAccountUpdateAsync(
_transactionsForMethod,
_accoountForMethod);
}
}