Skip to content

Commit

Permalink
Fix null check warning
Browse files Browse the repository at this point in the history
  • Loading branch information
kbaley committed Feb 15, 2023
1 parent 8550ee7 commit 7ef46e1
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 22 deletions.
7 changes: 2 additions & 5 deletions src/ScriptBuilder/Saga/InstructionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ static InstructionAnalyzer()

public static IList<Instruction> GetConfigureHowToFindSagaInstructions(TypeDefinition sagaTypeDefinition)
{
var configureMethod = sagaTypeDefinition.Methods.FirstOrDefault(m => m.Name == "ConfigureHowToFindSaga");
if (configureMethod == null)
{
throw new ErrorsException("Saga does not contain a ConfigureHowToFindSaga method.");
}
var configureMethod = sagaTypeDefinition.Methods.FirstOrDefault(m => m.Name == "ConfigureHowToFindSaga")
?? throw new ErrorsException("Saga does not contain a ConfigureHowToFindSaga method.");

return configureMethod.Body.Instructions;
}
Expand Down
8 changes: 2 additions & 6 deletions src/ScriptBuilder/Saga/SagaDefinitionReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,8 @@ static CorrelationProperty BuildConstraintProperty(TypeDefinition sagaDataTypeDe
return null;
}

var propertyDefinition = sagaDataTypeDefinition.FindInTypeHierarchy(t => t.Properties.SingleOrDefault(x => x.Name == propertyName));

if (propertyDefinition == null)
{
throw new ErrorsException($"Expected type '{sagaDataTypeDefinition.FullName}' to contain a property named '{propertyName}'.");
}
var propertyDefinition = sagaDataTypeDefinition.FindInTypeHierarchy(t => t.Properties.SingleOrDefault(x => x.Name == propertyName))
?? throw new ErrorsException($"Expected type '{sagaDataTypeDefinition.FullName}' to contain a property named '{propertyName}'.");
if (propertyDefinition.SetMethod == null)
{
throw new ErrorsException($"The type '{sagaDataTypeDefinition.FullName}' has a constraint property '{propertyName}' that is read-only.");
Expand Down
7 changes: 2 additions & 5 deletions src/SqlPersistence/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,8 @@ public static async Task<Guid> GetGuidAsync(this DbDataReader reader, int positi

internal static Func<T, object> GetPropertyAccessor<T>(this Type sagaDataType, string propertyName)
{
var propertyInfo = sagaDataType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
if (propertyInfo == null)
{
throw new Exception($"Expected '{sagaDataType.FullName}' to contain a gettable property named '{propertyName}'.");
}
var propertyInfo = sagaDataType.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
?? throw new Exception($"Expected '{sagaDataType.FullName}' to contain a gettable property named '{propertyName}'.");
return data => propertyInfo.GetValue(data);
}

Expand Down
9 changes: 3 additions & 6 deletions src/SqlPersistence/Saga/SagaPersister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ static void AddTransitionalParameter(IContainSagaData sagaData, RuntimeSagaInfo
{
return;
}
var transitionalId = sagaInfo.TransitionalAccessor(sagaData);
if (transitionalId == null)
{
//TODO: validate non default for value types
throw new Exception($"Null transitionalCorrelationProperty is not allowed. SagaDataType: {sagaData.GetType().FullName}.");
}

//TODO: validate non default for value types if TransitionalAccessor returns null
var transitionalId = sagaInfo.TransitionalAccessor(sagaData) ?? throw new Exception($"Null transitionalCorrelationProperty is not allowed. SagaDataType: {sagaData.GetType().FullName}.");
command.AddParameter("TransitionalCorrelationId", transitionalId);
}

Expand Down

0 comments on commit 7ef46e1

Please sign in to comment.