大家看看我設計的這個關於圖書館借書還書的模型屬於貧血還是充血?

tangxuehua發表於2012-09-17
之前大家也都對圖書館借書還書的例子討論很多了。所以業務需求描述我就不多說了。直接貼程式碼吧。
下面程式碼的實現基於我自己開發的一個EventSourcing框架,繼承自EventSourcing<T>類的表明是一個事件源。

/// <summary>
/// 圖書館賬號
/// </summary>
public class Account : EventSource<Guid>
{
    public Account() { }
    public Account(string number, string owner)
        : base(Guid.NewGuid())
    {
        Assert.IsNotNullOrWhiteSpace(number);
        Assert.IsNotNullOrWhiteSpace(owner);
        ApplyEvent(new AccountCreated(Id, number, owner));
    }

    public string Number { get; private set; }
    public string Owner { get; private set; }

    private void OnAccountCreated(AccountCreated evnt)
    {
        Number = evnt.Number;
        Owner = evnt.Owner;
    }
}
/// <summary>
/// 賬號建立事件
/// </summary>
public class AccountCreated : IEvent
{
    public Guid Id { get; private set; }
    public string Number { get; private set; }
    public string Owner { get; private set; }

    public AccountCreated(Guid id, string number, string owner)
    {
        Id = id;
        Number = number;
        Owner = owner;
    }
}
<p class="indent">

相關文章