Skip to content

Commit

Permalink
Implements one side of the merged event pipeline simulation for bulk …
Browse files Browse the repository at this point in the history
  • Loading branch information
jordimontana82 committed May 30, 2024
1 parent 923f516 commit 60e5be6
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 45 deletions.
86 changes: 44 additions & 42 deletions src/FakeXrmEasy.Plugins/Pipeline/PipelineProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,49 @@ internal static class PipelineProcessor
PipelineProcessor.ExecutePipelineStage(context, pipelineParameters);
}

private static void ProcessPreOperation(IXrmFakedContext context,
OrganizationRequest request)
{
var preImagePreOperation = PreImage.IsAvailableFor(request.GetType(), ProcessingStepStage.Preoperation)
? GetPreImageEntityForRequest(context, request)
: null;

var pipelineParameters = new PipelineStageExecutionParameters()
{
Request = request,
Stage = ProcessingStepStage.Preoperation,
Mode = ProcessingStepMode.Synchronous,
PreEntitySnapshot = preImagePreOperation,
PostEntitySnapshot = null
};

ExecutePipelineStage(context, pipelineParameters);
}

private static void ProcessPostOperation(IXrmFakedContext context,
OrganizationRequest request,
OrganizationResponse response,
Entity preEntity = null)
{
var postImagePostOperation = PostImage.IsAvailableFor(request.GetType(), ProcessingStepStage.Postoperation)
? GetPostImageEntityForRequest(context, request)
: null;

var pipelineParameters = new PipelineStageExecutionParameters()
{
Request = request,
Response = response,
Stage = ProcessingStepStage.Postoperation,
Mode = ProcessingStepMode.Synchronous,
PreEntitySnapshot = preEntity,
PostEntitySnapshot = postImagePostOperation
};
ExecutePipelineStage(context, pipelineParameters);

pipelineParameters.Mode = ProcessingStepMode.Asynchronous;
ExecutePipelineStage(context, pipelineParameters);
}

/// <summary>
/// Gets an entity image collection for each registered entity image and the current entity record
/// </summary>
Expand Down Expand Up @@ -377,47 +420,6 @@ private static Entity GetPostImageEntityForRequest(IXrmFakedContext context, Org
return postImage;
}

private static void ProcessPreOperation(IXrmFakedContext context,
OrganizationRequest request)
{
var preImagePreOperation = PreImage.IsAvailableFor(request.GetType(), ProcessingStepStage.Preoperation)
? GetPreImageEntityForRequest(context, request)
: null;

var pipelineParameters = new PipelineStageExecutionParameters()
{
Request = request,
Stage = ProcessingStepStage.Preoperation,
Mode = ProcessingStepMode.Synchronous,
PreEntitySnapshot = preImagePreOperation,
PostEntitySnapshot = null
};

ExecutePipelineStage(context, pipelineParameters);
}

private static void ProcessPostOperation(IXrmFakedContext context,
OrganizationRequest request,
OrganizationResponse response,
Entity preEntity = null)
{
var postImagePostOperation = PostImage.IsAvailableFor(request.GetType(), ProcessingStepStage.Postoperation)
? GetPostImageEntityForRequest(context, request)
: null;

var pipelineParameters = new PipelineStageExecutionParameters()
{
Request = request,
Response = response,
Stage = ProcessingStepStage.Postoperation,
Mode = ProcessingStepMode.Synchronous,
PreEntitySnapshot = preEntity,
PostEntitySnapshot = postImagePostOperation
};
ExecutePipelineStage(context, pipelineParameters);

pipelineParameters.Mode = ProcessingStepMode.Asynchronous;
ExecutePipelineStage(context, pipelineParameters);
}

}
}
26 changes: 23 additions & 3 deletions src/FakeXrmEasy.Plugins/Pipeline/RegisteredPluginStepsRetriever.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using FakeXrmEasy.Abstractions;
using FakeXrmEasy.Abstractions.Plugins.Enums;
using FakeXrmEasy.Plugins.Extensions;
using FakeXrmEasy.Plugins.PluginInstances;
using FakeXrmEasy.Plugins.PluginSteps;
using FakeXrmEasy.Plugins.PluginSteps.Extensions;
Expand All @@ -17,9 +18,28 @@ namespace FakeXrmEasy.Pipeline
/// </summary>
internal static class RegisteredPluginStepsRetriever
{
/// <summary>
/// Gets all the plugin steps needed to execute the request specified in the pipeline stage execution parameter.
/// Also implements the merged pipeline simulation where a bulk operation request might also trigger "non-bulk" plugins if they exist and were registered
/// and vice-versa (a non-bulk request triggering a bulk operation plugin)
/// </summary>
/// <param name="context">The In-Memory XrmFakedContext</param>
/// <param name="parameters">The pipeline stage execution parameter with info about the current event pipeline stage that is being executed</param>
/// <returns></returns>
internal static IEnumerable<PluginStepDefinition> GetPluginStepsForOrganizationRequest(IXrmFakedContext context, PipelineStageExecutionParameters parameters)
{
return GetStepsForStage(context, parameters);
var steps = GetStepsForStage(context, parameters);
if (parameters.Request.IsBulkOperation())
{
var nonBulkPipelineParameters = parameters.ToNonBulkPipelineExecutionParameters();
foreach (var nonBulkPipelineStageExecutionParameter in nonBulkPipelineParameters)
{
var nonBulkSteps = GetStepsForStage(context, nonBulkPipelineStageExecutionParameter);
steps.AddRange(nonBulkSteps);
}
}

return steps;
}

/// <summary>
Expand Down Expand Up @@ -85,7 +105,7 @@ internal static string GetOrganizationRequestEntityLogicalName(OrganizationReque
return null;
}

private static IEnumerable<PluginStepDefinition> GetStepsForStage(IXrmFakedContext context,
private static List<PluginStepDefinition> GetStepsForStage(IXrmFakedContext context,
PipelineStageExecutionParameters parameters)
{
int? entityTypeCode = null;
Expand Down Expand Up @@ -142,7 +162,7 @@ internal static string GetOrganizationRequestEntityLogicalName(OrganizationReque
.Where(ps => ps.EntityLogicalName != null && ps.EntityLogicalName == entityLogicalName || //Matches logical name
ps.EntityTypeCode != null && ps.EntityTypeCode.HasValue && ps.EntityTypeCode.Value == entityTypeCode || //Or matches entity type code
ps.EntityTypeCode == null && ps.EntityLogicalName == null) //Or matches plugins steps with none (Custom Apis)
.Where(ps => !ps.FilteringAttributes.Any() || ps.FilteringAttributes.Any(attr => requestDistinctAttributes.Contains(attr))).AsEnumerable();
.Where(ps => !ps.FilteringAttributes.Any() || ps.FilteringAttributes.Any(attr => requestDistinctAttributes.Contains(attr))).ToList();
}

internal static IEnumerable<Entity> GetPluginImageDefinitions(IXrmFakedContext context, Guid stepId, ProcessingStepImageType imageType)
Expand Down

0 comments on commit 60e5be6

Please sign in to comment.