// The client code works with an instance of a concrete creator, albeit // through its base interface. As long as the client keeps working with // the creator via the base interface, you can pass it any creator's // subclass. public void ClientCode(BaseCreator creator) { // ... Console.WriteLine("Client: I'm not aware of the creator's class," + "but it still works.\n" + creator.SomeOperation()); // ... }
public async ValueTask <(JsonStatus status, Error error)> CreateMessageAsync( BaseCreator creator, BaseCreator target, MessageType type, string content, MessageSource source) { var message = new Message { Creator = creator, Content = content, Receiver = target, MessageType = type, ReadState = ReadType.Unread, Source = source }; try { await this.collection.InsertOneAsync(message); return(JsonStatus.Succeed, Error.Empty); } catch (Exception e) { return(JsonStatus.MessageCreateFailed, Error.Create(Errors.MessageCreateFailed, e.Message)); } }
public async Task <JsonResult> AddCommentAsync(string id, [FromBody] CommentAddVM vm) { if (vm == null) { return(this.JsonFormat(false, JsonStatus.BodyFormIsNull)); } var creator = this.GetUser(); if (creator.UUID == null) { return(this.JsonFormat(false, JsonStatus.UserNotLogin)); } if (creator.UUID != vm.UID) { return(this.JsonFormat(false, JsonStatus.UserNotMatched)); } if (vm.UName == null) { vm.UName = creator.UserName; } var model = default(Comment); var(status, error) = default((JsonStatus, Model.Utils.Error)); if (vm.IsReply) { (status, error) = await this.status.Comments.AddReplyForCommentAsync( vm.MContent.CommentID, model = ModelCreator.CreateComment(vm, vm.MContent.CommentID, ModelType.Comment, creator.AvatarUrl)); } else { (status, error) = await this.status.AddCommentAsync( id, model = ModelCreator.CreateComment(vm, id, ModelType.Status, creator.AvatarUrl)); } if (status != JsonStatus.Succeed) { return(this.JsonFormat(false, status, error.Read())); } // send message var(entry, status02, error02) = await this.status.GetStatusDetailsAsync(id); var target = default(BaseCreator); if (vm.IsReply) { target = new BaseCreator { UUID = vm?.MContent?.UUID, UserName = vm?.MContent?.UserName } } ; else { target = new BaseCreator { UUID = entry.Creator.UUID, UserName = entry.Creator.UserName } }; if (creator.UUID != target.UUID) { var source = new MessageSource { Type = ModelType.Comment, HostID = id, TargetID = model.ID, HostType = ModelType.Status, HostAbstract = status02 == JsonStatus.Succeed ? entry.Content : null, Abstract = model.CommentContent, ParentID = vm?.MContent?.CommentID, ParentAbstract = vm?.MContent?.Content }; if (vm.IsReply) { await this.message.CreateMessageAsync(creator, target, MessageType.Reply, null, source); } else { await this.message.CreateMessageAsync(creator, target, MessageType.Comment, null, source); } } return(this.JsonFormat(true, content: model)); }