How to define dependency injection in Winforms C#?
Interface ICategory:
public interface ICategory
{
void Save();
}
Class CategoryRepository:
public class CategoryRepository : ICategory
{
private readonly ApplicationDbContext _context;
public CategoryRepository(ApplicationDbContext contex)
{
_context = contex;
}
public void Save()
{
_context.SaveChanges();
}
}
Form1:
public partial class Form1 : Form
{
private readonly ICategury _ic;
public Form1(ICategury ic)
{
InitializeComponent();
_ic=ic
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm= new Form2();
frm.show();
}
}
Form2:
public partial class Form2 : Form
{
private readonly ICategury _ic;
public Form2(ICategury ic)
{
InitializeComponent();
_ic=ic
}
}
Problem?
Definition of dependency injection in Program.cs
Application.Run(new Form1());
Definition of dependency injection at the time of Form 2 call
Form2 frm= new Form2(); frm.show();
There's another approach than the one described by Reza in his answer. This answer of mine was originally published on my blog , however, since Stack Overflow is the primary source of information for most of us, the blog post linked in a comment below Reza's answer could easily be missed.
Here goes then. This solution is based on the Local Factory pattern.
We'll start with a form factory
public interface IFormFactory
{
Form1 CreateForm1();
Form2 CreateForm2();
}
public class FormFactory : IFormFactory
{
static IFormFactory _provider;
public static void SetProvider( IFormFactory provider )
{
_provider = provider;
}
public Form1 CreateForm1()
{
return _provider.CreateForm1();
}
public Form2 CreateForm2()
{
return _provider.CreateForm2();
}
}
From now on, this factory is the primary client's interface to creating forms. The client code is no longer supposed to just call
var form1 = new Form1();
No, it's forbidden. Instead, the client should always call
var form1 = new FormFactory().CreateForm1();
(and similarily for other forms).
Note that while the factory is implemented, it doesn't do anything on its own. Instead it delegates the creation to a somehow mysterious provider which has to be injected into the factory, The idea behind this is that the provider will be injected, once, in the Composition Root which is a place in the code, close to the startup, and very high in the application stack so that all dependencies can be resolved there. So, the form factory doesn't need to know what provider will be ultimately injected into it.
This approach has a significant advantage - depending on actual requirements, different providers can be injected, for example you could have a DI-based provider (we'll write it in a moment) for an actual application and a stub provider for unit tests.
Anyway, let's have a form with a dependency:
public partial class Form1 : Form
{
private IHelloWorldService _service;
public Form1(IHelloWorldService service)
{
InitializeComponent();
this._service = service;
}
}
This form depends on a service and the service will be provided by the constructor. If the Form1 needs to create another form, Form2, it does it it a way we already discussed:
var form2 = new FormFactory().CreateForm2();
Things become more complicated, though, when a form needs not only dependant services but also, just some free parameters (strings, ints etc.). Normally, you'd have a constructor
public Form2( string something, int somethingElse ) ...
but now you need something more like
public Form2( ISomeService service1, IAnotherService service2,
string something, int somethingElse ) ...
This is something we should really take a look into. Look once again, a real-life form possibly needs
How do we handle that?
To have a complete example, let's then modify the form factory
public interface IFormFactory
{
Form1 CreateForm1();
Form2 CreateForm2(string something);
}
public class FormFactory : IFormFactory
{
static IFormFactory _provider;
public static void SetProvider( IFormFactory provider )
{
_provider = provider;
}
public Form1 CreateForm1()
{
return _provider.CreateForm1();
}
public Form2 CreateForm2(string something)
{
return _provider.CreateForm2(something);
}
}
And let's see how forms are defined
public partial class Form1 : Form
{
private IHelloWorldService _service;
public Form1(IHelloWorldService service)
{
InitializeComponent();
this._service = service;
}
private void button1_Click( object sender, EventArgs e )
{
var form2 = new FormFactory().CreateForm2("foo");
form2.Show();
}
}
public partial class Form2 : Form
{
private IHelloWorldService _service;
private string _something;
public Form2(IHelloWorldService service, string something)
{
InitializeComponent();
this._service = service;
this._something = something;
this.Text = something;
}
}
Can you see a pattern here?
Now finally to the Composition Root . Let's start with the service
public interface IHelloWorldService
{
string DoWork();
}
public class HelloWorldServiceImpl : IHelloWorldService
{
public string DoWork()
{
return "hello world service::do work";
}
}
Note that while the interface is supposed to be somewhere down in the stack (to be recognized by everyone), the implementation is free to be provided anywhere (forms don't need reference to the implementation.), Then, follows the starting code where the form factory is finally provided and the container is set up
internal static class Program
{
[STAThread]
static void Main()
{
var formFactory = CompositionRoot();
ApplicationConfiguration.Initialize();
Application.Run(formFactory.CreateForm1());
}
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services) => {
services.AddTransient<IHelloWorldService, HelloWorldServiceImpl>();
services.AddTransient<Form1>();
services.AddTransient<Func<string,Form2>>(
container =>
something =>
{
var helloWorldService =
container.GetRequiredService<IHelloWorldService>();
return new Form2(helloWorldService, something);
});
});
}
static IFormFactory CompositionRoot()
{
// host
var hostBuilder = CreateHostBuilder();
var host = hostBuilder.Build();
// container
var serviceProvider = host.Services;
// form factory
var formFactory = new FormFactoryImpl(serviceProvider);
FormFactory.SetProvider(formFactory);
return formFactory;
}
}
public class FormFactoryImpl : IFormFactory
{
private IServiceProvider _serviceProvider;
public FormFactoryImpl(IServiceProvider serviceProvider)
{
this._serviceProvider = serviceProvider;
}
public Form1 CreateForm1()
{
return _serviceProvider.GetRequiredService<Form1>();
}
public Form2 CreateForm2(string something)
{
var _form2Factory = _serviceProvider.GetRequiredService<Func<string, Form2>>();
return _form2Factory( something );
}
}
First note how the container is created with Host.CreateDefaultBuilder, an easy task. Then note how services are registered and how forms are registered among other services.
This is straightforward for forms that don't have any dependencies, it's just
services.AddTransient<Form1>();
However, if a form needs both services and free parameters, it's registered as... form creation function, a Func of any free parameters that returns actual form. Take a look at this
services.AddTransient<Func<string,Form2>>(
container =>
something =>
{
var helloWorldService = container.GetRequiredService<IHelloWorldService>();
return new Form2(helloWorldService, something);
});
That's clever. We register a form factory function using one of registration mechanisms that itself uses a factory function (yes, a factory that uses another factory, a Factception. Feel free to take a short break if you feel lost here). Our registered function, the Func<string, Form2> has a single parameter, the something (that corresponds to the free parameter of the form constructor) but its other dependencies are resolved... by the container (which is what we wanted).
This is why the actual form factory needs to pay attention of what it resolves. A simple form is resolved as follows
return _serviceProvider.GetRequiredService<Form1>();
where the other is resolved in two steps. We first resolve the factory function and then use the creation's method parameter to feed it to the function:
var _form2Factory = _serviceProvider.GetRequiredService<Func<string, Form2>>();
return _form2Factory( something );
And, that's it. Whenever a form is created, is either
new FormFactory().CreateForm1();
for "simple" forms (with service dependencies only) or just
new FormFactory().CreateForm2("foo");
for forms that need both service dependncies and other free parameters.
To use DI in a WinForms .NET 5 or 6 you can do the following steps:
Create a WinForms .NET Application
Install Microsoft.Extensions.Hosting package (which gives you a bunch of useful features like DI, Logging, Configurations, and etc.)
Add a new interface, IHelloService.cs
:
public interface IHelloService { string SayHello(); }
Add a new implementation for your service HelloService.cs
:
public class HelloService: IHelloService { public string SayHello() { return "Hello, world;"; } }
Modify the Program.cs
:
static class Program { [STAThread] static void Main() { Application.SetHighDpiMode(HighDpiMode.SystemAware); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var host = CreateHostBuilder().Build(); ServiceProvider = host.Services; Application.Run(ServiceProvider.GetRequiredService<Form1>()); } public static IServiceProvider ServiceProvider { get; private set; } static IHostBuilder CreateHostBuilder() { return Host.CreateDefaultBuilder().ConfigureServices((context, services)=>{ services.AddTransient<IHelloService, HelloService>(); services.AddTransient<Form1>(); }); } }
Now you can inject IHelloService
in Form1
and use it:
public partial class Form1 : Form
{
private readonly IHelloService helloService;
public Form1(IHelloService helloService, Form2 form2)
{
InitializeComponent();
this.helloService = helloService;
MessageBox.Show(helloService.SayHello());
}
}
If you want to show Form2
using DI, you first need to register it services.AddTransient<Form2>();
, then depending to the usage of Form2, you can use either of the following options:
If you only need a single instance of Form2
in the whole life time of Form1
, then you can inject it as a dependency to the constructor of Form1
and store the instance and show it whenever you want.
But please pay attention: it will be initialized just once, when you open Form1
and it will not be initialized again. You also should not dispose it, because it's the only instance passed to Form1
.
public Form1(IHelloService helloService, Form2 form2) { InitializeComponent(); form2.ShowDialog(); }
If you need multiple instances of Form2
or you need to initialize it multiple times, then you may get an instance of it like this:
using (var form2 = Program.ServiceProvider.GetRequiredService<Form2>()) form2.ShowDialog();
In winforms the constructors of forms should be default constructors. You should set the values that you wanted to pass in the constructor as a property. During Form Loading you can check if the property has been set, and if not act as desired: use a default value, or warn the operator, or close the form.
Of course, at least one of the forms should decide which ICategory should be injected. For example the main form:
public class MainForm
{
private ICategory Category {get; }
public MainForm()
{
InitializeComponent();
CategoryFactory factory = new CategoryFactory();
this.Category = factory.Create();
}
Later, the MainForm must Show Form1, for instance after a button click:
private void ShowForm1()
{
using (Form1 form = new Form1())
{
form.Category = this.Category;
var dlgResult = form.ShowDialog(this);
if (dlgResult == DialogResult.Ok)
{
this.ProcessDialogResult(...);
}
}
}
Form1:
class Form1 : ...
{
public Form1()
{
InitializeComponent(); // subscribe to event form load
}
public ICategory Category {get; }
public void FormLoading(object sender, ...)
{
// check if Category set, and report problems
if (this.Category == null)
{
this.LogMissingCategory();
this.WarnOperatorMissingCategory();
this.Close();
}
...
}
private void ShowForm2()
{
using (Form2 form = new Form2())
{
form.Category = this.Category;
var dlgResult = form.ShowDialog(this);
if (dlgResult == DialogResult.Ok)
{
...
}
}
}
}
If you have a lot of forms that need a Category, then apparently there is something like a CategoryForm:
class CategoryForm : Form
{
public ICategory Category {get; }
protected override void OnFormLoading(...)
{
// TODO check Category
base.OnFormLoading(...);
}
}
classs Form3 : CategoryForm {...}
Just wanted to add this here too as an alternative pattern for the IFormFactory. This is how I usually approach it.
The benefit is you don't need to keep changing your IFormFactory interface foreach form that you add and each set of parameters.
Load all forms when application starts and pass arguments to the show method or some other base method you can define on your forms.
internal static class Program
{
public static IServiceProvider ServiceProvider { get; private set; }
[STAThread]
static void Main()
{
ApplicationConfiguration.Initialize();
ServiceProvider = CreateHostBuilder().Build().Services;
Application.Run(ServiceProvider.GetService<Form1>());
}
static IHostBuilder CreateHostBuilder()
{
return Host.CreateDefaultBuilder()
.ConfigureServices((context, services) => {
services.AddSingleton<IFormFactory,FormFactory>();
services.AddSingleton<IProductRepository, ProductRepository>();
//Add all forms
var forms = typeof(Program).Assembly
.GetTypes()
.Where(t => t.BaseType == typeof(Form))
.ToList();
forms.ForEach(form =>
{
services.AddTransient(form);
});
});
}
}
Form Factory
public interface IFormFactory
{
T? Create<T>() where T : Form;
}
public class FormFactory : IFormFactory
{
private readonly IServiceScope _scope;
public FormFactory(IServiceScopeFactory scopeFactory)
{
_scope = scopeFactory.CreateScope();
}
public T? Create<T>() where T : Form
{
return _scope.ServiceProvider.GetService<T>();
}
}
Form 1
public partial class Form1 : Form
{
private readonly IFormFactory _formFactory;
public Form1(IFormFactory formFactory)
{
InitializeComponent();
_formFactory = formFactory;
}
private void button_Click(object sender, EventArgs e)
{
var form2 = _formFactory.Create<Form2>();
form2?.Show(99);
}
}
Form 2
public partial class Form2 : Form
{
private readonly IProductRepository _productRepository;
public Form2(IProductRepository productRepository)
{
InitializeComponent();
_productRepository = productRepository;
}
public void Show(int recordId)
{
var product = _productRepository.GetProduct(recordId);
//Bind your controls etc
this.Show();
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.