我正在开发一个新的应用程序,我选择了使用Blazor端。我有现有的后端设置,并试图只替换UI项目。我为所有服务都有一个注册中心,这个注册中心在API和Blazor项目中都使用:
public class DependencyContainer
{
public static void RegisterServices(IServiceCollection services)
{
#region Domain InMemoryBus MediatR
services.AddScoped<IMediatorHandler, InMemoryBus>();
#endregion
#region Domain Handlers
services.AddScoped<IRequestHandler<RadarCreateCommand, bool>, RadarProjectCommandHandler>();
#endregion
#region Application Layer
services.AddScoped<IRadarProjectService, RadarProjectService>();
//services.AddScoped<IContactService, ContactService>();
//services.AddScoped<ICategoryService, CategoryService>();
//services.AddScoped<ISubCategoryService, SubCategoryService>();
#endregion
#region Infrastructure/Data Layer
services.AddScoped<IPlaygroundBiRepository, PlaygroundBiRepository>();
//services.AddScoped<IContactRepository, ContactRepository>();
//services.AddScoped<ICategoryRepository, CategoryRepository>();
//services.AddScoped<ISubCategoryRepository, SubCategoryRepository>();
services.AddScoped<DesignTrackerContext>();
services.AddScoped<PlaygroundBiContext>();
#endregion
}
}在我的Blazor项目中,我有对注册表项目的引用,并且在'Startup.cs‘中调用这个方法为:
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
...
services.AddMediatR(typeof(Startup));
services.RegisterAutoMapper();
RegisterServices(services);
}现在,在我的页面上,我想显示一个项目列表。有一个服务将公开所有项目,非实体POCOs。该页面的设置如下:
@page "/projects"
@using DesignTracker.Application.Interfaces
@using DesignTracker.Application.ViewModels
<div class="container-fluid">
<div class="row my-4">
<div class="col-12">
<TelerikGrid Data="@allProjects" Height="550px" FilterMode="@GridFilterMode.FilterMenu"
Sortable="true" Pageable="true" PageSize="20" Groupable="true"
EditMode="@GridEditMode.Inline">
<GridColumns>
<GridColumn Field="Id" Title="Id" Width="100px" Editable="false" Groupable="false" />
<GridColumn Field="Date">
<Template>
@((context as RadarProjectViewModel).LastUpdateTime?.ToString("dddd, dd MMM yyyy"))
</Template>
</GridColumn>
<GridColumn Field="Name" />
<GridColumn Field="Department" />
<GridColumn Field="Summary" />
<GridCommandColumn>
<GridCommandButton Command="Save" Icon="@IconName.Save" ShowInEdit="true">Update</GridCommandButton>
<GridCommandButton Command="Edit" Icon="@IconName.Edit" Primary="true">Edit</GridCommandButton>
<GridCommandButton Command="Delete" Icon="@IconName.Delete">Delete</GridCommandButton>
<GridCommandButton Command="Cancel" Icon="@IconName.Cancel" ShowInEdit="true">Cancel</GridCommandButton>
</GridCommandColumn>
</GridColumns>
<GridToolBar>
<GridCommandButton Command="Add" Icon="@IconName.Plus" Primary="true">Add Project</GridCommandButton>
</GridToolBar>
</TelerikGrid>
</div>
</div>
</div>
@code {
private readonly IRadarProjectService _service;
private IEnumerable<RadarProjectViewModel> allProjects;
}如果我遵循WeatherService示例,即随Blazor模板一起提供,我就不会看到IoC在运行,因为在OnInitializedAsync()中调用了一个方法,它实际上初始化了预测:
List<WeatherForecast> forecasts { get; set; }
protected override async Task OnInitializedAsync()
{
await GetForecasts();
}
async Task GetForecasts()
{
forecasts = await ForecastService.GetForecastListAsync(DateTime.Now);
}根据我的MVC和Asp.Net核心经验,我所需要做的就是为我需要的服务创建支持接口,并创建一个加载的构造函数,DI引擎将在运行时注入初始化的对象。
在布拉泽的code{ ... }中这是怎么可能的
发布于 2020-03-12 06:45:13
若要将依赖项注入直接应用于blazor页面,请使用@inject指令。
示例
.razor页面:
@page "/"
@using YourServiceNamespace
@inject IYourService yourService // Here injecting dependency
<div>/*some html here*/</div>
@code
{
public string[] ArrayOfStrings => yourService.GetArrayOfStrings();
}Startup.cs文件:
services.AddScoped<IYourService, YourService>();Blazor将自动从应用程序环境接收您的服务,并将其直接注入到页面中,这样您就可以像使用普通的csharp代码一样使用它。
https://stackoverflow.com/questions/60648408
复制相似问题