SourceKit.Generators.Builder
1.3.77
Prefix Reserved
dotnet add package SourceKit.Generators.Builder --version 1.3.77
NuGet\Install-Package SourceKit.Generators.Builder -Version 1.3.77
<PackageReference Include="SourceKit.Generators.Builder" Version="1.3.77" />
<PackageVersion Include="SourceKit.Generators.Builder" Version="1.3.77" />
<PackageReference Include="SourceKit.Generators.Builder" />
paket add SourceKit.Generators.Builder --version 1.3.77
#r "nuget: SourceKit.Generators.Builder, 1.3.77"
#:package SourceKit.Generators.Builder@1.3.77
#addin nuget:?package=SourceKit.Generators.Builder&version=1.3.77
#tool nuget:?package=SourceKit.Generators.Builder&version=1.3.77
SourceKit.Generators.Builder
A source generator that generates builders for types.
- Mark a type with
[GenerateBuilder]attribute - Generator supports types with single or primary constructor
Builderclass will be generated inside annotated type- Builder will have methods that set/add values
With{PropName}for non-collection propertiesWith{PropName}andWith{PropName}sfor collection properties- Builder can be used as a standalone object, or it can be used within static Build method
- Static Build method accepts delegate with configures builder and calls build immediately, returning the built object
- It is primarily used for scoping the builder calls which allows for analysis of methods called on builder (for required values)
[GenerateBuilder]
public partial record SomeQuery(IReadOnlyCollection<Guid> Ids, int Count = 10);
...
var query = SomeQuery.Build(x => x.WithCount(2).WithId(Guid.NewGuid());
Default values for record properties are supported
Configure builder properties
Use [BuilderPropery] and BuilderPropertyOptions to customize generated code.
Required properties
Use BuilderPropertyOptions.Required to configure required properties. This annotation will be used for analysis of
calls to static Build method, it will ensure that the method that initializes this parameter is called within the
delegate.
[Generate builder]
public partial record SomeQuery(long[] Ids, [BuilderProperty(BuilderPropertyOptions.Required)] int PageSize);
The following code will produce an error.
var query = SomeQuery.Build(x => x.WithId(1));
Constructor parameter
Use BuilderPropertyOptions.ConstructorParameter to configure parameter to be builder's constructor parameter. It also
would be a
parameter of a static Build method. Such option is primarily used when you need a guarantee that a certain value is
specified when working with builder at any point (this option is commonly combined with Exposed option).
[Generate builder]
public partial record SomeQuery(long[] Ids, [BuilderProperty(BuilderPropertyOptions.ConstructorParameter)] int PageSize);
var query = SomeQuery.Build(1, builder => builder.WithId(2));
Exposed parameter
Use BuilderPropertyOptions.Exposed to configure parameter to be exposed as a property.
- This option is commonly combined with
ConstructorParameteroption, ex:- you have a builder for some entity that is used in an enrichment pipeline as a data buffer
- you will need an id of the entity at each enrichment step
- so it would be useful to have it exposed as a property of the builder
- and it is useful that it is guaranteed to be specified on builder creation
- For non-collection parameters annotated with
ExposedandConstructorParameteroptions the non-nullable property will be generated (if the underlying parameter's type is non-nullable) - For non-collection parameters annotated with
Exposedoption only – the property will be nullable. - For collection parameter, annotated with
Exposedoption the IEnumerable type is used, when collection parameter yet to be called, then the empty enumerable will be returned.
[Generate builder]
public partial record SomeQuery(
long[] Ids,
[BuilderProperty(BuilderPropertyOptions.ConstructorParameter | BuilderPropertyOptions.Exposed)]
int PageSize);
var builder = new SomeQuery.Builder(pageSize: 1);
Console.WriteLine(builder.PageSize); // 1
Allow duplicates
Use BuilderPropertyOptions.AllowsDuplicates to disable deduplication logic for collections with equatable elements.
- By default, builder uses
HashSet<>to store intermediate values of collection parameters with equatable elements - There may be reasons for you not wanting to do that:
- performance overhead
- logic requirements
- When parameter is annotated with
AllowDuplicatesoption, theList<>would be used to store intermediate values
[Generate builder]
public partial record SomeQuery([BuilderProperty(BuilderPropertyOptions.AllowsDuplicates)] long[] Ids);
var query = SomeQuery.Build(builder => builder.WithId(1).WithId(1));
Console.WriteLine(query.Ids.Length); // 2
Disable collection copying
Use BuilderPropertyOptions.NotCopied to avoid copying intermediate collections when consturcting models.
- By default, when passing values of collection properties the spread operator is used (
[..values]) - For better performance this behavior may be altered and copying could be disabled
- Note that generator does not validate type compatibility on its own
- If you have an array parameter and annotate it with
NotCopiedoption – the compiler error will occur - It happens as the type of intermediate collection is not compatible with your parameter type
- You should manually match the type of intermediate collection, or use collection interfaces
- If you have an array parameter and annotate it with
- Consider the builder's usecase and whether the data will be or won't be copied down the line
- For example: Npgsql could be a valid usecase for this option
- If you want to allocate as little memory as possible you may want to enable this option
- But! Npgsql only works with arrays and types that implement
IList<>the conditions thatHashSet<>type fails to meet - So if your usecase has some collection's type limitations, consider combining this option with
AllowDuplicatesas theList<>is wider acceptable type.
[Generate builder]
public partial record SomeQuery([BuilderProperty(BuilderPropertyOptions.NotCopied)] IReadOnlyCollection<long> Ids);
var builder = new SomeQuery.Builder().WithId(1);
var model = builder.Build();
builder.WithId(2);
Console.WriteLine(model.Ids.Count); // 2
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Humanizer.Core (>= 3.0.10)
- Humanizer.Core.uk (>= 3.0.10)
- Microsoft.Bcl.AsyncInterfaces (>= 10.0.10)
- Microsoft.CodeAnalysis.Common (>= 5.6.0)
- Microsoft.CodeAnalysis.CSharp (>= 5.6.0)
- Microsoft.CodeAnalysis.Workspaces.Common (>= 5.6.0)
- Microsoft.Extensions.DependencyInjection (>= 10.0.10)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 10.0.10)
- SourceKit (>= 1.1.77)
- SourceKit.Generators.Builder.Annotations (>= 1.1.77)
NuGet packages (9)
Showing the top 5 NuGet packages that depend on SourceKit.Generators.Builder:
| Package | Downloads |
|---|---|
|
Itmo.Dev.Platform.Kafka
Package Description |
|
|
Itmo.Dev.Platform.BackgroundTasks
Package Description |
|
|
Itmo.Dev.Platform.MessagePersistence
Package Description |
|
|
Itmo.Dev.Platform.MessagePersistence.Postgres
Package Description |
|
|
Itmo.Dev.Platform.BackgroundTasks.Postgres
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.3.77 | 49 | 8/3/2026 |
| 1.2.76 | 49 | 7/31/2026 |
| 1.2.75 | 66 | 7/30/2026 |
| 1.2.74-alpha | 42 | 7/29/2026 |
| 1.2.73-alpha | 43 | 7/29/2026 |
| 1.2.72 | 49 | 7/29/2026 |
| 1.2.71 | 59 | 7/29/2026 |
| 1.2.69 | 40 | 7/29/2026 |
| 1.2.68 | 42 | 7/29/2026 |
| 1.2.67 | 43 | 7/29/2026 |
| 1.2.66 | 48 | 7/29/2026 |
| 1.2.65 | 51 | 7/29/2026 |
| 1.2.64 | 51 | 7/29/2026 |
| 1.2.63 | 115 | 7/16/2026 |
| 1.2.62 | 130 | 6/30/2026 |
| 1.2.61 | 115 | 6/30/2026 |
| 1.2.60 | 158 | 6/13/2026 |
| 1.2.59 | 191 | 5/4/2026 |
| 1.2.58 | 1,487 | 4/2/2026 |
| 1.2.57 | 131 | 4/2/2026 |
Added BuilderPropertyOptions