Skip to content

Commit 7300630

Browse files
committed
fix: design the service envelope to only contain a single service
1 parent 107dfa2 commit 7300630

9 files changed

+30
-45
lines changed

src/Whaally.Domain/Abstractions/IMessageMetadata.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ public interface IMessageMetadata
99
public DateTimeOffset CreatedAt { get; set; }
1010
}
1111

12-
public interface IServiceMetadata : IMessageMetadata;
12+
public interface IServiceMetadata : IMessageMetadata
13+
{
14+
public Type? ServiceType { get; set; }
15+
}
1316

1417
public interface ICommandMetadata : IMessageMetadata
1518
{

src/Whaally.Domain/Abstractions/Service/IServiceEnvelope.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
public interface IServiceEnvelope : IMessageEnvelope
44
{
5-
public new IEnumerable<IService> Messages { get; }
5+
public IService Message { get; }
66
public new IServiceMetadata Metadata { get; }
77

8-
IEnumerable<IMessage> IMessageEnvelope.Messages => Messages;
8+
IEnumerable<IMessage> IMessageEnvelope.Messages => [ Message ];
99
IMessageMetadata IMessageEnvelope.Metadata => Metadata;
1010
}

src/Whaally.Domain/DefaultContextFactory.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ public ISagaContext CreateSagaContext(IEventMetadata metadata)
1515
};
1616

1717
public IServiceHandlerContext CreateServiceHandlerContext(IServiceMetadata metadata)
18-
=> new ServiceHandlerContext(services)
18+
=> new ServiceHandlerContext(services, metadata)
1919
{
2020
Attributes = new ReadOnlyDictionary<string, object>(metadata.Attributes),
21-
ParentContext = metadata.ParentContext
2221
};
2322

2423
public ICommandHandlerContext<TAggregate> CreateCommandHandlerContext<TAggregate>(

src/Whaally.Domain/DefaultEvaluationAgent.cs

+1-12
Original file line numberDiff line numberDiff line change
@@ -34,24 +34,13 @@ public async Task<IResult<ICommandEnvelope[]>> Evaluate(IServiceEnvelope service
3434
if (serviceEnvelope.Messages.Count() != 1)
3535
throw new ArgumentException($"Expected {nameof(serviceEnvelope)} to contain one message");
3636

37-
using var activity = DomainContext.ActivitySource.StartActivity(
38-
ActivityKind.Internal,
39-
name: $"Evaluate {serviceEnvelope.Messages.Single().GetType().Name}",
40-
parentContext: serviceEnvelope.Metadata.ParentContext ?? default,
41-
tags: new Dictionary<string, object?>
42-
{
43-
44-
});
45-
46-
serviceEnvelope.Metadata.ParentContext = activity?.Context;
47-
4837
var serviceContext = _contextFactory.CreateServiceHandlerContext(serviceEnvelope.Metadata);
4938

5039
var result = await _domainContext
5140
.GetServiceHandler(serviceEnvelope.Messages.Single().GetType())
5241
.Handle(
5342
serviceContext,
54-
serviceEnvelope.Messages.Single());
43+
serviceEnvelope.Message);
5544

5645
return new Result<ICommandEnvelope[]>()
5746
.WithValue(serviceContext.Commands.ToArray())

src/Whaally.Domain/Service/ServiceEnvelope.cs

+3-21
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,6 @@
22

33
namespace Whaally.Domain;
44

5-
public record ServiceEnvelope : IServiceEnvelope
6-
{
7-
public ServiceEnvelope(
8-
IServiceMetadata metadata,
9-
IEnumerable<IService> messages)
10-
{
11-
Metadata = metadata;
12-
Messages = messages;
13-
}
14-
15-
public ServiceEnvelope(
16-
IServiceMetadata metadata,
17-
params IService[] messages)
18-
{
19-
Metadata = metadata;
20-
Messages = messages;
21-
}
22-
23-
public IServiceMetadata Metadata { get; init; }
24-
public IEnumerable<IService> Messages { get; init; }
25-
}
5+
public record ServiceEnvelope(
6+
IServiceMetadata Metadata,
7+
IService Message) : IServiceEnvelope;

src/Whaally.Domain/Service/ServiceHandlerContext.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@ public class ServiceHandlerContext : IServiceHandlerContext
1212
private readonly IEvaluationAgent _evaluationAgent;
1313
private readonly DomainContext _domainContext;
1414

15+
private readonly Activity? _activity;
1516
// ToDo: Add an activity here to track service evaluation
1617

17-
public ServiceHandlerContext(IServiceProvider services)
18+
public ServiceHandlerContext(IServiceProvider services, IServiceMetadata metadata)
1819
{
1920
_services = services;
2021
_evaluationAgent = services.GetRequiredService<IEvaluationAgent>();
2122
_domainContext = services.GetRequiredService<DomainContext>();
23+
24+
_activity = DomainContext.ActivitySource.StartActivity(
25+
ActivityKind.Internal,
26+
name: $"Evaluate {metadata.ServiceType?.Name}",
27+
parentContext: metadata.ParentContext ?? default,
28+
tags: new Dictionary<string, object?>
29+
{
30+
31+
});
32+
33+
ParentContext = _activity?.Context;
2234
}
2335

2436
public ActivityContext? ParentContext { get; init; }

src/Whaally.Domain/Service/ServiceMetadata.cs

+2
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ public record ServiceMetadata : IServiceMetadata
1111
public ActivityContext? ParentContext { get; set; }
1212

1313
public DateTimeOffset CreatedAt { get; set; }
14+
15+
public Type? ServiceType { get; set; }
1416
}

tests/Whaally.Domain.Tests/RecursiveServiceInvocationTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public async Task RecursiveServiceCanBeEvaluated()
6060

6161
var serviceHandler = _services.GetRequiredService<IServiceHandler<RecursiveService>>();
6262
var serviceHandlerContext =
63-
new ServiceHandlerContext(_services);
63+
new ServiceHandlerContext(_services, new ServiceMetadata());
6464

6565
var result = await serviceHandler.Handle(serviceHandlerContext, service);
6666

tests/Whaally.Domain.Tests/ServiceTests.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using Microsoft.Extensions.DependencyInjection;
2-
using Whaally.Domain.Abstractions;
3-
using Whaally.Domain.Tests.Domain;
1+
using Whaally.Domain.Tests.Domain;
42

53
namespace Whaally.Domain.Tests;
64

@@ -11,7 +9,7 @@ public class ServiceTests
119
[Fact]
1210
public async Task ServiceCanBeEvaluated()
1311
{
14-
var context = new ServiceHandlerContext(_services);
12+
var context = new ServiceHandlerContext(_services, new ServiceMetadata());
1513
var service = new TestService
1614
{
1715
Id = Guid.NewGuid().ToString()
@@ -27,7 +25,7 @@ public async Task ServiceCanBeEvaluated()
2725
[Fact]
2826
public async Task ServiceCanInvokeOtherServices()
2927
{
30-
var context = new ServiceHandlerContext(_services);
28+
var context = new ServiceHandlerContext(_services, new ServiceMetadata());
3129
var service = new TestParentService
3230
{
3331
Id1 = Guid.NewGuid().ToString(),

0 commit comments

Comments
 (0)