为什么我的命令事件字符串字段检索为null

编程入门 行业动态 更新时间:2024-10-26 07:37:56
本文介绍了为什么我的命令事件字符串字段检索为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在编写我的第一个EventStore测试应用程序,正在从流中重新填充对象,虽然它正确获取了数字,但标题为null,并且我不明白为什么-从流中的标题设置为null,但我确定它写的很好.

I am writing my first EventStore test app, I am re-hydrating my object from a stream, and whilst it gets the numberSold correctly, the title is null, and I don't understand why - the command when retrieved from the stream has the title set as null but I am sure it is being written OK.

新鲜的眼睛可以看到我在做什么吗?

Can a fresh pair of eyes see what I am doing wrong?

private static void Main() { using (store = WireupEventStore()) { var newBook = new Book("my book", 0); newBook.ChangeBookName("renamed book"); newBook.AdjustBooksSold(5); var idToRetrieveLater = newBook.bookId; var bookRepo = new BookRepository(store); bookRepo.Put(newBook); var bookReadBack = bookRepo.Get(idToRetrieveLater); // book name is set to null here, but count==5 is OK }

图书课

public class Book { public readonly Guid bookId; private int numberSold; private string title { get; set; } private List<object> eventsToCommit = new List<object>(); public Book(string title, int sold) { this.bookId = Guid.NewGuid(); this.title = title; this.numberSold = sold; } public Book(Guid id, IEnumerable<EventMessage> events) { this.bookId = id; foreach (var ev in events) { dynamic @eventToCall = ev.Body; Apply(@eventToCall); } } public void ChangeBookName(string name) { var nameChanged = new BookNameChanged(this.bookId, name); this.RaiseEvent(nameChanged); } public void AdjustBooksSold(int count) { var booksSold = new BookSold(this.bookId, count); this.RaiseEvent(booksSold); } private void Apply(BookNameChanged nameChanged) { this.title = nameChanged.title; } private void Apply(BookSold bookSold) { this.numberSold += bookSold.count; } protected void RaiseEvent(object eventToRaise) { dynamic @ev = eventToRaise; Apply(@ev); this.eventsToCommit.Add(eventToRaise); } public ICollection<object> GetEvents() { return this.eventsToCommit; } public void ResetEvents() { this.eventsToCommit = new List<object>(); } }

图书库

public class BookRepository { IStoreEvents store; public BookRepository(IStoreEvents store) { this.store = store; } public void Put(Book book) { using (var stream = this.store.OpenStream(book.bookId, 0, int.MaxValue)) { foreach (object o in book.GetEvents()) { stream.Add(new EventMessage { Body = @o }); } stream.CommitChanges(Guid.NewGuid()); book.ResetEvents(); } } public Book Get(Guid id) { using (var commits = this.store.OpenStream(id, 0, int.MaxValue)) { var eventsToReply = commits.CommittedEvents; return new Book(id, eventsToReply); } } }

命令

public class BookNameChanged { public readonly Guid id; public readonly string title; public BookNameChanged(Guid id, string bookName) { this.id = id; this.title = bookName; } } public class BookSold { public readonly Guid id; public readonly int count; public BookSold(Guid id, int count) { this.id = id; this.count = count; } }

推荐答案

首次使用会很好.根据您的连接方式,字段,字段具有readonly的事实以及缺少无参数的公共构造函数的事实很可能会导致大多数序列化机制出现问题,即使它们成为自动属性.

Nice work for a first go. Depending on how you've wired up, fields, the fact that they hare readonly and lack of a parameterless public constructor are likely to cause issues with most serialization mechanisms - i.e. make them auto properties.

虽然我通常喜欢通过约束内容来保护不变量,但重要的是要平衡这一事实,即一旦事件发生,事件就被完全烘焙了-因此具有可写属性的POCO并不像您想要的那样疯狂想.

While I generally like to protect invariants by constraining stuff as you have, it's important to balance this with the fact that an event, once it has happened is fully baked - so a POCO with writable properties isnt as crazy as you'd think.

我要做的另一件事是摆脱事件中的ID.

One other thing I'd do is get rid of the ids out of the events.

(然后加入DDD-CQRS邮件列表-最近有一个讨论涉及胖事件的概念-例如,重述可以从以前的事件中收集的内容[在您知道事件处理程序需要什么的基础上对事件做出反应],我认为这是个坏主意.

(And go join the DDD-CQRS mailing list - there's a recent discussion that touched on the notion of fat events - i.e. restating stuff that can be gleaned from previous events [on the basis that you know what an event handler needs to react to an event] which I agree is a bad idea).

我必须发布我的AggregateBase-您的建议有些微妙,但我会改变很多琐碎的事情.如果我没有早点戳一个星期就给我戳...

I must post my AggregateBase - there's a of subtleties you got right but lots of litle things I'd change. Poke me in a week if I haven't done it sooner...

更多推荐

为什么我的命令事件字符串字段检索为null

本文发布于:2023-11-25 13:44:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629990.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   字符串   命令   事件   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!