This example demonstrates the creation of a Web API application in the pure DI paradigm using the Pure.DI code generator.
Composition setup file is Composition.cs:
using Pure.DI;
using Controllers;
using Microsoft.AspNetCore.Mvc;
using Pure.DI.MS;
using WeatherForecast;
using static Pure.DI.Lifetime;
internal partial class Composition: ServiceProviderFactory<Composition>
{
void Setup() => DI.Setup()
// Use the DI setup from the base class
.DependsOn(Base)
.Bind().As(Singleton).To<WeatherForecastService>()
// Registers controllers as roots
.Roots<ControllerBase>();
}
The composition class inherits from the ServiceProviderFactory<T>
class, where T is the composition class itself. It depends on the Base
setup.
The web application entry point is in the Program.cs file:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews().AddControllersAsServices();
// Uses Composition as an alternative IServiceProviderFactory
builder.Host.UseServiceProviderFactory(new Composition());
The project file looks like this:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Pure.DI" Version="2.1.56">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Pure.DI.MS" Version="2.1.56" />
</ItemGroup>
</Project>
It contains additional references to NuGet packages:
Pure.DI | DI source code generator | |
Pure.DI.MS | Add-ons on Pure.DI to work with Microsoft DI |