From 0568dc10ef0bfaab2b240e3b7696427a6fabc507 Mon Sep 17 00:00:00 2001 From: jletroui Date: Sun, 6 Nov 2011 09:20:00 -0500 Subject: [PATCH] Simplified a couple of things. Made the event store a single table. --- .../ContainerCommandBus.cs | 6 ++- .../SqlServerEventStore.cs | 37 ++++++------------- .../SqlServerPersistenceManager.cs | 1 + .../Presentation/Views/Shared/Site.Master | 2 +- DDDPart6/Presentation/Web.config | 2 +- DDDPart6/Presentation/schema.sql | 12 +++--- 6 files changed, 27 insertions(+), 33 deletions(-) diff --git a/DDDPart6/Infrastructure.Impl/ContainerCommandBus.cs b/DDDPart6/Infrastructure.Impl/ContainerCommandBus.cs index 24a844e..95c0587 100644 --- a/DDDPart6/Infrastructure.Impl/ContainerCommandBus.cs +++ b/DDDPart6/Infrastructure.Impl/ContainerCommandBus.cs @@ -11,10 +11,12 @@ namespace Infrastructure.Impl public class ContainerCommandBus : ICommandBus { private IContainer container; + private IPersistenceManager persistenceManager; - public ContainerCommandBus(IContainer container) + public ContainerCommandBus(IContainer container, IPersistenceManager persistenceManager) { this.container = container; + this.persistenceManager = persistenceManager; } public void Send(T cmd) where T : ICommand @@ -27,6 +29,8 @@ public ContainerCommandBus(IContainer container) try { handler.Handle(cmd); + // Trigger persistence and concurrency checking. + persistenceManager.Commit(); handled = true; } catch (ConcurrencyException) diff --git a/DDDPart6/Infrastructure.Impl/SqlServerEventStore.cs b/DDDPart6/Infrastructure.Impl/SqlServerEventStore.cs index b64b36d..296ca66 100644 --- a/DDDPart6/Infrastructure.Impl/SqlServerEventStore.cs +++ b/DDDPart6/Infrastructure.Impl/SqlServerEventStore.cs @@ -20,40 +20,27 @@ public SqlServerEventStore(IPersistenceManager persistenceManager) this.persistenceManager = persistenceManager; } + private const int UniqueKeyViolation = 2627; + public void PersistUncommitedEvents(IAggregateRoot aggregate) { - persistenceManager.ExecuteNonQuery( - "INSERT INTO [Events] (Id, aggregate_id, version, data) VALUES (@Id, @AggregateId, @Version, @Data)", - new - { - Id = Guid.NewGuid(), - Version = aggregate.Version + 1, - AggregateId = aggregate.Id, - Data = Serialize(aggregate.UncommitedEvents) - }); - - if (aggregate.Version == 0) + try { persistenceManager.ExecuteNonQuery( - "INSERT INTO [Aggregates] (aggregate_id, version) VALUES (@AggregateId, 1)", + "INSERT INTO [Events] (Id, aggregate_id, version, data) VALUES (@Id, @AggregateId, @Version, @Data)", new { - AggregateId = aggregate.Id + Id = Guid.NewGuid(), + Version = aggregate.Version + 1, + AggregateId = aggregate.Id, + Data = Serialize(aggregate.UncommitedEvents) }); } - else + catch (SqlException se) { - var rowCount = persistenceManager.ExecuteNonQuery( - "UPDATE [Aggregates] SET Version = @Version WHERE aggregate_id = @AggregateId AND Version = @Expected", - new - { - AggregateId = aggregate.Id, - Version = aggregate.Version + 1, - Expected = aggregate.Version - }); - - // The version evolved since we performed the command, so we have to retry the command. - if (rowCount != 1) throw new ConcurrencyException(); + // Thanks Jonathan Oliver's CQRS Event Store + if (se.Number == UniqueKeyViolation) throw new ConcurrencyException(); + throw; } } diff --git a/DDDPart6/Infrastructure.Impl/SqlServerPersistenceManager.cs b/DDDPart6/Infrastructure.Impl/SqlServerPersistenceManager.cs index d4b20f8..9743d84 100644 --- a/DDDPart6/Infrastructure.Impl/SqlServerPersistenceManager.cs +++ b/DDDPart6/Infrastructure.Impl/SqlServerPersistenceManager.cs @@ -96,6 +96,7 @@ public void Commit() catch (ConcurrencyException) { tx.Rollback(); + context[TRANSACTION_KEY] = null; throw; } diff --git a/DDDPart6/Presentation/Views/Shared/Site.Master b/DDDPart6/Presentation/Views/Shared/Site.Master index f619fb1..5326fd0 100644 --- a/DDDPart6/Presentation/Views/Shared/Site.Master +++ b/DDDPart6/Presentation/Views/Shared/Site.Master @@ -16,7 +16,7 @@