Skip to content

Instantly share code, notes, and snippets.

@pjvds
Created December 17, 2010 15:12
Show Gist options
  • Save pjvds/745087 to your computer and use it in GitHub Desktop.
Save pjvds/745087 to your computer and use it in GitHub Desktop.
Poco Aggregate Root braindump

Intro

This is just an braindrump and rolling gist that reflect the thought about POCO support for Ncqrs.

The Poco object

  1. Contain typed event handling methods like OnCustomerNameChanged(NameChanged e) that update internal state.
  2. Contains a Guid ID property. This will hold the event source id.
  3. Contains one ctor for 'new' instances that only accepts an Func callback.
  4. Contains one ctor to support loading from history. This ctor has two parameters. On Func callback and an Guid value with the existing id.

So minimal code is:

public class TestPocoAggregateRoot
{
    private readonly Action<ISourcedEvent> ApplyEvent;
    public Guid Id { get; private set; }

    private int _state;

    public TestPocoAggregateRoot(Guid id, Action<ISourcedEvent> applyEvent) : this(applyEvent)
    {
        Id = id;
    }

    public TestPocoAggregateRoot(Action<ISourcedEvent> applyEvent)
    {
        ApplyEvent = applyEvent;
    }

    public void DoSomething(int arguments)
    {
        ApplyEvent(new SomethingDone{ Value = arguments * 42 });
    }

    protected void OnSomethingDone(SomethingDone evnt)
    {
        _state += evnt.Value;
    }
}

public class SomethingDone : SourcedEvent
{
    public int Value { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment