Skip to content

Commit a5c9cfd

Browse files
committed
refactor: Improve SQS client registration and enhance message dispatcher invocation
1 parent dbcb7f1 commit a5c9cfd

File tree

2 files changed

+24
-23
lines changed

2 files changed

+24
-23
lines changed

Modules/Intent.Modules.Aws.Sqs/Templates/SqsConfiguration/SqsConfigurationTemplatePartial.cs

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,29 +51,30 @@ public SqsConfigurationTemplate(IOutputTarget outputTarget, object? model = null
5151

5252
// Register SQS client (with LocalStack support)
5353
method.AddStatement("""var serviceUrl = configuration["AWS:ServiceURL"];""");
54-
var ifStmt = method.AddIfStatement("!string.IsNullOrEmpty(serviceUrl)");
55-
ifStmt.AddStatement("// LocalStack or custom endpoint");
56-
ifStmt.AddInvocationStatement("services.AddSingleton<IAmazonSQS>", inv => inv
57-
.AddArgument(new CSharpLambdaBlock("sp"), arg =>
58-
{
59-
arg.AddStatement("""
60-
var sqsConfig = new AmazonSQSConfig
61-
{
62-
ServiceURL = serviceUrl,
63-
AuthenticationRegion = configuration["AWS:Region"] ?? "us-east-1"
64-
};
65-
""");
66-
arg.AddStatement("""
67-
return new AmazonSQSClient(
68-
new BasicAWSCredentials("test", "test"),
69-
sqsConfig
70-
);
71-
""");
72-
}));
54+
method.AddIfStatement("!string.IsNullOrEmpty(serviceUrl)", ifStmt =>
55+
{
56+
ifStmt.AddStatement("// LocalStack or custom endpoint");
57+
ifStmt.AddInvocationStatement("services.AddSingleton<IAmazonSQS>", inv => inv
58+
.AddArgument(new CSharpLambdaBlock("sp"), arg =>
59+
{
60+
arg.AddStatement(new CSharpAssignmentStatement(
61+
new CSharpVariableDeclaration("sqsConfig"),
62+
new CSharpObjectInitializerBlock("new AmazonSQSConfig")
63+
.AddInitStatement("ServiceURL", "serviceUrl")
64+
.AddInitStatement("AuthenticationRegion", @"configuration[""AWS:Region""] ?? ""us-east-1"""))
65+
.WithSemicolon());
66+
arg.AddReturn(new CSharpInvocationStatement("new AmazonSQSClient")
67+
.AddArgument(@"new BasicAWSCredentials(""test"", ""test"")")
68+
.AddArgument(@"sqsConfig")
69+
.WithoutSemicolon());
70+
}));
71+
});
7372

74-
var elseStmt = ifStmt.AddElseStatement();
75-
elseStmt.AddStatement("// Production AWS");
76-
elseStmt.AddStatement("services.AddAWSService<IAmazonSQS>();");
73+
method.AddElseStatement(elseStmt =>
74+
{
75+
elseStmt.AddStatement("// Production AWS");
76+
elseStmt.AddStatement("services.AddAWSService<IAmazonSQS>();");
77+
});
7778

7879
// Register event bus
7980
method.AddStatement($"services.AddScoped<{this.GetEventBusInterfaceName()}, {this.GetTypeName(SqsEventBusTemplate.TemplateId)}>();", stmt => stmt.SeparatedFromPrevious());

Modules/Intent.Modules.Aws.Sqs/Templates/SqsSubscriptionOptions/SqsSubscriptionOptionsTemplate.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void Add<TMessage, THandler>()
3535
{
3636
_entries.Add(new {{SubscriptionEntry}}(
3737
typeof(TMessage),
38-
SqsMessageDispatcher.InvokeDispatchHandler<TMessage, THandler>));
38+
{{this.GetSqsMessageDispatcherName()}}.InvokeDispatchHandler<TMessage, THandler>));
3939
}
4040
}
4141

0 commit comments

Comments
 (0)