1

我正在编写我的第一个 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;
    }
}
4

1 回答 1

1

第一次做的不错。根据您连接字段的方式,它们具有野兔readonly以及缺少无参数公共构造函数的事实可能会导致大多数序列化机制出现问题 - 即使它们成为自动属性。

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.

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

(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).

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...

于 2013-03-30T23:02:00.137 回答