使用依赖注入初始化主窗体中的接口
Initialize interfaces in main form with dependency injection
这是我的代码隐藏形式 ExpressionOfNeeds
private readonly IExpressionOfNeeds _expressionOfNeeds;
public FrmExpressionOfNeeds(IExpressionOfNeeds expressionOfNeeds)
{
InitializeComponent();
_expressionOfNeeds = expressionOfNeeds;
}
private async void FrmExpressionOfNeeds_Load(object sender, EventArgs e)
{
GCData.DataSource = await _expressionOfNeeds.GetAllExpressionOfNeeds();
}
这是我 MainForm
背后的代码
private readonly IExpressionOfNeeds _expressionOfNeeds;
private readonly IService2 _service2;
private readonly IService3 _service3;
//and so on and so forth
public XtraMain()
{
InitializeComponent();
}
private void bbtnExpressionOfNeeds_ItemClick(object sender, ItemClickEventArgs e)
{
FrmExpressionOfNeeds frme = new(_expressionOfNeeds)
{
Name = "FrmExpressionOfNeeds"
};
ViewChildForm(frme);
}
private void bbtnForm2_ItemClick(object sender, ItemClickEventArgs e)
{
Form2 frme = new(_service2)
{
Name = "Form2"
};
ViewChildForm(frme);
}
private void bbtnForm3_ItemClick(object sender, ItemClickEventArgs e)
{
Form3 frme = new(_service3)
{
Name = "Form3"
};
ViewChildForm(frme);
}
等等等等
这是我 Program class
背后的代码
static void Main()
{
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<XtraMain>();
services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
services.AddSingleton<IExpressionOfNeeds, ExpressionOfNeeds>();
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var mainform = services.GetRequiredService<XtraMain>();
Application.Run(mainform);
}
}
问题是 _expressionOfNeeds 的值始终为空,我找不到初始化它的方法
更新
我有很多表单和很多接口
我仅将其限制为一个示例,因此代码不会太大。
我建议创建一个工厂来获取主表单所需的表单
public interface IFormFactory {
TForm Create<TForm>() where TForm : Form;
}
假设在这种情况下要创建的类型都派生自 Form
实施将使用服务提供商来解析提供的表单类型
public class FormFactory: IFormFactory {
private readonly IServiceProvider services;
public FormFactory(IServiceProvider services) {
this.services = services;
}
public TForm Create<TForm>() where TForm : Form {
return services.GetRequiredService<TForm>();
}
}
主窗体将需要依赖于工厂,以便它可以根据需要创建窗体
//...
private readonly IFormFactory factory;
public XtraMain(IFormFactory factory) {
InitializeComponent();
this.factory = factory;
}
private void bbtnExpressionOfNeeds_ItemClick(object sender, ItemClickEventArgs e) {
FrmExpressionOfNeeds frme = factory.Create<FrmExpressionOfNeeds>();
frme.Name = "FrmExpressionOfNeeds";
ViewChildForm(frme);
}
private void bbtnForm2_ItemClick(object sender, ItemClickEventArgs e) {
Form2 frme = factory.Create<Form2>();
frme.Name = "Form2";
ViewChildForm(frme);
}
private void bbtnForm3_ItemClick(object sender, ItemClickEventArgs e) {
Form3 frme = factory.Create<Form3>();
frme.Name = "Form3";
ViewChildForm(frme);
}
//...
确保服务提供商解决的所有问题都在启动时注册
static void Main() {
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) => {
services.AddScoped<XtraMain>();
services.AddTransient<FrmExpressionOfNeeds>();
services.AddTransient<Form2>();
services.AddTransient<Form3>();
services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
services.AddSingleton<IExpressionOfNeeds, ExpressionOfNeeds>();
services.AddSingleton<IFormFactory, FormFactory>();
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope()) {
IServiceProvider services = serviceScope.ServiceProvider;
XtraMain mainform = services.GetRequiredService<XtraMain>();
Application.Run(mainform);
}
}
这样,无论有多少表单和接口,所有依赖项都可以由容器解析和注入。
你快到了。您已将表单和服务注册到 DI 容器中,但忘记将接口注入到表单的构造函数中。
您必须将依赖接口添加到表单的构造函数(首选解决方案),或者稍后在需要时从服务提供商处获取它们的实例。
您可以在下面找到分步示例 post:
- How to use dependency injection in WinForms.
假设你有一个 MainForm
依赖于 IMyServie
,那么你应该有一个像这样的构造函数:
IMyService _myService;
public MainForm(IMyService myService)
{
_myService = myService;
}
然后,一旦您将 MainForm
和 IMyService
注册到 DI 容器并从服务提供商处获取实例,一切都会按预期进行。这是主要入口点:
static class Program
{
public static IServiceProvider ServiceProvider { get; private set; }
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var host = CreateHostBuilder().Build();
ServiceProvider = host.Services;
Application.Run(ServiceProvider.GetRequiredService<MainForm>());
}
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services)=>{
services.AddTransient<IMyService, MyService>();
services.AddTransient<MainForm>();
});
}
}
如果出于某种原因(就像我在链接答案中解释的那样)您需要获取服务实例而不将其注入构造函数,那么您可以使用 Program.ServiceProvider.GetRequiredService<SomeFormOrService>()
.
这是我的代码隐藏形式 ExpressionOfNeeds
private readonly IExpressionOfNeeds _expressionOfNeeds;
public FrmExpressionOfNeeds(IExpressionOfNeeds expressionOfNeeds)
{
InitializeComponent();
_expressionOfNeeds = expressionOfNeeds;
}
private async void FrmExpressionOfNeeds_Load(object sender, EventArgs e)
{
GCData.DataSource = await _expressionOfNeeds.GetAllExpressionOfNeeds();
}
这是我 MainForm
背后的代码private readonly IExpressionOfNeeds _expressionOfNeeds;
private readonly IService2 _service2;
private readonly IService3 _service3;
//and so on and so forth
public XtraMain()
{
InitializeComponent();
}
private void bbtnExpressionOfNeeds_ItemClick(object sender, ItemClickEventArgs e)
{
FrmExpressionOfNeeds frme = new(_expressionOfNeeds)
{
Name = "FrmExpressionOfNeeds"
};
ViewChildForm(frme);
}
private void bbtnForm2_ItemClick(object sender, ItemClickEventArgs e)
{
Form2 frme = new(_service2)
{
Name = "Form2"
};
ViewChildForm(frme);
}
private void bbtnForm3_ItemClick(object sender, ItemClickEventArgs e)
{
Form3 frme = new(_service3)
{
Name = "Form3"
};
ViewChildForm(frme);
}
等等等等
这是我 Program class
static void Main()
{
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<XtraMain>();
services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
services.AddSingleton<IExpressionOfNeeds, ExpressionOfNeeds>();
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope())
{
var services = serviceScope.ServiceProvider;
var mainform = services.GetRequiredService<XtraMain>();
Application.Run(mainform);
}
}
问题是 _expressionOfNeeds 的值始终为空,我找不到初始化它的方法
更新
我有很多表单和很多接口
我仅将其限制为一个示例,因此代码不会太大。
我建议创建一个工厂来获取主表单所需的表单
public interface IFormFactory {
TForm Create<TForm>() where TForm : Form;
}
假设在这种情况下要创建的类型都派生自 Form
实施将使用服务提供商来解析提供的表单类型
public class FormFactory: IFormFactory {
private readonly IServiceProvider services;
public FormFactory(IServiceProvider services) {
this.services = services;
}
public TForm Create<TForm>() where TForm : Form {
return services.GetRequiredService<TForm>();
}
}
主窗体将需要依赖于工厂,以便它可以根据需要创建窗体
//...
private readonly IFormFactory factory;
public XtraMain(IFormFactory factory) {
InitializeComponent();
this.factory = factory;
}
private void bbtnExpressionOfNeeds_ItemClick(object sender, ItemClickEventArgs e) {
FrmExpressionOfNeeds frme = factory.Create<FrmExpressionOfNeeds>();
frme.Name = "FrmExpressionOfNeeds";
ViewChildForm(frme);
}
private void bbtnForm2_ItemClick(object sender, ItemClickEventArgs e) {
Form2 frme = factory.Create<Form2>();
frme.Name = "Form2";
ViewChildForm(frme);
}
private void bbtnForm3_ItemClick(object sender, ItemClickEventArgs e) {
Form3 frme = factory.Create<Form3>();
frme.Name = "Form3";
ViewChildForm(frme);
}
//...
确保服务提供商解决的所有问题都在启动时注册
static void Main() {
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) => {
services.AddScoped<XtraMain>();
services.AddTransient<FrmExpressionOfNeeds>();
services.AddTransient<Form2>();
services.AddTransient<Form3>();
services.AddSingleton<ISqlDataAccess, SqlDataAccess>();
services.AddSingleton<IExpressionOfNeeds, ExpressionOfNeeds>();
services.AddSingleton<IFormFactory, FormFactory>();
});
var host = builder.Build();
using (var serviceScope = host.Services.CreateScope()) {
IServiceProvider services = serviceScope.ServiceProvider;
XtraMain mainform = services.GetRequiredService<XtraMain>();
Application.Run(mainform);
}
}
这样,无论有多少表单和接口,所有依赖项都可以由容器解析和注入。
你快到了。您已将表单和服务注册到 DI 容器中,但忘记将接口注入到表单的构造函数中。
您必须将依赖接口添加到表单的构造函数(首选解决方案),或者稍后在需要时从服务提供商处获取它们的实例。
您可以在下面找到分步示例 post:
- How to use dependency injection in WinForms.
假设你有一个 MainForm
依赖于 IMyServie
,那么你应该有一个像这样的构造函数:
IMyService _myService;
public MainForm(IMyService myService)
{
_myService = myService;
}
然后,一旦您将 MainForm
和 IMyService
注册到 DI 容器并从服务提供商处获取实例,一切都会按预期进行。这是主要入口点:
static class Program
{
public static IServiceProvider ServiceProvider { get; private set; }
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
var host = CreateHostBuilder().Build();
ServiceProvider = host.Services;
Application.Run(ServiceProvider.GetRequiredService<MainForm>());
}
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services)=>{
services.AddTransient<IMyService, MyService>();
services.AddTransient<MainForm>();
});
}
}
如果出于某种原因(就像我在链接答案中解释的那样)您需要获取服务实例而不将其注入构造函数,那么您可以使用 Program.ServiceProvider.GetRequiredService<SomeFormOrService>()
.