-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDomainModelConfiguratorGenerator.DomainEvents.cs
48 lines (39 loc) · 1.98 KB
/
DomainModelConfiguratorGenerator.DomainEvents.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System.Collections.Immutable;
using Microsoft.CodeAnalysis;
namespace Architect.DomainModeling.Generator.Configurators;
public partial class DomainModelConfiguratorGenerator
{
internal static void GenerateSourceForDomainEvents(SourceProductionContext context, (ImmutableArray<DomainEventGenerator.Generatable> Generatables, (bool HasConfigureConventions, string AssemblyName) Metadata) input)
{
context.CancellationToken.ThrowIfCancellationRequested();
// Generate the method only if we have any generatables, or if we are an assembly in which ConfigureConventions() is called
if (!input.Generatables.Any() && !input.Metadata.HasConfigureConventions)
return;
var targetNamespace = input.Metadata.AssemblyName;
var configurationText = String.Join($"{Environment.NewLine}\t\t\t", input.Generatables.Select(generatable =>
$"configurator.ConfigureDomainEvent<{generatable.ContainingNamespace}.{generatable.TypeName}>({Environment.NewLine} new {Constants.DomainModelingNamespace}.Configuration.IDomainEventConfigurator.Args() {{ HasDefaultConstructor = {(generatable.ExistingComponents.HasFlag(DomainEventGenerator.DomainEventTypeComponents.DefaultConstructor) ? "true" : "false")} }});"));
var source = $@"
using {Constants.DomainModelingNamespace};
#nullable enable
namespace {targetNamespace}
{{
public static class DomainEventDomainModelConfigurator
{{
/// <summary>
/// <para>
/// Invokes a callback on the given <paramref name=""configurator""/> for each marked domain event type in the current assembly.
/// </para>
/// <para>
/// For example, this can be used to have Entity Framework configure a convention for every matching type in the domain model, in a trim-safe way.
/// </para>
/// </summary>
public static void ConfigureDomainEvents({Constants.DomainModelingNamespace}.Configuration.IDomainEventConfigurator configurator)
{{
{configurationText}
}}
}}
}}
";
AddSource(context, source, "DomainEventDomainModelConfigurator", targetNamespace);
}
}