【问题标题】:Dependency injection not working properly for my ASP.NET Core MVVM Blazor application我的 ASP.NET Core MVVM Blazor 应用程序的依赖注入无法正常工作
【发布时间】:2020-11-08 20:55:18
【问题描述】:

我在尝试应用依赖注入时遇到问题。经过大量研究并查看 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 类


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();
        }
    }
}

我的创业班:

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");
            });
        }
    }
}

我正在尝试对以下两个类应用依赖注入:

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 总是返回 null,这是出现的错误信息:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

我能做些什么来解决这个问题?或者在这种情况下如何更好地应用 DI?

更新:

我根据 cmets 中建议的解决方案之一进行了以下更改:

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 仍然会返回 null
  • 我还添加了调试代码时出现的错误消息。
  • 您没有更改任务以使用构造函数注入

标签: c# asp.net-core dependency-injection blazor repository-pattern


【解决方案1】:

_taskRepository 必须是属性或构造函数参数。您拥有它作为班级成员。不能这样注入。

【讨论】:

  • 我不太确定我是否理解。我为 _taskRepository 添加了一个默认设置以使其成为一个属性,但它仍然返回一个空值。我对这一切还是陌生的。您能否举例说明如何正确执行此操作?
  • 你能显示你添加的代码吗?此外,我不确定 ASP.Net 是否支持属性注入,尤其是默认情况下。尝试使用构造函数注入。
  • 它仍然不起作用,我使用您建议的解决方案发布了更新。
【解决方案2】:

您必须在启动文件的 ConfigureServices(IServiceCollection services) 中注册您的依赖项:


services.AddScoped<ITaskRepository, TaskRepository>();

or 

services.AddTransient<ITaskRepository, TaskRepository>();

您必须决定什么更适合您的应用。

【讨论】:

    【解决方案3】:

    根据您对最初问题所做的编辑,您似乎正在对未注册的类使用依赖注入机制:任务。这个类是如何实现的?

    如果您想在特定类上使用 DI,您应该像使用 ITaskRepository 一样注册它。

    将以下行添加到您的 ConfigureServices() 方法中:

            services.AddScoped<Tasks>();
    

    【讨论】:

    • 这个类实际上是一个剃须刀页面后面的代码。它没有以任何特定的方式实现,但我一定会试一试。
    【解决方案4】:

    在这种情况下有两个主要挑战。

    第一个问题是我没有正确设计我的应用程序。我的意思是,我最初打算在我的存储库实例上使用依赖注入,以便能够像这样在我的代码中创建我的 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 组件代码隐藏中使用依赖注入。不确定这是否同样适用于 razor 页面代码隐藏,但您对 Blazor 组件代码隐藏使用依赖注入的方式是使用 Inject 属性

    Tasks.razor.cs

    
     [Inject]
     private ITaskViewModel _viewModel { get; set; }
    

    确保您还安装了以下 Nuget 包以使 Inject 属性正常工作。

    using Microsoft.AspNetCore.Components;
    

    【讨论】:

      猜你喜欢
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2018-01-16
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      相关资源
      最近更新 更多