Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#2900 Prevent code generation bugs when multiple aggregates are code … #2901

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,36 +78,61 @@ protected override bool tryAttachTypes(Assembly assembly, StoreOptions options)
return _inlineType != null && _liveType != null;
}

/// <summary>
/// Prevent code generation bugs when multiple aggregates are code generated in parallel
/// Happens more often on dynamic code generation
/// </summary>
private static readonly object LockObjAssembleTypes = new object();

protected override void assembleTypes(GeneratedAssembly assembly, StoreOptions options)
{
lock (LockObjAssembleTypes)
{
ReferenceAssembliesAndTypes(assembly);
AddUsingNamespaces(assembly);
CheckAndSetAsyncFlag();
ValidateAndSetAggregateMapping(options);
BuildAggregationTypes(assembly);
}
}

private void ReferenceAssembliesAndTypes(GeneratedAssembly assembly)
{
assembly.Rules.ReferenceTypes(GetType());
assembly.ReferenceAssembly(GetType().Assembly);
assembly.ReferenceAssembly(typeof(T).Assembly);


assembly.Rules.ReferenceTypes(_applyMethods.ReferencedTypes().ToArray());
assembly.Rules.ReferenceTypes(_createMethods.ReferencedTypes().ToArray());
assembly.Rules.ReferenceTypes(_shouldDeleteMethods.ReferencedTypes().ToArray());

// Walk the assembly dependencies for the projection and aggregate types,
// and this will catch generic type argument dependencies as well. For GH-2061
assembly.Rules.ReferenceTypes(GetType(), typeof(T));
}

private static void AddUsingNamespaces(GeneratedAssembly assembly)
{
assembly.UsingNamespaces.Add("System");
assembly.UsingNamespaces.Add("System.Linq");
}

private void CheckAndSetAsyncFlag()
{
_isAsync = _createMethods.IsAsync || _applyMethods.IsAsync;
}

private void ValidateAndSetAggregateMapping(StoreOptions options)
{
_aggregateMapping = options.Storage.FindMapping(typeof(T));


if (_aggregateMapping.IdMember == null)
{
throw new InvalidDocumentException(
$"No identity property or field can be determined for the aggregate '{typeof(T).FullNameInCode()}', but one is required to be used as an aggregate in projections");
}
}


private void BuildAggregationTypes(GeneratedAssembly assembly)
{
buildLiveAggregationType(assembly);
buildInlineAggregationType(assembly);
}
Expand Down
Loading