我的 ASP.NET 核心 MVVM Blazor 应用程序的依赖注入无法正常工作
Dependency injection not working properly for my ASP.NET Core MVVM Blazor application
我在尝试应用依赖项注入时遇到问题。经过大量研究并查看 YouTube 上的各种视频和 Stack overflow 上的答案后,我的 ITaskRepository 一直返回 null 而不是我的存储库的实例。查看我的代码,似乎我已经添加了所有正确的东西来使依赖注入工作。
我的基础存储库界面
using portfolio_backend.Data.Base;
using System.Collections.Generic;
namespace portfolio_backend.Business.Repositories.Base
{
public interface IBaseRepository<TEntity> where TEntity : BaseModel
{
void Add(TEntity model);
void Delete(TEntity model);
bool Exists(int Id);
TEntity Get(int Id);
IEnumerable<TEntity> GetAll();
void Update(int Id, TEntity model);
}
}
我的 BaseRepository class
using Microsoft.EntityFrameworkCore.Internal;
using portfolio_backend.Data;
using portfolio_backend.Data.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace portfolio_backend.Business.Repositories.Base
{
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : BaseModel
{
protected PortfolioContext _context;
public BaseRepository(PortfolioContext context)
{
_context = context;
}
public void Add(TEntity model)
{
if (!Exists(model.Id))
{
_context.Set<TEntity>().Add(model);
_context.SaveChanges();
}
}
public void Delete(TEntity model)
{
if (Exists(model.Id))
{
_context.Set<TEntity>().Remove(model);
_context.SaveChanges();
}
}
public bool Exists(int Id)
{
return _context.Set<TEntity>().Any(model => model.Id == Id);
}
public TEntity Get(int Id)
{
return _context.Set<TEntity>().FirstOrDefault(model => model.Id == Id);
}
public IEnumerable<TEntity> GetAll()
{
return _context.Set<TEntity>().ToList();
}
public void Update(int Id, TEntity model)
{
var modelToFind = Get(Id);
_context.Set<TEntity>().Update(modelToFind);
_context.SaveChanges();
}
}
}
我的 ITaskRepository 界面
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Data;
using System.Collections.Generic;
namespace portfolio_backend.Business.Repositories
{
public interface ITaskRepository : IBaseRepository<Task>
{
IEnumerable<Task> GetTaskByProjects(int ProjectId);
}
}
TaskRepository 实现
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Data;
using System.Collections.Generic;
using System.Linq;
namespace portfolio_backend.Business.Repositories
{
public class TaskRepository : BaseRepository<Task>, ITaskRepository
{
public TaskRepository(PortfolioContext context) : base(context)
{
}
public IEnumerable<Task> GetTaskByProjects(int ProjectId)
{
return _context.Tasks.OrderByDescending(task => task.Project.Id == ProjectId).ToList();
}
}
}
我的创业公司class:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Business.Repositories;
using portfolio_backend.Data;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
namespace portfolio_backend.Presentation
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<PortfolioContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("portfolio")));
services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
services.AddScoped<ITaskRepository, TaskRepository>();
services.AddBlazorise(options =>{
options.ChangeTextOnKeyPress = true;})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
services.AddRazorPages();
services.AddServerSideBlazor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.ApplicationServices
.UseBootstrapProviders()
.UseFontAwesomeIcons();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
我正在尝试为以下两个 classes 应用依赖注入:
Tasks.razor.cs(Blazor 组件的代码隐藏)
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel => new TaskViewModel(_taskRepository);
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
以及此组件的视图模型
using portfolio_backend.Business.Repositories;
using portfolio_backend.Data;
using System.Collections.Generic;
namespace portfolio_backend.Business
{
public class TaskViewModel
{
private ITaskRepository _taskRepository {get; set;}
private List<Task> _allTasks;
public TaskViewModel(ITaskRepository repository)
{
_taskRepository = repository;
}
public List<Task> AllTasks
{
get => _allTasks;
set => _taskRepository.GetAll();
}
public void SeedTasks()
{
_taskRepository.Add( new Task { Description = "Task 1"} );
_taskRepository.Add(new Task { Description = "Task 2" });
_taskRepository.Add(new Task { Description = "Task 3" });
}
}
}
_taskRepository 总是returns null,这是出现的错误信息:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
我该怎么做才能解决这个问题?或者在这种情况下如何更好地应用 DI?
更新:
我根据评论中建议的解决方案之一进行了以下更改:
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel;
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
这将触发以下错误:
MissingMethodException: No parameterless constructor defined for type 'portfolio_backend.Presentation.Pages.Tasks'.
根据错误提示,我添加了一个额外的无参数构造函数
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel;
public Tasks()
{
}
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
上述更改造成了相同的问题,即 taskRepository 为空。
_taskRepository
必须是 属性 或构造函数参数。您拥有它作为 class 成员。不能这样注入。
您必须在启动文件的 ConfigureServices(IServiceCollection services) 中注册您的依赖项:
services.AddScoped<ITaskRepository, TaskRepository>();
or
services.AddTransient<ITaskRepository, TaskRepository>();
你必须决定什么更适合你的应用程序。
根据您对初始问题所做的编辑,您似乎在未注册的 class 上使用依赖注入机制:Tasks。这个class是如何实现的?
如果您想在特定 class 上使用 DI,您应该像注册 ITaskRepository 一样注册它。
将以下行添加到您的 ConfigureServices() 方法中:
services.AddScoped<Tasks>();
在这个场景中有两个主要挑战。
第一个是我没有正确设计我的应用程序。我的意思是,我最初打算在我的存储库实例上使用依赖注入,以便能够像这样在我的代码中创建我的 TaskViewModel 实例。
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
作为我的解决方案的一部分,更好的方法是还为我的 TaskViewModel 创建一个接口,这样我就可以在我的 Blazor 组件代码隐藏中使用依赖注入。 TaskViewModel 本身应该通过依赖注入拥有我的存储库的一个实例。
ITaskViewModel:
public interface ITaskViewModel : IBaseViewModel<Task>
{
List<Task> AllTasks { get; set; }
void SeedTasks();
}
我对 TaskViewModel 的实现
public class TaskViewModel : BaseViewModel<Task>, ITaskViewModel
{
private ITaskRepository _taskRepository;
private List<Task> _allTasks;
public TaskViewModel(ITaskRepository repository) : base(repository)
{
_taskRepository = repository;
}
public List<Task> AllTasks
{
get => _allTasks;
set
{
_allTasks = value;
}
}
public void SeedTasks()
{
var task1 = new Task { Description = "Task 1" };
var task2 = new Task { Description = "Task 2" };
var task3 = new Task { Description = "Task 3" };
_taskRepository.Add(task1);
_taskRepository.Add(task2);
_taskRepository.Add(task3);
}
}
Startup.cs 文件的 ConfigureServices 方法上的组件注册
services.AddScoped<ITaskViewModel, TaskViewModel>();
第二个问题是我无法使用构造函数或成员 属性 方法在 Blazor 组件代码隐藏中使用依赖注入。不确定这是否同样适用于剃须刀页面代码隐藏,但是您将依赖注入用于 Blazor 组件代码隐藏的方式是使用 Inject 属性
Tasks.razor.cs
[Inject]
private ITaskViewModel _viewModel { get; set; }
请确保您还安装了以下 Nuget 程序包,以便 Inject 属性正常工作。
using Microsoft.AspNetCore.Components;
我在尝试应用依赖项注入时遇到问题。经过大量研究并查看 YouTube 上的各种视频和 Stack overflow 上的答案后,我的 ITaskRepository 一直返回 null 而不是我的存储库的实例。查看我的代码,似乎我已经添加了所有正确的东西来使依赖注入工作。
我的基础存储库界面
using portfolio_backend.Data.Base;
using System.Collections.Generic;
namespace portfolio_backend.Business.Repositories.Base
{
public interface IBaseRepository<TEntity> where TEntity : BaseModel
{
void Add(TEntity model);
void Delete(TEntity model);
bool Exists(int Id);
TEntity Get(int Id);
IEnumerable<TEntity> GetAll();
void Update(int Id, TEntity model);
}
}
我的 BaseRepository class
using Microsoft.EntityFrameworkCore.Internal;
using portfolio_backend.Data;
using portfolio_backend.Data.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace portfolio_backend.Business.Repositories.Base
{
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : BaseModel
{
protected PortfolioContext _context;
public BaseRepository(PortfolioContext context)
{
_context = context;
}
public void Add(TEntity model)
{
if (!Exists(model.Id))
{
_context.Set<TEntity>().Add(model);
_context.SaveChanges();
}
}
public void Delete(TEntity model)
{
if (Exists(model.Id))
{
_context.Set<TEntity>().Remove(model);
_context.SaveChanges();
}
}
public bool Exists(int Id)
{
return _context.Set<TEntity>().Any(model => model.Id == Id);
}
public TEntity Get(int Id)
{
return _context.Set<TEntity>().FirstOrDefault(model => model.Id == Id);
}
public IEnumerable<TEntity> GetAll()
{
return _context.Set<TEntity>().ToList();
}
public void Update(int Id, TEntity model)
{
var modelToFind = Get(Id);
_context.Set<TEntity>().Update(modelToFind);
_context.SaveChanges();
}
}
}
我的 ITaskRepository 界面
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Data;
using System.Collections.Generic;
namespace portfolio_backend.Business.Repositories
{
public interface ITaskRepository : IBaseRepository<Task>
{
IEnumerable<Task> GetTaskByProjects(int ProjectId);
}
}
TaskRepository 实现
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Data;
using System.Collections.Generic;
using System.Linq;
namespace portfolio_backend.Business.Repositories
{
public class TaskRepository : BaseRepository<Task>, ITaskRepository
{
public TaskRepository(PortfolioContext context) : base(context)
{
}
public IEnumerable<Task> GetTaskByProjects(int ProjectId)
{
return _context.Tasks.OrderByDescending(task => task.Project.Id == ProjectId).ToList();
}
}
}
我的创业公司class:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using portfolio_backend.Business.Repositories.Base;
using portfolio_backend.Business.Repositories;
using portfolio_backend.Data;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
namespace portfolio_backend.Presentation
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<PortfolioContext>(options =>
options.UseMySQL(Configuration.GetConnectionString("portfolio")));
services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
services.AddScoped<ITaskRepository, TaskRepository>();
services.AddBlazorise(options =>{
options.ChangeTextOnKeyPress = true;})
.AddBootstrapProviders()
.AddFontAwesomeIcons();
services.AddRazorPages();
services.AddServerSideBlazor();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.ApplicationServices
.UseBootstrapProviders()
.UseFontAwesomeIcons();
app.UseEndpoints(endpoints =>
{
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
我正在尝试为以下两个 classes 应用依赖注入:
Tasks.razor.cs(Blazor 组件的代码隐藏)
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel => new TaskViewModel(_taskRepository);
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
以及此组件的视图模型
using portfolio_backend.Business.Repositories;
using portfolio_backend.Data;
using System.Collections.Generic;
namespace portfolio_backend.Business
{
public class TaskViewModel
{
private ITaskRepository _taskRepository {get; set;}
private List<Task> _allTasks;
public TaskViewModel(ITaskRepository repository)
{
_taskRepository = repository;
}
public List<Task> AllTasks
{
get => _allTasks;
set => _taskRepository.GetAll();
}
public void SeedTasks()
{
_taskRepository.Add( new Task { Description = "Task 1"} );
_taskRepository.Add(new Task { Description = "Task 2" });
_taskRepository.Add(new Task { Description = "Task 3" });
}
}
}
_taskRepository 总是returns null,这是出现的错误信息:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
我该怎么做才能解决这个问题?或者在这种情况下如何更好地应用 DI?
更新:
我根据评论中建议的解决方案之一进行了以下更改:
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel;
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
这将触发以下错误:
MissingMethodException: No parameterless constructor defined for type 'portfolio_backend.Presentation.Pages.Tasks'.
根据错误提示,我添加了一个额外的无参数构造函数
using portfolio_backend.Business;
using portfolio_backend.Business.Repositories;
namespace portfolio_backend.Presentation.Pages
{
public partial class Tasks
{
private ITaskRepository _taskRepository;
private TaskViewModel _taskViewModel;
public Tasks()
{
}
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
protected override void OnInitialized()
{
_taskViewModel.SeedTasks();
}
}
}
上述更改造成了相同的问题,即 taskRepository 为空。
_taskRepository
必须是 属性 或构造函数参数。您拥有它作为 class 成员。不能这样注入。
您必须在启动文件的 ConfigureServices(IServiceCollection services) 中注册您的依赖项:
services.AddScoped<ITaskRepository, TaskRepository>();
or
services.AddTransient<ITaskRepository, TaskRepository>();
你必须决定什么更适合你的应用程序。
根据您对初始问题所做的编辑,您似乎在未注册的 class 上使用依赖注入机制:Tasks。这个class是如何实现的?
如果您想在特定 class 上使用 DI,您应该像注册 ITaskRepository 一样注册它。
将以下行添加到您的 ConfigureServices() 方法中:
services.AddScoped<Tasks>();
在这个场景中有两个主要挑战。
第一个是我没有正确设计我的应用程序。我的意思是,我最初打算在我的存储库实例上使用依赖注入,以便能够像这样在我的代码中创建我的 TaskViewModel 实例。
public Tasks(ITaskRepository repository)
{
_taskRepository = repository;
_taskViewModel = new TaskViewModel(_taskRepository);
}
作为我的解决方案的一部分,更好的方法是还为我的 TaskViewModel 创建一个接口,这样我就可以在我的 Blazor 组件代码隐藏中使用依赖注入。 TaskViewModel 本身应该通过依赖注入拥有我的存储库的一个实例。
ITaskViewModel:
public interface ITaskViewModel : IBaseViewModel<Task>
{
List<Task> AllTasks { get; set; }
void SeedTasks();
}
我对 TaskViewModel 的实现
public class TaskViewModel : BaseViewModel<Task>, ITaskViewModel
{
private ITaskRepository _taskRepository;
private List<Task> _allTasks;
public TaskViewModel(ITaskRepository repository) : base(repository)
{
_taskRepository = repository;
}
public List<Task> AllTasks
{
get => _allTasks;
set
{
_allTasks = value;
}
}
public void SeedTasks()
{
var task1 = new Task { Description = "Task 1" };
var task2 = new Task { Description = "Task 2" };
var task3 = new Task { Description = "Task 3" };
_taskRepository.Add(task1);
_taskRepository.Add(task2);
_taskRepository.Add(task3);
}
}
Startup.cs 文件的 ConfigureServices 方法上的组件注册
services.AddScoped<ITaskViewModel, TaskViewModel>();
第二个问题是我无法使用构造函数或成员 属性 方法在 Blazor 组件代码隐藏中使用依赖注入。不确定这是否同样适用于剃须刀页面代码隐藏,但是您将依赖注入用于 Blazor 组件代码隐藏的方式是使用 Inject 属性
Tasks.razor.cs
[Inject]
private ITaskViewModel _viewModel { get; set; }
请确保您还安装了以下 Nuget 程序包,以便 Inject 属性正常工作。
using Microsoft.AspNetCore.Components;