public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"case {SpecificEvent.VariableType.FullNameInCode()} {SpecificEvent.Usage}:"); writer.IndentionLevel++; if (AlwaysDeletes) { writer.Write("return null;"); } CreationFrame?.GenerateCode(method, writer); if (Apply != null) { if (CreationFrame == null) { var defaultConstructor = AggregateType.GetConstructor( BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, Type.EmptyTypes, null); if (defaultConstructor?.IsPublic == true) { writer.Write($"{Aggregate.Usage} ??= new {AggregateType.FullNameInCode()}();"); } else if (defaultConstructor?.IsPublic == false) { writer.Write($"{Aggregate.Usage} ??= AggregateBuilder();"); } else { var errorMessage = $"Projection for {AggregateType.FullName} should either have the Create Method or Constructor for event of type {SpecificEvent.VariableType.FullNameInCode()}, or {AggregateType.FullName} should have a Default Constructor."; writer.Write( $"if({Aggregate.Usage} == default) throw new ArgumentException(\"{errorMessage}\");"); } } Apply.GenerateCode(method, writer); } if (Deletion != null) { writer.Write($"if ({Aggregate.Usage} == null) return null;"); Deletion.GenerateCode(method, writer); } writer.Write($"return {Aggregate.Usage};"); writer.IndentionLevel--; Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"case {SpecificEvent.VariableType.FullNameInCode()} {SpecificEvent.Usage}:"); writer.IndentionLevel++; if (AlwaysDeletes) { writer.Write("return null;"); } CreationFrame?.GenerateCode(method, writer); if (Apply != null) { if (AggregateType.GetConstructors().Any(x => !x.GetParameters().Any())) { writer.Write($"{Aggregate.Usage} ??= new {AggregateType.FullNameInCode()}();"); } Apply.GenerateCode(method, writer); } if (Deletion != null) { writer.Write($"if ({Aggregate.Usage} == null) return null;"); Deletion.GenerateCode(method, writer); } writer.Write($"return {Aggregate.Usage};"); writer.IndentionLevel--; Next?.GenerateCode(method, writer); }