0

I've read through many question on dependency injection in WinForms, with this being the most popular one. I was shocked to discover that every example I've seen on this website uses a library for this job, such as Microsoft.Extensions.DependencyInjection. This gives me my question: Can WinForms have dependencies injected without using a dependency injection framework?

For example, suppose that I have Form1 and Form2. Opening the application opens Form1. Form1 has a single button, which opens Form2. Both forms must have a SqlConnection injected. Can this be done without a dependency injection framework? For example, what forbids us from using plain-old constructor injection? I would expect Application.Run(new Form1(MySqlConnectionHere)) to work in Main()

J. Mini
  • 1,868
  • 1
  • 9
  • 38
  • Nobody is stopping you from *injecting* anything in a custom Constructor, so it's not clear what you're asking, IMO -- Not related to Dependency Injection, though – Jimi May 17 '23 at 19:00
  • 1
    **Yes**, you can perform DI without an IoC container just by supplying the dependencies yourself in the constructor. It gets a little tricky when the dependencies themselves have dependencies, so people tend to use an IoC container, but it certainly isn't mandatory. – John Wu May 17 '23 at 19:14
  • DI is a [design pattern](https://en.wikipedia.org/wiki/Dependency_injection). period. A DI framework is just a tool that will reduce the boilerplate – Sir Rufo May 18 '23 at 01:35

1 Answers1

0

Yes.

See Mark Seemann's Pure DI concept.

David Osborne
  • 6,436
  • 1
  • 21
  • 35