private void Handle(CasePlanInstanceCreatedEvent evt) { Roles = evt.Roles.ToList(); var unknownRoles = evt.Permissions.Where(_ => !Roles.Any(__ => __.Id == _.Id)); if (unknownRoles.Any()) { throw new AggregateValidationException(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("validation", $"unknown roles '{string.Join(",", unknownRoles.Select(_ => _.Id))}'") }); } if (!string.IsNullOrWhiteSpace(evt.CaseOwner)) { if (Roles.FirstOrDefault(_ => _.Id == evt.CaseOwner) == null) { throw new AggregateValidationException(new List <KeyValuePair <string, string> > { new KeyValuePair <string, string>("validation", $"unknown case owner role '{evt.CaseOwner}'") }); } } foreach (var permission in evt.Permissions) { var role = Roles.First(_ => _.Id == permission.Id); role.Claims = permission.Claims; } AggregateId = evt.AggregateId; if (!string.IsNullOrWhiteSpace(evt.JsonContent)) { Children.Add(StageElementInstance.FromJson(evt.JsonContent)); } if (evt.Files != null && evt.Files.Any()) { foreach (var file in evt.Files) { Children.Add(file.Clone() as CaseFileItemInstance); } } CreateDateTime = evt.CreateDateTime; UpdateDateTime = evt.CreateDateTime; Version = evt.Version; CasePlanId = evt.CasePlanId; CaseOwner = evt.CaseOwner; if (evt.Parameters != null) { foreach (var kvp in evt.Parameters) { ExecutionContext.SetStrVariable(kvp.Key, kvp.Value); } } }
public static CasePlanInstanceAggregate New(string id, StageElementInstance stage, ICollection <CaseFileItemInstance> caseFiles) { var result = new CasePlanInstanceAggregate(); var evt = new CasePlanInstanceCreatedEvent(Guid.NewGuid().ToString(), id, 0, null, new List <CasePlanInstanceRole>(), new List <CasePlanInstanceRole>(), stage.ToJson(), DateTime.UtcNow, null, string.Empty, new Dictionary <string, string>(), caseFiles); result.Handle(evt); result.DomainEvents.Add(evt); return(result); }