Skip to content

Commit 4f95d4a

Browse files
committed
- Add SampleLibrary project that uses source generator via project reference. This verifies that it compiles correctly when used for real.
- Change namespace of generated file so it does not clash with existing namespaces. - Updated readme.
1 parent 37f320a commit 4f95d4a

File tree

8 files changed

+60
-5
lines changed

8 files changed

+60
-5
lines changed

OptionsBindingsGenerator.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Solution>
22
<Project Path="OptionsBindingsGenerator.UnitTests/OptionsBindingsGenerator.UnitTests.csproj" />
33
<Project Path="OptionsBindingsGenerator/OptionsBindingsGenerator.csproj" />
4+
<Project Path="SampleLibrary/SampleLibrary.csproj" Id="208c9f38-b448-41b6-afc0-4e657c208f31" />
45
<Project Path="TestLibrary/TestLibrary.csproj" />
56
</Solution>

OptionsBindingsGenerator/OutputGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static (string source, string className) GenerateOutput(ImmutableArray<IN
2222
using Microsoft.Extensions.DependencyInjection;
2323
using Microsoft.Extensions.Options;
2424
25-
namespace CodingFlow.Generated.{namespaceId};
25+
namespace CodingFlow.Generated.OptionsBindingsGenerator.Generated{namespaceId};
2626
2727
internal static class OptionsBindings
2828
{{

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ internal record NatsServiceOptions
3131
}
3232
```
3333

34-
An extension method called `AddOptionsBindings` containing all the required DI code will be generated in the namespace `CodingFlow.Generated.{assemblyName}`, where `{assemblyName}` will be the name of the assembly of one of the option types, which will most likely be the name of the project the option type is in.
35-
In this example, the fully qualified name will be `CodingFlow.Generated.TestLibrary.AddOptionsBindings`.
34+
An extension method called `AddOptionsBindings` containing all the required DI code will be generated in the namespace `CodingFlow.Generated.OptionsBindingsGenerator.Generated{assemblyName}`, where `{assemblyName}` will be the name of the assembly of one of the option types, which will most likely be the name of the project the option type is in.
35+
In this example, the fully qualified name will be `CodingFlow.Generated.OptionsBindingsGenerator.GeneratedTestLibrary.AddOptionsBindings`.
3636

3737
A nonexistent options validator class will also be registered per options type with the attribute. In this example it's called `ValidateNatsServiceOptions`. Create the missing classes using the official [source generator attribute](https://learn.microsoft.com/en-us/dotnet/core/extensions/options-validation-generator) for generating performant options validation based on the data annotations in the options type:
3838

@@ -50,7 +50,7 @@ internal partial class ValidateNatsServiceOptions : IValidateOptions<NatsService
5050
Finally, call the generated extension method in your application DI code:
5151

5252
```csharp
53-
using CodingFlow.Generated.TestLibrary;
53+
using CodingFlow.Generated.OptionsBindingsGenerator.GeneratedTestLibrary;
5454

5555
var builder = Host.CreateApplicationBuilder(args);
5656

SampleLibrary/Main.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using CodingFlow.Generated.OptionsBindingsGenerator.GeneratedSampleLibrary;
2+
using Microsoft.Extensions.Hosting;
3+
4+
namespace SampleLibrary;
5+
6+
public class Main
7+
{
8+
public void Run()
9+
{
10+
var builder = Host.CreateApplicationBuilder();
11+
12+
builder.Services.AddOptionsBindings(builder.Configuration);
13+
14+
using IHost host = builder.Build();
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.ComponentModel.DataAnnotations;
2+
using CodingFlow.OptionsBindingsGenerator;
3+
4+
namespace SampleLibrary;
5+
6+
[OptionsBindings(false, "Services:Nats")]
7+
internal record NatsServiceOptions
8+
{
9+
[Required]
10+
public required string ServiceHost { get; init; }
11+
12+
public required string Port { get; init; }
13+
}

SampleLibrary/SampleLibrary.csproj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.1" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<ProjectReference Include="..\OptionsBindingsGenerator\OptionsBindingsGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Microsoft.Extensions.Options;
2+
3+
namespace SampleLibrary;
4+
5+
[OptionsValidator]
6+
internal partial class ValidateNatsServiceOptions : IValidateOptions<NatsServiceOptions>
7+
{
8+
}

TestLibrary/OptionsBindings.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using Microsoft.Extensions.DependencyInjection;
66
using Microsoft.Extensions.Options;
77

8-
namespace CodingFlow.Generated.TestProject;
8+
namespace CodingFlow.Generated.OptionsBindingsGenerator.GeneratedTestProject;
99

1010
internal static class OptionsBindings
1111
{

0 commit comments

Comments
 (0)