为什么我的命令事件字符串字段被检索为空(Why is my command event string

2019-10-17 22:50发布

我写我的第一个EventStore测试应用程序,我重新保湿我的对象从流,并同时它正确地得到numberSold,标题是空的,我不明白为什么 - 从流检索时的命令有标题设置为空,但我相信它正在被写入确定。

一双崭新的眼睛能看到什么,我做错了什么?

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;
    }
}

Answer 1:

对于第一次去尼斯的工作。 根据您如何连接起来,田野,他们野兔事实readonly和缺乏一个无参数的公共构造很可能与大多数序列化机制造成的问题-即让他们自动属性。

所以POCO有可写的属性不是一样,你会觉得疯狂 - 虽然我一般喜欢通过约束的东西,你要保护的不变量,它与事实的事件,一旦发生完全出炉,以平衡这是很重要的。

我会做的另一件事就是摆脱了IDS出来的事件。

(和去参加DDD-CQRS邮件列表 - 还有,关于脂肪的事件的概念,触及最近的讨论 - 即重申的东西,可以从先前的事件[基础上,你知道的事件处理程序需要一个反应是什么来收集事件]这是我同意的,是一个坏主意)。

我必须张贴我AggregateBase -有一个你有正确的精妙之处,但很多的痘痘的事情,我会改变的。 捅我在一个星期如果我没有完成它越早...



文章来源: Why is my command event string field being retrieved as null