Skip to content

Commit 85c9dfb

Browse files
Merge pull request #865 from TransactionProcessing/task/#851_contract_domain_service_performance_changes
Refactor contract methods to remove ApplyUpdates
2 parents 8dedde3 + 8f82433 commit 85c9dfb

1 file changed

Lines changed: 86 additions & 65 deletions

File tree

TransactionProcessor.BusinessLogic/Services/ContractDomainService.cs

Lines changed: 86 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -51,110 +51,124 @@ public ContractDomainService(Func<IAggregateService> aggregateService, IEventSto
5151
#endregion
5252

5353
#region Methods
54-
55-
private async Task<Result> ApplyUpdates(Func<(EstateAggregate estateAggregate, ContractAggregate contractAggregate), Task<Result>> action, Guid estateId, Guid contractId, CancellationToken cancellationToken, Boolean isNotFoundError = true)
56-
{
57-
try
58-
{
59-
Result<EstateAggregate> getResult = await this.AggregateService.Get<EstateAggregate>(estateId, cancellationToken);
60-
if (getResult.IsFailed)
61-
return ResultHelpers.CreateFailure(getResult);
62-
EstateAggregate estateAggregate = getResult.Data;
63-
if (estateAggregate.IsCreated == false)
64-
return Result.Failure("Estate is not created");
65-
66-
Result<ContractAggregate> getContractResult = await this.AggregateService.GetLatest<ContractAggregate>(contractId, cancellationToken);
67-
Result<ContractAggregate> contractAggregateResult =
68-
DomainServiceHelper.HandleGetAggregateResult(getContractResult, contractId, isNotFoundError);
69-
if (contractAggregateResult.IsFailed)
70-
return ResultHelpers.CreateFailure(contractAggregateResult);
71-
72-
ContractAggregate contractAggregate = contractAggregateResult.Data;
73-
Result result = await action((estateAggregate, contractAggregate));
74-
if (result.IsFailed)
75-
return ResultHelpers.CreateFailure(result);
76-
77-
Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken);
78-
if (saveResult.IsFailed)
79-
return ResultHelpers.CreateFailure(saveResult);
80-
81-
return Result.Success();
82-
}
83-
catch (Exception ex)
84-
{
85-
return Result.Failure(ex.GetExceptionMessages());
86-
}
87-
}
88-
54+
8955
public async Task<Result> AddProductToContract(ContractCommands.AddProductToContractCommand command, CancellationToken cancellationToken)
9056
{
9157
Models.Contract.ProductType productType = (Models.Contract.ProductType)command.RequestDTO.ProductType;
9258

93-
Result result = await this.ApplyUpdates(async ((EstateAggregate estateAggregate, ContractAggregate contractAggregate) aggregates) => {
94-
if (aggregates.estateAggregate.IsCreated == false) {
95-
return Result.Forbidden($"Estate with Id {command.EstateId} not created");
96-
}
59+
try
60+
{
61+
Result<ContractAggregate> contractResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<ContractAggregate>(command.ContractId, ct), command.ContractId, cancellationToken);
62+
if (contractResult.IsFailed)
63+
return ResultHelpers.CreateFailure(contractResult);
9764

98-
if (aggregates.contractAggregate.IsCreated == false) {
65+
ContractAggregate contractAggregate = contractResult.Data;
66+
67+
if (contractAggregate.IsCreated == false) {
9968
return Result.Forbidden($"Contract Id [{command.ContractId}] must be created to add products");
10069
}
10170

10271
if (command.RequestDTO.Value.HasValue) {
103-
aggregates.contractAggregate.AddFixedValueProduct(command.ProductId, command.RequestDTO.ProductName,
72+
contractAggregate.AddFixedValueProduct(command.ProductId, command.RequestDTO.ProductName,
10473
command.RequestDTO.DisplayText, command.RequestDTO.Value.Value, productType);
10574
}
10675
else {
107-
aggregates.contractAggregate.AddVariableValueProduct(command.ProductId, command.RequestDTO.ProductName,
76+
contractAggregate.AddVariableValueProduct(command.ProductId, command.RequestDTO.ProductName,
10877
command.RequestDTO.DisplayText, productType);
10978
}
11079

111-
return Result.Success();
112-
}, command.EstateId, command.ContractId, cancellationToken);
113-
return result;
80+
Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken);
81+
if (saveResult.IsFailed)
82+
return ResultHelpers.CreateFailure(saveResult);
83+
84+
return saveResult;
85+
}
86+
catch (Exception ex)
87+
{
88+
return Result.Failure(ex.GetExceptionMessages());
89+
}
11490
}
11591

11692
public async Task<Result> AddTransactionFeeForProductToContract(ContractCommands.AddTransactionFeeForProductToContractCommand command, CancellationToken cancellationToken)
11793
{
11894
Models.Contract.CalculationType calculationType = (Models.Contract.CalculationType)command.RequestDTO.CalculationType;
11995
Models.Contract.FeeType feeType = (Models.Contract.FeeType)command.RequestDTO.FeeType;
12096

121-
Result result = await this.ApplyUpdates(async ((EstateAggregate estateAggregate, ContractAggregate contractAggregate) aggregates) => {
97+
try
98+
{
99+
Result<ContractAggregate> contractResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<ContractAggregate>(command.ContractId, ct), command.ContractId, cancellationToken);
100+
if (contractResult.IsFailed)
101+
return ResultHelpers.CreateFailure(contractResult);
122102

123-
if (aggregates.contractAggregate.IsCreated == false)
103+
ContractAggregate contractAggregate = contractResult.Data;
104+
if (contractAggregate.IsCreated == false)
124105
{
125106
return Result.Forbidden($"Contract Id [{command.ContractId}] must be created to add transaction fees");
126107
}
127108

128-
List<Product> products = aggregates.contractAggregate.GetProducts();
109+
List<Product> products = contractAggregate.GetProducts();
129110
Product product = products.SingleOrDefault(p => p.ContractProductId == command.ProductId);
130111
if (product == null)
131112
{
132-
throw new InvalidOperationException($"Product Id [{command.ProductId}] not added to contract [{aggregates.contractAggregate.Description}]");
113+
throw new InvalidOperationException($"Product Id [{command.ProductId}] not added to contract [{contractAggregate.Description}]");
133114
}
134115

135-
aggregates.contractAggregate.AddTransactionFee(product, command.TransactionFeeId, command.RequestDTO.Description, calculationType, feeType, command.RequestDTO.Value);
116+
contractAggregate.AddTransactionFee(product, command.TransactionFeeId, command.RequestDTO.Description, calculationType, feeType, command.RequestDTO.Value);
136117

137-
return Result.Success();
138-
}, command.EstateId, command.ContractId, cancellationToken);
139-
return result;
118+
Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken);
119+
if (saveResult.IsFailed)
120+
return ResultHelpers.CreateFailure(saveResult);
121+
122+
return saveResult;
123+
}
124+
catch (Exception ex)
125+
{
126+
return Result.Failure(ex.GetExceptionMessages());
127+
}
140128

141129
}
142130

143131
public async Task<Result> DisableTransactionFeeForProduct(ContractCommands.DisableTransactionFeeForProductCommand command, CancellationToken cancellationToken)
144132
{
145-
Result result = await this.ApplyUpdates(async ((EstateAggregate estateAggregate, ContractAggregate contractAggregate) aggregates) => {
133+
try
134+
{
135+
Result<ContractAggregate> contractResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<ContractAggregate>(command.ContractId, ct), command.ContractId, cancellationToken);
136+
if (contractResult.IsFailed)
137+
return ResultHelpers.CreateFailure(contractResult);
138+
139+
ContractAggregate contractAggregate = contractResult.Data;
146140

147-
aggregates.contractAggregate.DisableTransactionFee(command.ProductId, command.TransactionFeeId);
148-
return Result.Success();
149-
}, command.EstateId, command.ContractId, cancellationToken);
150-
return result;
141+
contractAggregate.DisableTransactionFee(command.ProductId, command.TransactionFeeId);
142+
143+
Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken);
144+
if (saveResult.IsFailed)
145+
return ResultHelpers.CreateFailure(saveResult);
146+
147+
return saveResult;
148+
}
149+
catch (Exception ex)
150+
{
151+
return Result.Failure(ex.GetExceptionMessages());
152+
}
151153
}
152154

153155
public async Task<Result> CreateContract(ContractCommands.CreateContractCommand command, CancellationToken cancellationToken)
154156
{
155-
Result result = await this.ApplyUpdates(async ((EstateAggregate estateAggregate, ContractAggregate contractAggregate) aggregates) => {
157+
try
158+
{
159+
Result<EstateAggregate> estateResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.Get<EstateAggregate>(command.EstateId, ct), command.EstateId, cancellationToken);
160+
if (estateResult.IsFailed)
161+
return ResultHelpers.CreateFailure(estateResult);
156162

157-
Models.Estate.Estate estate = aggregates.estateAggregate.GetEstate();
163+
Result<ContractAggregate> contractResult = await DomainServiceHelper.GetAggregateOrFailure(ct => this.AggregateService.GetLatest<ContractAggregate>(command.ContractId, ct), command.ContractId, cancellationToken, false);
164+
if (contractResult.IsFailed)
165+
return ResultHelpers.CreateFailure(contractResult);
166+
167+
ContractAggregate contractAggregate = contractResult.Data;
168+
EstateAggregate estateAggregate = estateResult.Data;
169+
170+
171+
Models.Estate.Estate estate = estateAggregate.GetEstate();
158172
if (estate.Operators.Any(o => o.OperatorId == command.RequestDTO.OperatorId) == false)
159173
{
160174
return Result.NotFound($"Unable to create a contract for an operator that is not setup on estate [{estate.Name}]");
@@ -182,15 +196,22 @@ public async Task<Result> CreateContract(ContractCommands.CreateContractCommand
182196
}
183197
}
184198

185-
if (aggregates.contractAggregate.IsCreated)
199+
if (contractAggregate.IsCreated)
186200
{
187201
return Result.Forbidden($"Contract Id [{command.ContractId}] already created for estate [{estate.Name}]");
188202
}
189-
aggregates.contractAggregate.Create(command.EstateId, command.RequestDTO.OperatorId, command.RequestDTO.Description);
203+
contractAggregate.Create(command.EstateId, command.RequestDTO.OperatorId, command.RequestDTO.Description);
190204

191-
return Result.Success();
192-
}, command.EstateId, command.ContractId, cancellationToken, false);
193-
return result;
205+
Result saveResult = await this.AggregateService.Save(contractAggregate, cancellationToken);
206+
if (saveResult.IsFailed)
207+
return ResultHelpers.CreateFailure(saveResult);
208+
209+
return saveResult;
210+
}
211+
catch (Exception ex)
212+
{
213+
return Result.Failure(ex.GetExceptionMessages());
214+
}
194215
}
195216

196217
#endregion

0 commit comments

Comments
 (0)