Skip to content

Commit d6751b2

Browse files
committed
ADD: Extension methods for getting data out of plugin context
ADD: Inject plugin execution contexts 2 through 7
1 parent fcaafc2 commit d6751b2

File tree

5 files changed

+92
-56
lines changed

5 files changed

+92
-56
lines changed

DG.XrmPluginCore/AbstractPlugin.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,21 @@ protected ServiceProvider BuildServiceProvider(IServiceProvider serviceProvider)
136136
// Get services of the ServiceProvider
137137
var tracingService = serviceProvider.GetService<ITracingService>() ?? throw new Exception("Unable to get Tracing service");
138138
var telemetryService = serviceProvider.GetService<ILogger>();
139-
var pluginContext = serviceProvider.GetService<IPluginExecutionContext>() ?? throw new Exception("Unable to get Plugin Execution Context");
140-
var organizationServiceFactory = serviceProvider.GetService<IOrganizationServiceFactory>() ?? throw new Exception("Unable to get service factory");
141139

142140
var extendedTracingService = new ExtendedTracingService(tracingService, telemetryService);
143141

144142
// Create a new service collection and add the relevant services
145143
IServiceCollection services = new ServiceCollection();
146-
services.AddScoped(_ => pluginContext);
147-
services.AddScoped(_ => organizationServiceFactory);
144+
145+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext>());
146+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext2>());
147+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext3>());
148+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext4>());
149+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext5>());
150+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext6>());
151+
services.AddScoped(_ => serviceProvider.GetService<IPluginExecutionContext7>());
152+
153+
services.AddScoped(_ => serviceProvider.GetService<IOrganizationServiceFactory>());
148154
services.AddScoped(_ => telemetryService);
149155
services.AddScoped<ITracingService>(_ => extendedTracingService);
150156
services.AddScoped<IExtendedTracingService>(_ => extendedTracingService);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using DG.XrmPluginCore.Enums;
2+
using Microsoft.Xrm.Sdk;
3+
using System;
4+
5+
namespace DG.XrmPluginCore.Extensions
6+
{
7+
public static class PluginExecutionContextExtensions
8+
{
9+
public static T GetEntity<T>(this IPluginExecutionContext context, ITracingService trace = null) where T : Entity
10+
{
11+
if (!context.InputParameters.Contains("Target"))
12+
{
13+
trace?.Trace("Context does not contain 'Target'");
14+
return null;
15+
}
16+
17+
var target = context.InputParameters["Target"];
18+
if (target is Entity entity)
19+
{
20+
var logicalName = Activator.CreateInstance<T>().LogicalName;
21+
if (logicalName != entity.LogicalName)
22+
{
23+
trace?.Trace("'Entity' is not of specified type: {0} vs. {1}",
24+
entity.LogicalName, logicalName);
25+
return null;
26+
}
27+
28+
return entity.ToEntity<T>();
29+
}
30+
31+
var typeName = target.GetType().Name;
32+
trace?.Trace("'Target' is not an Entity. It's of type: {0}", typeName);
33+
return null;
34+
}
35+
36+
public static T GetImage<T>(this IPluginExecutionContext context, ImageType imageType, string name) where T : Entity
37+
{
38+
EntityImageCollection collection = null;
39+
if (imageType == ImageType.PreImage)
40+
{
41+
collection = context.PreEntityImages;
42+
}
43+
else if (imageType == ImageType.PostImage)
44+
{
45+
collection = context.PostEntityImages;
46+
}
47+
48+
if (collection != null && collection.TryGetValue(name, out var entity))
49+
{
50+
return entity.ToEntity<T>();
51+
}
52+
else
53+
{
54+
return null;
55+
}
56+
}
57+
58+
public static T GetImage<T>(this IPluginExecutionContext context, ImageType imageType) where T : Entity
59+
{
60+
return GetImage<T>(context, imageType, imageType.ToString());
61+
}
62+
63+
public static T GetPreImage<T>(this IPluginExecutionContext context, string name = "PreImage") where T : Entity
64+
{
65+
return GetImage<T>(context, ImageType.PreImage, name);
66+
}
67+
68+
public static T GetPostImage<T>(this IPluginExecutionContext context, string name = "PostImage") where T : Entity
69+
{
70+
return GetImage<T>(context, ImageType.PostImage, name);
71+
}
72+
}
73+
}

DG.XrmPluginCore/Plugin.cs

Lines changed: 3 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -90,63 +90,14 @@ protected static bool MatchesEventOperation(IPluginExecutionContext context, par
9090
return operations.Any(o => o == operation);
9191
}
9292

93-
protected static T GetEntity<T>(LocalPluginContext localPluginContext) where T : Entity
93+
protected static T GetEntity<T>(LocalPluginContext context) where T : Entity
9494
{
95-
var context = localPluginContext.PluginExecutionContext;
96-
var trace = localPluginContext.TracingService;
97-
98-
return GetEntity<T>(context, trace);
99-
}
100-
101-
protected static T GetEntity<T>(IPluginExecutionContext context, ITracingService trace) where T : Entity
102-
{
103-
var logicalName = (Activator.CreateInstance<T>()).LogicalName;
104-
105-
if (!context.InputParameters.Contains("Target"))
106-
{
107-
trace.Trace("Context does not contain 'Target'");
108-
return null;
109-
}
110-
111-
var target = context.InputParameters["Target"];
112-
113-
if (target is Entity entity)
114-
{
115-
if (logicalName != entity.LogicalName)
116-
{
117-
trace.Trace("'Entity' is not of specified type: {0} vs. {1}",
118-
entity.LogicalName, logicalName);
119-
return null;
120-
}
121-
122-
return entity.ToEntity<T>();
123-
}
124-
125-
var typeName = target.GetType().Name;
126-
trace.Trace("'Target' is not an Entity. It's of type: {0}", typeName);
127-
return null;
95+
return context.PluginExecutionContext.GetEntity<T>(context.TracingService);
12896
}
12997

13098
protected static T GetImage<T>(LocalPluginContext context, ImageType imageType, string name) where T : Entity
13199
{
132-
EntityImageCollection collection = null;
133-
if (imageType == ImageType.PreImage)
134-
{
135-
collection = context.PluginExecutionContext.PreEntityImages;
136-
}
137-
else if (imageType == ImageType.PostImage)
138-
{
139-
collection = context.PluginExecutionContext.PostEntityImages;
140-
}
141-
142-
if (collection != null && collection.TryGetValue(name, out var entity))
143-
{
144-
return entity.ToEntity<T>();
145-
}
146-
else
147-
{
148-
return null;
149-
}
100+
return context.PluginExecutionContext.GetImage<T>(imageType, name);
150101
}
151102

152103
protected static T GetImage<T>(LocalPluginContext context, ImageType imageType) where T : Entity

XrmPluginCore.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Files", "Solution
1515
EndProject
1616
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Scripts", "Scripts", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}"
1717
ProjectSection(SolutionItems) = preProject
18+
scripts\Pack-Local.ps1 = scripts\Pack-Local.ps1
1819
scripts\Set-VersionFromChangelog.ps1 = scripts\Set-VersionFromChangelog.ps1
1920
EndProjectSection
2021
EndProject

scripts/Pack-Local.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
./scripts/Set-VersionFromChangelog.ps1 -ChangelogPath .\DG.XrmPluginCore\CHANGELOG.md -CsprojPath .\DG.XrmPluginCore\DG.XrmPluginCore.csproj
2+
./scripts/Set-VersionFromChangelog.ps1 -ChangelogPath .\DG.XrmPluginCore.Abstractions\CHANGELOG.md -CsprojPath .\DG.XrmPluginCore.Abstractions\DG.XrmPluginCore.Abstractions.csproj
3+
4+
dotnet build --configuration Release
5+
dotnet pack

0 commit comments

Comments
 (0)