Skip to content

Instantly share code, notes, and snippets.

@hallgren
Forked from jesjos/proposal.rb
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save hallgren/f7df85d836cdf3a4f580 to your computer and use it in GitHub Desktop.

Select an option

Save hallgren/f7df85d836cdf3a4f580 to your computer and use it in GitHub Desktop.

Revisions

  1. Morgan Hallgren revised this gist Jan 13, 2015. 1 changed file with 7 additions and 6 deletions.
    13 changes: 7 additions & 6 deletions proposal.rb
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,12 @@
    # = Example Sandthorn setup

    Sandthorn.config do |sand|
    sand.snapshot_store = SnapshotStore.new
    snapshot_store = SnapshotStore.new
    sand.event_stores = {
    default: SequelStore.new(url: "sqlite://my_db", snapshot_store: sand.snapshot_store)
    alternative: MongoStore.new(url: "some_url", snapshot_store: sand.snapshot_store)
    aux: InMemoryStore.new
    default: EventStore.new(event_driver: SequelDriver.new(url: "sqlite://my_db"), snapshot_driver: snapshot_store)
    mongo: EventStore.new(event_driver: MongoDBDriver.new(), snapshot_driver: snapshot_store)
    alternative: ObjectStore.new(object_driver: InMemoryDriver.new)
    aux: InMemoryStore.new
    }
    # This maps aggregates to event stores.
    # Aggregates that aren't exlicitly mapped will be stored in the default store.
    @@ -19,7 +20,7 @@
    # Example aggregate

    class MyAggregate
    include AggregateRoot
    include EventStore::AggregateRoot

    attr_reader :name

    @@ -49,7 +50,7 @@ def name_changed

    # Here's an incomplete implementation of AggregateRoot

    module Sandthorn
    module EventStore
    module AggregateRoot

    attr_reader :unsaved_events
  2. @jesjos jesjos revised this gist Jan 12, 2015. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion proposal.rb
    Original file line number Diff line number Diff line change
    @@ -54,7 +54,11 @@ module AggregateRoot

    attr_reader :unsaved_events

    class << self
    def self.included(base)
    base.extend(ClassMethods)
    end

    module ClassMethods
    def event_store
    Sandthorn.event_store_for(self)
    end
  3. @jesjos jesjos revised this gist Jan 12, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion proposal.rb
    Original file line number Diff line number Diff line change
    @@ -45,7 +45,7 @@ def name_changed
    # Saving means finding the appropriate event store for the aggregate type, and then calling `save_events` on the event store with any unsaved events.
    # Example:
    # 1. my_aggregate.save calls Sandthorn.event_store_for(my_aggregate) and gets an event_store
    # 2. Call event_store.save_events(my_aggregate, )
    # 2. Call event_store.save_events(my_aggregate, my_aggregate.unsaved_events)

    # Here's an incomplete implementation of AggregateRoot

  4. @jesjos jesjos created this gist Jan 12, 2015.
    80 changes: 80 additions & 0 deletions proposal.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    # = Example Sandthorn setup

    Sandthorn.config do |sand|
    sand.snapshot_store = SnapshotStore.new
    sand.event_stores = {
    default: SequelStore.new(url: "sqlite://my_db", snapshot_store: sand.snapshot_store)
    alternative: MongoStore.new(url: "some_url", snapshot_store: sand.snapshot_store)
    aux: InMemoryStore.new
    }
    # This maps aggregates to event stores.
    # Aggregates that aren't exlicitly mapped will be stored in the default store.
    # I see the need to be able to define either concrete classes or patterns.
    sand.aggregate_mappings = {
    alternative: [StoreAggregates::Foo, StoreAggregates::Bar]
    aux: /LogAggregates/
    }
    end

    # Example aggregate

    class MyAggregate
    include AggregateRoot

    attr_reader :name

    def initialize(name: nil)
    @name = name
    end

    def change_name(name)
    @name = name
    name_changed
    end

    def name_changed
    commit
    end
    end

    # = Committing
    # When commit is called, we just construct the appropriate event and save it in the aggregate.
    # The events should be in the agreed-upon event format.

    # = Saving an aggregate
    # Saving means finding the appropriate event store for the aggregate type, and then calling `save_events` on the event store with any unsaved events.
    # Example:
    # 1. my_aggregate.save calls Sandthorn.event_store_for(my_aggregate) and gets an event_store
    # 2. Call event_store.save_events(my_aggregate, )

    # Here's an incomplete implementation of AggregateRoot

    module Sandthorn
    module AggregateRoot

    attr_reader :unsaved_events

    class << self
    def event_store
    Sandthorn.event_store_for(self)
    end
    end

    def save
    if unsaved_events.any?
    save_events(unsaved_events)
    end
    end

    private

    def save_events(events)
    event_store.save_events(self, events)
    end

    def event_store
    self.class.event_store
    end

    end
    end