public async Task <ICommandResult <TReply> > CreateAsync(TReply reply) { // Validate if (reply == null) { throw new ArgumentNullException(nameof(reply)); } if (reply.Id > 0) { throw new ArgumentOutOfRangeException(nameof(reply.Id)); } if (reply.EntityId <= 0) { throw new ArgumentOutOfRangeException(nameof(reply.EntityId)); } if (String.IsNullOrWhiteSpace(reply.Message)) { throw new ArgumentNullException(nameof(reply.Message)); } if (reply.CreatedDate == null) { throw new ArgumentNullException(nameof(reply.CreatedDate)); } var result = new CommandResult <TReply>(); // Ensure the entity exists var entity = await _entityStore.GetByIdAsync(reply.EntityId); if (entity == null) { return(result.Failed(new CommandError($"An entity with the Id '{reply.EntityId}' could not be found"))); } // Parse Html and abstract reply.Html = await ParseEntityHtml(reply.Message); reply.Abstract = await ParseEntityAbstract(reply.Message); reply.Urls = await ParseEntityUrls(reply.Html); // Raise creating event Creating?.Invoke(this, new EntityReplyEventArgs <TReply>(entity, reply)); // Invoke EntityReplyCreating subscriptions foreach (var handler in _broker.Pub <TReply>(this, "EntityReplyCreating")) { reply = await handler.Invoke(new Message <TReply>(reply, this)); } var newReply = await _entityReplyStore.CreateAsync(reply); if (newReply != null) { // Raise created event Created?.Invoke(this, new EntityReplyEventArgs <TReply>(entity, newReply)); // Invoke EntityReplyCreated subscriptions foreach (var handler in _broker.Pub <TReply>(this, "EntityReplyCreated")) { newReply = await handler.Invoke(new Message <TReply>(newReply, this)); } return(result.Success(newReply)); } return(result.Failed(new CommandError("An unknown error occurred whilst attempting to create the reply"))); }