This example demonstrates the creation of a WinForms application in the pure DI paradigm using the Pure.DI code generator.
The composition definition is in the file Composition.cs. Remember to define all the necessary roots of the composition, for example, this could be a main form such as FormMain:
using Pure.DI;
using static Pure.DI.Lifetime;
internal partial class Composition
{
void Setup() => DI.Setup()
// Provides the composition root for main form
.Root<Owned<FormMain>>(nameof(Root))
// Forms
.Bind().As(Singleton).To<FormMain>()
// View Models
.Bind().To<ClockViewModel>()
// Models
.Bind().To<Log<TT>>()
.Bind().To(_ => TimeSpan.FromSeconds(1))
.Bind().To<Clock.Models.Timer>()
.Bind().As(PerBlock).To<SystemClock>()
// Infrastructure
.Bind().To<Dispatcher>();
}
A single instance of the Composition class is defined in the Main method of the Program.cs file:
public static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
using var composition = new Composition();
using var root = composition.Root;
Application.Run(root.Value);
}
}
The project file looks like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<UseWPF>true</UseWPF>
<TargetFramework>net9.0-windows</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pure.DI" Version="2.1.41">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
It contains an additional reference to the NuGet package:
Pure.DI | DI Source code generator |