Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

No way to resolve SagaBase<> implementations against IoC container #20

Open
aleksandrov opened this Issue · 3 comments

2 participants

@aleksandrov

Currently SagaEventStoreRepository creates instances of sagas using "Activator.CreateInstance()".
Need possibility to resolve TSaga againts IoC container.

EventStoreRepository have IConstructAggregates hook to do this, it will be great to have something similar for SagaEventStoreRepository.

@aleksandrov

Hi Jonathan,

Here is patch for this (sorry about Hg, I dont have Git):


diff -r 703305dbb140 -r 07508ae533ed libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs
--- a/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs  Sun Jan 15 09:43:58 2012 +0200
+++ b/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs  Sun Jan 15 10:25:36 2012 +0200
@@ -12,13 +12,15 @@
        private const string UndispatchedMessageHeader = "UndispatchedMessage.";
        private readonly IDictionary<Guid, IEventStream> streams = new Dictionary<Guid, IEventStream>();
        private readonly IStoreEvents eventStore;
+        private readonly IConstructSagas factory;

-       public SagaEventStoreRepository(IStoreEvents eventStore)
+        public SagaEventStoreRepository(IStoreEvents eventStore, IConstructSagas factory)
        {
-           this.eventStore = eventStore;
+           this.eventStore = eventStore;
+           this.factory = factory;
        }

-       public void Dispose()
+       public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
@@ -59,9 +61,9 @@
            return this.streams[sagaId] = stream;
        }

-       private static TSaga BuildSaga<TSaga>(IEventStream stream) where TSaga : class, ISaga, new()
+       private TSaga BuildSaga<TSaga>(IEventStream stream) where TSaga : class, ISaga, new()
        {
-           var saga = new TSaga();
+           var saga = factory.Build<TSaga>();
            foreach (var @event in stream.CommittedEvents.Select(x => x.Body))
                saga.Transition(@event);

diff -r 703305dbb140 -r 07508ae533ed libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/CommonDomain.Persistence.csproj
--- a/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/CommonDomain.Persistence.csproj Sun Jan 15 09:43:58 2012 +0200
+++ b/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/CommonDomain.Persistence.csproj Sun Jan 15 10:25:36 2012 +0200
@@ -46,6 +46,7 @@
     </Compile>
     <Compile Include="ConflictingCommandException.cs" />
     <Compile Include="IConstructAggregates.cs" />
+    <Compile Include="IConstructSagas.cs" />
     <Compile Include="IRepository.cs" />
     <Compile Include="ISagaRepository.cs" />
     <Compile Include="PersistenceException.cs" />
diff -r 703305dbb140 -r 07508ae533ed libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/IConstructSagas.cs
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/IConstructSagas.cs  Sun Jan 15 10:25:36 2012 +0200
@@ -0,0 +1,12 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace CommonDomain.Persistence
+{
+    public interface IConstructSagas
+    {
+        TSaga Build<TSaga>() where TSaga : ISaga;
+    }
+}
@aleksandrov

And additional fix for that

diff -r 07508ae533ed libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs
--- a/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs  Sun Jan 15 10:25:36 2012 +0200
+++ b/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence.EventStore/SagaEventStoreRepository.cs  Mon Jan 16 11:01:00 2012 +0200
@@ -39,7 +39,7 @@
            }
        }

-       public TSaga GetById<TSaga>(Guid sagaId) where TSaga : class, ISaga, new()
+       public TSaga GetById<TSaga>(Guid sagaId) where TSaga : class, ISaga
        {
            return BuildSaga<TSaga>(this.OpenStream(sagaId));
        }
@@ -61,7 +61,7 @@
            return this.streams[sagaId] = stream;
        }

-       private TSaga BuildSaga<TSaga>(IEventStream stream) where TSaga : class, ISaga, new()
+       private TSaga BuildSaga<TSaga>(IEventStream stream) where TSaga : class, ISaga
        {
            var saga = factory.Build<TSaga>();
            foreach (var @event in stream.CommittedEvents.Select(x => x.Body))
diff -r 07508ae533ed libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/ISagaRepository.cs
--- a/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/ISagaRepository.cs  Sun Jan 15 10:25:36 2012 +0200
+++ b/libs/joliver-CommonDomain-1.4.0-9-g3d5a3e9/joliver-CommonDomain-3d5a3e9/src/proj/CommonDomain.Persistence/ISagaRepository.cs  Mon Jan 16 11:01:00 2012 +0200
@@ -5,7 +5,7 @@

    public interface ISagaRepository
    {
-       TSaga GetById<TSaga>(Guid sagaId) where TSaga : class, ISaga, new();
+       TSaga GetById<TSaga>(Guid sagaId) where TSaga : class, ISaga;
        void Save(ISaga saga, Guid commitId, Action<IDictionary<string, object>> updateHeaders);
    }
 }
@joliver

Could you make this into a pull request?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.