public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"BLOCK:using (var {_session.Usage} = {_store.Usage}.OpenSession())"); Next?.GenerateCode(method, writer); writer.Write($"{_session.Usage}.{nameof(IFakeSession.SaveChanges)}();"); writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var arguments = _arguments.Select(x => x.Usage).Join(", "); var implementationTypeName = ImplementationType.FullName.Replace("+", "."); var declaration = $"var {Variable.Usage} = new {implementationTypeName}({arguments})"; if (IsDisposable) { if (Next is ConstructorFrame && Next.As <ConstructorFrame>().IsDisposable) { writer.Write($"using ({declaration})"); Next?.GenerateCode(method, writer); } else { writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w)); } } else { writer.Write(declaration + ";"); Next?.GenerateCode(method, writer); } }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { if (_sagaIdProperty.PropertyType == typeof(string)) { writer.Write( $"{_sagaIdProperty.PropertyType.NameInCode()} {SagaFramePolicy.SagaIdVariableName} = {_envelope.Usage}.{nameof(Envelope.SagaId)} ?? {_message.Usage}.{_sagaIdProperty.Name};"); writer.Write( $"if (string.{nameof(string.IsNullOrEmpty)}({SagaFramePolicy.SagaIdVariableName})) throw new {typeof(IndeterminateSagaStateIdException).FullName}({_envelope.Usage});"); } else { var typeNameInCode = _sagaIdProperty.PropertyType == typeof(Guid) ? typeof(Guid).FullName : _sagaIdProperty.PropertyType.NameInCode(); writer.Write( $"if (!{typeNameInCode}.TryParse({_envelope.Usage}.{nameof(Envelope.SagaId)}, out {typeNameInCode} sagaId)) sagaId = {_message.Usage}.{_sagaIdProperty.Name};"); if (_sagaIdProperty.PropertyType == typeof(Guid)) { writer.Write( $"if ({SagaId.Usage} == System.Guid.Empty) throw new {typeof(IndeterminateSagaStateIdException).FullName}({_envelope.Usage});"); } else { writer.Write( $"if ({SagaId.Usage} == 0) throw new {typeof(IndeterminateSagaStateIdException).FullName}({_envelope.Usage});"); } } // TODO -- set the SagaId on message context? Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { if (_createsSession) { writer.Write($"BLOCK:using (var {Session.Usage} = {_store.Usage}.{nameof(IDocumentStore.LightweightSession)}())"); } if (_context != null && _isUsingPersistence) { writer.Write($"await {typeof(MessageContextExtensions).FullName}.{nameof(MessageContextExtensions.EnlistInTransaction)}({_context.Usage}, {Session.Usage});"); } foreach (var loaded in _loadedDocs) { loaded.Write(writer, Session); } Next?.GenerateCode(method, writer); foreach (var saved in _saved) { writer.Write($"{Session.Usage}.{nameof(IDocumentSession.Store)}({saved.Usage});"); } writer.Write($"await {Session.Usage}.{nameof(IDocumentSession.SaveChangesAsync)}();"); if (_createsSession) { writer.FinishBlock(); } }
public void WriteMethod(ISourceWriter writer) { var returnValue = AsyncMode == AsyncMode.AsyncTask ? "async Task" : "Task"; if (Overrides) { returnValue = "override " + returnValue; } var arguments = Arguments.Select(x => x.Declaration).Join(", "); writer.Write($"BLOCK:public {returnValue} {MethodName}({arguments})"); Top.GenerateCode(this, writer); if (AsyncMode == AsyncMode.ReturnCompletedTask) { writer.Write("return Task.CompletedTask;"); } writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"BLOCK:using (var {Session.Usage} = {_store.Usage}.{nameof(IDocumentStore.OpenSession)}())"); Next?.GenerateCode(method, writer); writer.Write($"await {Session.Usage}.{nameof(IDocumentSession.SaveChangesAsync)}();"); writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var arguments = _arguments.Select(x => x.Usage).Join(", "); if (ReturnCreated) { writer.Write($"return {_func.Usage}({arguments});"); Next?.GenerateCode(method, writer); return; } var declaration = $"var {Variable.Usage} = {_func.Usage}({arguments})"; switch (Disposal) { case DisposeTracking.None: writer.Write(declaration + ";"); Next?.GenerateCode(method, writer); break; case DisposeTracking.WithUsing: writer.UsingBlock(declaration, w => Next?.GenerateCode(method, w)); break; case DisposeTracking.RegisterWithScope: writer.Write(declaration + ";"); writer.Write($"{_scope.Usage}.{nameof(Scope.TryAddDisposable)}({Variable.Usage});"); Next?.GenerateCode(method, writer); break; } }
private void writeDeclaration(ISourceWriter writer) { var implemented = implements().ToArray(); if (implemented.Any()) writer.Write( $"BLOCK:public class {TypeName} : {implemented.Select(x => x.FullNameInCode()).Join(", ")}"); else writer.Write($"BLOCK:public class {TypeName}"); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"BLOCK:if ({_handler.Usage}.{nameof(StatefulSagaOf<string>.IsCompleted)})"); writer.Write($"{_session.Usage}.{nameof(IDocumentSession.Delete)}({_document.Usage});"); writer.FinishBlock(); writer.Write("BLOCK:else"); writer.Write($"{_session.Usage}.{nameof(IDocumentSession.Store)}({_document.Usage});"); writer.FinishBlock(); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"BLOCK:if ({_handler.Usage}.{nameof(StatefulSagaOf<string>.IsCompleted)})"); writer.Write($"{_persistor.Usage}.{nameof(InMemorySagaPersistor.Delete)}<{_document.VariableType.FullNameInCode()}>({_document.Usage}.Id);"); writer.FinishBlock(); writer.Write("BLOCK:else"); writer.Write($"{_persistor.Usage}.{nameof(InMemorySagaPersistor.Store)}({_document.Usage});"); writer.FinishBlock(); Next?.GenerateCode(method, writer); }
/// <summary> /// Writes the declaration of a new class to the source writer /// </summary> /// <param name="writer"></param> /// <param name="className"></param> /// <param name="inheritsOrImplements"></param> public static void StartClass(this ISourceWriter writer, string className, params Type[] inheritsOrImplements) { if (inheritsOrImplements.Length == 0) { writer.Write($"BLOCK:public class {className}"); } else { writer.Write($"BLOCK:public class {className} : {inheritsOrImplements.Select(x => x.FullNameInCode()).Join(", ")}"); } }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"var {Persistor.Usage} = new {typeof(InMemoryEnvelopeTransaction).FullNameInCode()}();"); writer.Write($"await {_context.Usage}.{nameof(IMessageContext.EnlistInTransaction)}({Persistor.Usage});"); if (_existence == SagaStateExistence.Existing) { writer.Write($"var {Document.Usage} = {_persistor.Usage}.{nameof(InMemorySagaPersistor.Load)}<{Document.VariableType.FullNameInCode()}>({_sagaId.Usage});"); } Next?.GenerateCode(method, writer); }
public override void GenerateCode(IGenerationModel generationModel, ISourceWriter writer) { var callingCode = $"{Method.Name}({_variables.Select(x => x.Usage).Join(", ")})"; var target = Method.IsStatic ? HandlerType.NameInCode() : _target.Usage; var returnValue = ""; var suffix = ""; if (IsAsync) { if (generationModel.AsyncMode == AsyncMode.ReturnFromLastNode) { returnValue = "return "; } else { returnValue = "await "; suffix = ".ConfigureAwait(false)"; } } if (ReturnVariable != null) { returnValue = $"var {ReturnVariable.Usage} = {returnValue}"; } // TODO -- will need to see if it's IDisposable too writer.Write($"{returnValue}{target}.{callingCode}{suffix};"); Next?.GenerateCode(generationModel, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.BlankLine(); writer.WriteComment($"From {nameof(BuildClientFrame)}"); writer.Write($"var {Client.Usage} = {_factory.Usage}.{nameof(IHttpClientFactory.CreateClient)}(\"{_interfaceType.Name}\");"); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.BlankLine(); writer.WriteComment($"From {nameof(FillUrlFrame)}"); writer.Write($"var {Url.Usage} = $\"{Pattern}\";"); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var invokeMethod = invocationCode(); var returnValue = ""; if (IsAsync) { returnValue = method.AsyncMode == AsyncMode.ReturnFromLastNode ? "return " : "await "; } var isDisposable = false; if (shouldAssignVariableToReturnValue(method)) { returnValue = ReturnVariable.VariableType.IsValueTuple() ? $"{ReturnVariable.Usage} = {returnValue}" : $"var {ReturnVariable.Usage} = {returnValue}"; isDisposable = ReturnVariable.VariableType.CanBeCastTo <IDisposable>(); } if (isDisposable && DisposalMode == DisposalMode.UsingBlock) { writer.UsingBlock($"{returnValue}{invokeMethod}", w => Next?.GenerateCode(method, writer)); } else { writer.Write($"{returnValue}{invokeMethod};"); Next?.GenerateCode(method, writer); } }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var declaration = $"{typeof(List<>).Namespace}.List<{ElementType.FullNameInCode()}>"; var elements = Elements.Select(x => x.Usage).Join(", "); if (ReturnCreated) { writer.Write($"return new {declaration}{{{elements}}};"); } else { writer.Write($"var {Variable.Usage} = new {declaration}{{{elements}}};"); } Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.WriteComment("Check if the saga has been completed"); writer.Write($"BLOCK:if ({_handler.Usage}.{nameof(StatefulSagaOf<string>.IsCompleted)})"); writer.WriteComment("Delete the saga state entity"); writer.Write($"{_context.Usage}.{nameof(DbContext.Remove)}({_state.Usage});"); writer.FinishBlock(); // writer.Write("BLOCK:else"); // writer.WriteComment("Persist the saga state entity"); // writer.Write($"{_context.Usage}.{nameof(DbContext.Add)}({_state.Usage});"); // writer.FinishBlock(); writer.BlankLine(); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"case {SpecificEvent.VariableType.FullNameInCode()} {SpecificEvent.Usage}:"); // TODO -- fix this with LamarCodeGeneration writer.As <SourceWriter>().IndentionLevel++; foreach (var frame in _inner) { frame.GenerateCode(method, writer); } writer.Write("break;"); writer.As <SourceWriter>().IndentionLevel--; Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var elements = Elements.Select(x => x.Usage).Join(", "); writer.Write($"var {Variable.Usage} = new {ElementType.FullNameInCode()}[]{{{elements}}};"); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { if (InsideForEach) { writer.Write("BLOCK:foreach (var @event in events)"); } if (IsAsync) { writer.WriteLine( $"{_aggregate.Usage} = await {ApplyMethodCollection.MethodName}(@event, {_aggregate.Usage}, {_session.Usage}, {_cancellation.Usage});"); } else { writer.WriteLine( $"{_aggregate.Usage} = {ApplyMethodCollection.MethodName}(@event, {_aggregate.Usage}, {_session.Usage});"); } if (InsideForEach) { writer.FinishBlock(); } Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write( $"BLOCK:using (var {Session.Usage} = {_store.Usage}.{nameof(IDocumentStore.LightweightSession)}())"); Next?.GenerateCode(method, writer); writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { if (SagaId.VariableType == typeof(string)) { writer.Write($"var {SagaId.Usage} = {_envelope.Usage}.{nameof(Envelope.SagaId)};"); writer.Write($"if (string.{nameof(string.IsNullOrEmpty)}({SagaFramePolicy.SagaIdVariableName})) throw new {typeof(IndeterminateSagaStateIdException).FullName}({_envelope.Usage});"); } else { var typeNameInCode = SagaId.VariableType == typeof(Guid) ? typeof(Guid).FullName : SagaId.VariableType.NameInCode(); writer.Write($"if (!{typeNameInCode}.TryParse({_envelope.Usage}.{nameof(Envelope.SagaId)}, out {typeNameInCode} sagaId)) throw new {typeof(IndeterminateSagaStateIdException).FullName}({_envelope.Usage});"); } Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write( $"BLOCK:using ({_instance.ServiceType.FullNameInCode()} {Connection.Usage} = new {typeof(SqlConnection).FullName}({_settings.Usage}.{nameof(SqlServerSettings.ConnectionString)}))"); Next?.GenerateCode(method, writer); writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { if (CommentText.IsNotEmpty()) { writer.WriteComment(CommentText); } var invokeMethod = InvocationCode(method); if (shouldWriteInUsingBlock(method)) { writer.UsingBlock($"{returnActionCode(method)}{invokeMethod}", w => Next?.GenerateCode(method, writer)); } else { writer.Write($"{returnActionCode(method)}{invokeMethod};"); // This is just to make the generated code a little // easier to read if (CommentText.IsNotEmpty()) { writer.BlankLine(); } Next?.GenerateCode(method, writer); } }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.Write($"BLOCK:foreach (var @event in {EventIteration})"); _inner.GenerateCode(method, writer); writer.FinishBlock(); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { writer.WriteComment("Enroll the DbContext & IMessagingContext in the outgoing Jasper outbox transaction"); writer.Write( $"await {typeof(JasperEnvelopeEntityFrameworkCoreExtensions).FullName}.{nameof(JasperEnvelopeEntityFrameworkCoreExtensions.EnlistInTransaction)}({_context.Usage}, {_dbContext.Usage});"); Next?.GenerateCode(method, writer); }
public static void UsingBlock(this ISourceWriter writer, string declaration, Action <ISourceWriter> inner) { writer.Write($"BLOCK:using ({declaration})"); inner(writer); writer.FinishBlock(); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { var substitutions = _values.Select(CodeFormatter.Write).ToArray(); var code = string.Format(_format, substitutions); // TODO -- It's important to use Write() and not WriteLine() here. writer.Write(code); Next?.GenerateCode(method, writer); }
public override void GenerateCode(GeneratedMethod method, ISourceWriter writer) { Maybe.GenerateCode(method, writer); writer.Write($"BLOCK:if ({Maybe.ReturnVariable.Usage})"); writer.WriteLine($"return null;"); writer.FinishBlock(); }