Skip to content

Commit

Permalink
Merge pull request #304 from Particular/release-3.2
Browse files Browse the repository at this point in the history
Support Core sagas in 3.2
  • Loading branch information
DavidBoike authored Jul 2, 2018
2 parents ba568c1 + 6118911 commit 75f7fd4
Show file tree
Hide file tree
Showing 261 changed files with 2,226 additions and 17,060 deletions.
48 changes: 42 additions & 6 deletions src/AcceptanceTestHelper/RuntimeSagaDefinitionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Runtime.Serialization;
using Mono.Cecil;
using NServiceBus;
using NServiceBus.Persistence.Sql;
using NServiceBus.Persistence.Sql.ScriptBuilder;

public static class RuntimeSagaDefinitionReader
Expand Down Expand Up @@ -40,6 +41,11 @@ public static IEnumerable<SagaDefinition> GetSagaDefinitions(EndpointConfigurati

public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sqlDialect)
{
if (SagaTypeHasIntermediateBaseClass(sagaType))
{
throw new Exception("Saga implementations must inherit from either Saga<T> or SqlSaga<T> directly. Deep class hierarchies are not supported.");
}

var saga = (Saga)FormatterServices.GetUninitializedObject(sagaType);
var mapper = new ConfigureHowToFindSagaWithMessage();
methodInfo.Invoke(saga, new object[]
Expand All @@ -53,10 +59,8 @@ public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sq
name: mapper.CorrelationProperty,
type: CorrelationPropertyTypeReader.GetCorrelationPropertyType(mapper.CorrelationType));
}
var transitionalCorrelationPropertyName = (string)sagaType
.GetProperty("TransitionalCorrelationPropertyName", AnyInstanceMember)
.GetValue(saga);


var transitionalCorrelationPropertyName = GetSagaMetadataProperty(sagaType, saga, "TransitionalCorrelationPropertyName", att => att.TransitionalCorrelationProperty);

CorrelationProperty transitional = null;
if (transitionalCorrelationPropertyName != null)
Expand All @@ -65,8 +69,8 @@ public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sq
var transitionalProperty = sagaDataType.GetProperty(transitionalCorrelationPropertyName, AnyInstanceMember);
transitional = new CorrelationProperty(transitionalCorrelationPropertyName, CorrelationPropertyTypeReader.GetCorrelationPropertyType(transitionalProperty.PropertyType));
}

var tableSuffixOverride = (string)sagaType.GetProperty("TableSuffix", AnyInstanceMember).GetValue(saga);
var tableSuffixOverride = GetSagaMetadataProperty(sagaType, saga, "TableSuffix", att => att.TableSuffix);
var tableSuffix = tableSuffixOverride ?? sagaType.Name;

if (sqlDialect == BuildSqlDialect.Oracle)
Expand All @@ -80,4 +84,36 @@ public static SagaDefinition GetSagaDefinition(Type sagaType, BuildSqlDialect sq
correlationProperty: correlationProperty,
transitionalCorrelationProperty: transitional);
}

static bool SagaTypeHasIntermediateBaseClass(Type sagaType)
{
var baseType = sagaType.BaseType;
if (!baseType.IsGenericType)
{
// Saga<T> and SqlSaga<T> are both generic types
return true;
}

var genericBase = baseType.GetGenericTypeDefinition();
return genericBase != typeof(Saga<>) && genericBase != typeof(SqlSaga<>);
}

static string GetSagaMetadataProperty(Type sagaType, Saga instance, string sqlSagaPropertyName, Func<SqlSagaAttribute, string> getSqlSagaAttributeValue)
{
if (sagaType.IsSubclassOfRawGeneric(typeof(SqlSaga<>)))
{
// ReSharper disable once PossibleNullReferenceException
return (string)sagaType
.GetProperty(sqlSagaPropertyName, AnyInstanceMember)
.GetValue(instance);
}

if (sagaType.IsSubclassOfRawGeneric(typeof(Saga<>)))
{
var attr = sagaType.GetCustomAttribute<SqlSagaAttribute>();
return (attr != null) ? getSqlSagaAttributeValue(attr) : null;
}

throw new Exception($"Type '{sagaType.FullName}' is not a Saga<T>.");
}
}
264 changes: 0 additions & 264 deletions src/AcceptanceTestsHolder/AcceptanceTestsHolder.csproj

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 75f7fd4

Please sign in to comment.