@@ -9,16 +9,17 @@ public class DefaultEvaluationAgent : IEvaluationAgent
99{
1010 private readonly IServiceProvider _services ;
1111 private readonly DomainContext _domainContext ;
12- private readonly IAggregateHandlerFactory _handlerFactory ;
1312 private readonly IContextFactory _contextFactory ;
13+ private readonly IAggregateHandlerFactory _handlerFactory ;
14+
1415 private readonly Activity ? _activity ;
1516
1617 public DefaultEvaluationAgent ( IServiceProvider services )
1718 {
1819 _services = services ;
1920 _domainContext = _services . GetRequiredService < DomainContext > ( ) ;
20- _handlerFactory = _services . GetRequiredService < IAggregateHandlerFactory > ( ) ;
2121 _contextFactory = _services . GetRequiredService < IContextFactory > ( ) ;
22+ _handlerFactory = _services . GetRequiredService < IAggregateHandlerFactory > ( ) ;
2223
2324 _activity = DomainContext . ActivitySource . StartActivity (
2425 ActivityKind . Internal ,
@@ -36,7 +37,9 @@ public async Task<IResult<ICommandEnvelope[]>> Evaluate(IServiceEnvelope service
3637 {
3738 if ( serviceEnvelope . Messages . Count ( ) != 1 )
3839 throw new ArgumentException ( $ "Expected { nameof ( serviceEnvelope ) } to contain one message") ;
39-
40+
41+ serviceEnvelope . Metadata . ParentContext = _activity ? . Context ;
42+
4043 var serviceContext = _contextFactory . CreateServiceHandlerContext ( serviceEnvelope . Metadata ) ;
4144
4245 var result = await _domainContext
@@ -45,13 +48,9 @@ public async Task<IResult<ICommandEnvelope[]>> Evaluate(IServiceEnvelope service
4548 serviceContext ,
4649 serviceEnvelope . Messages . Single ( ) ) ;
4750
48- if ( result . IsFailed )
49- return Result . Fail < ICommandEnvelope [ ] > ( result . Errors ) ;
50-
51- return Result
52- . Ok ( Array . Empty < ICommandEnvelope > ( ) )
53- . WithReasons ( result . Reasons )
54- . WithValue ( serviceContext . Commands . ToArray ( ) ) ;
51+ return new Result < ICommandEnvelope [ ] > ( )
52+ . WithValue ( serviceContext . Commands . ToArray ( ) )
53+ . WithReasons ( result . Reasons ) ;
5554 }
5655
5756 /// <summary>
@@ -68,23 +67,20 @@ public async Task<IResult<IEventEnvelope[]>> Evaluate(params ICommandEnvelope[]
6867 {
6968 if ( ! envelope . Messages . Any ( ) ) continue ;
7069
70+ envelope . Metadata . ParentContext = _activity ? . Context ;
71+
7172 var handler = _handlerFactory . Instantiate (
7273 GetCommonAggregateType ( envelope ) ,
7374 envelope . Metadata . AggregateId ) ;
7475
7576 results . Add ( await handler . Evaluate ( envelope ) ) ;
7677 }
77-
78- var result = Result
79- . Ok ( Array . Empty < IEventEnvelope > ( ) )
80- . WithReasons ( results . SelectMany ( q => q . Reasons ) ) ;
81-
82- if ( result . IsSuccess )
83- result . WithValue ( results
78+
79+ return new Result < IEventEnvelope [ ] > ( )
80+ . WithValue ( results
8481 . Select ( q => q . Value )
85- . ToArray ( ) ) ;
86-
87- return result ;
82+ . ToArray ( ) )
83+ . WithReasons ( results . SelectMany ( q => q . Reasons ) ) ;
8884 }
8985
9086 /// <summary>
@@ -100,6 +96,8 @@ public async Task<IResultBase> Apply(params IEventEnvelope[] eventEnvelopes)
10096 foreach ( var envelope in eventEnvelopes )
10197 {
10298 if ( ! envelope . Messages . Any ( ) ) continue ;
99+
100+ envelope . Metadata . ParentContext = _activity ? . Context ;
103101
104102 var handler = _handlerFactory . Instantiate (
105103 GetCommonAggregateType ( envelope ) ,
@@ -108,10 +106,8 @@ public async Task<IResultBase> Apply(params IEventEnvelope[] eventEnvelopes)
108106 results . Add ( await handler . Apply ( envelope ) ) ;
109107 }
110108
111- var result = new Result ( )
109+ return new Result ( )
112110 . WithReasons ( results . SelectMany ( q => q . Reasons ) ) ;
113-
114- return result ;
115111 }
116112
117113 /// <summary>
@@ -121,6 +117,8 @@ public async Task<IResultBase> Apply(params IEventEnvelope[] eventEnvelopes)
121117 /// <returns></returns>
122118 public Task < IResultBase > Continue ( IEventEnvelope eventEnvelope )
123119 {
120+ eventEnvelope . Metadata . ParentContext = _activity ? . Context ;
121+
124122 foreach ( var @event in eventEnvelope . Messages )
125123 {
126124 foreach ( var saga in _domainContext . GetSaga ( @event . GetType ( ) ) )
@@ -142,6 +140,8 @@ public async Task<IResultBase> Invoke(ISaga saga, IEventEnvelope eventEnvelope)
142140 {
143141 if ( eventEnvelope . Messages . Count ( ) != 1 )
144142 throw new ArgumentException ( $ "Expected { nameof ( eventEnvelope ) } to contain one message") ;
143+
144+ eventEnvelope . Metadata . ParentContext = _activity ? . Context ;
145145
146146 return await saga . Evaluate (
147147 _contextFactory . CreateSagaContext ( eventEnvelope . Metadata ) ,
0 commit comments