Skip to content

Commit 7ee0ea1

Browse files
committed
custom required property selector for #567 and #563
1 parent 4c897de commit 7ee0ea1

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

samples/BlazorServerSide/BlazorServerSide.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net7.0</TargetFramework>
55
<Nullable>enable</Nullable>
66
<ImplicitUsings>enable</ImplicitUsings>
7+
<LangVersion>11</LangVersion>
78
</PropertyGroup>
89

910
<ItemGroup>

samples/BlazorServerSide/Data/WeatherForecastService.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@ public class WeatherForecastService
77
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
88
};
99

10+
public required Func<WeatherForecast> WeatherForecastFactory { protected get; init; }
11+
1012
public Task<WeatherForecast[]> GetForecastAsync(DateOnly startDate)
1113
{
12-
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
14+
return Task.FromResult(Enumerable.Range(1, 5).Select(index =>
1315
{
14-
Date = startDate.AddDays(index),
15-
TemperatureC = Random.Shared.Next(-20, 55),
16-
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
16+
var f = WeatherForecastFactory();
17+
f.Date = startDate.AddDays(index);
18+
f.TemperatureC = Random.Shared.Next(-20, 55);
19+
f.Summary = Summaries[Random.Shared.Next(Summaries.Length)];
20+
return f;
1721
}).ToArray());
1822
}
1923
}

samples/BlazorServerSide/Program.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
using Microsoft.AspNetCore.Components;
22
using Microsoft.AspNetCore.Components.Web;
33
using BlazorServerSide.Data;
4+
5+
// to get the required properties
6+
using System.Reflection;
7+
using System.Runtime.CompilerServices;
8+
49
using Serilog;
510
using DryIoc;
611
using DryIoc.Microsoft.DependencyInjection;
712

813
var builder = WebApplication.CreateBuilder(args);
914

10-
var container = new Container(
11-
// this is causing the #567 error
12-
// Rules.MicrosoftDependencyInjectionRules.With(propertiesAndFields: PropertiesAndFields.Auto)
13-
);
15+
// todo: @wip take into account constructor with SetsRequiredMembers attribute
16+
var requiredProperties = PropertiesAndFields.All(withFields: false,
17+
serviceInfo: (p, _) => p.GetCustomAttribute<RequiredMemberAttribute>() != null ? PropertyOrFieldServiceInfo.Of(p) : null);
18+
19+
var container = new Container(Rules.MicrosoftDependencyInjectionRules.With(propertiesAndFields: requiredProperties));
20+
1421

1522
// Here it goes the integration with the existing DryIoc container
1623
var diFactory = new DryIocServiceProviderFactory(container, RegistrySharing.Share);
@@ -33,6 +40,8 @@
3340
.CreateLogger();
3441
builder.Host.UseSerilog(logger);
3542

43+
44+
builder.Services.AddSingleton<WeatherForecast>(); // will be injected as required property
3645
builder.Services.AddSingleton<WeatherForecastService>();
3746

3847
var app = builder.Build();

0 commit comments

Comments
 (0)