/
BasePlugin.cs
169 lines (142 loc) · 5.56 KB
/
BasePlugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Odx.Xrm.Core.DataAccess;
namespace Odx.Xrm.Core
{
public abstract class BasePlugin : IPlugin
{
private string unsecureConfig;
private string secureConfig;
private HashSet<string> availableMessages;
private bool disableRegisterCheck;
private enum PipelineStage
{
PreValidation = 10,
PreOperation = 20,
PostOperation = 40
}
protected string UnsecureConfig
{
get
{
return this.unsecureConfig;
}
}
protected string SecureConfig
{
get
{
return this.secureConfig;
}
}
/// <summary>
/// Register all messages that this plugin is registered on using fluent RegisterMessage method Example:
/// RegisterMessage<CreateRequest>().RegisterMessage<UpdateRequest>()
/// </summary>
protected abstract void RegisterAvailableMessages();
private BasePlugin RegisterMessage<TMessage>(PipelineStage stage)
where TMessage : OrganizationRequest, new()
{
var temp = new TMessage();
return this.RegisterMessage(stage, temp.RequestName);
}
public void DisableRegisterCheck()
{
this.disableRegisterCheck = true;
}
private BasePlugin RegisterMessage(PipelineStage stage, string messageName)
{
this.availableMessages.Add(stage + messageName);
return this;
}
public BasePlugin RegisterMessagePost(string messageName)
{
return this.RegisterMessage(PipelineStage.PostOperation, messageName);
}
public BasePlugin RegisterMessagePost<TMessage>()
where TMessage : OrganizationRequest, new()
{
return this.RegisterMessage<TMessage>(PipelineStage.PostOperation);
}
public BasePlugin RegisterMessagePre(string messageName)
{
return this.RegisterMessage(PipelineStage.PreOperation, messageName);
}
public BasePlugin RegisterMessagePre<TMessage>()
where TMessage : OrganizationRequest, new()
{
return this.RegisterMessage<TMessage>(PipelineStage.PreOperation);
}
public BasePlugin RegisterMessagePreValidation<TMessage>()
where TMessage : OrganizationRequest, new()
{
return this.RegisterMessage<TMessage>(PipelineStage.PreValidation);
}
public BasePlugin RegisterMessagePreValidation(string messageName)
{
return this.RegisterMessage(PipelineStage.PreValidation, messageName);
}
public BasePlugin(string unsecureConfig, string secureConfig)
{
this.availableMessages = new HashSet<string>();
this.unsecureConfig = unsecureConfig;
this.secureConfig = secureConfig;
}
public virtual void Execute(IServiceProvider serviceProvider)
{
this.RegisterAvailableMessages();
var context = this.GetPluginExecutionContext(serviceProvider);
if (!this.disableRegisterCheck)
{
if (!availableMessages.Contains((PipelineStage)context.Stage + context.MessageName))
{
throw new InvalidPluginExecutionException($"Plugin registered on bad message. Contact your system administrator");
}
}
}
protected IPluginExecutionContext GetPluginExecutionContext(IServiceProvider serviceProvider)
{
return (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
}
}
public abstract class BasePlugin<T> : BasePlugin, IPlugin
where T : HandlerBase, IHandler, new()
{
public BasePlugin(string unsecureConfig, string secureConfig) : base(unsecureConfig, secureConfig) { }
public override void Execute(IServiceProvider serviceProvider)
{
base.Execute(serviceProvider);
var localContext = this.GetLocalPluginContext(serviceProvider);
var repositoryFactory = this.GetRepositoryFactory(serviceProvider);
var tracingService = this.GetTracingService(serviceProvider);
var handler = new T();
handler.InitializeTracing(tracingService);
handler.InitializeConfiguration(this.UnsecureConfig, this.SecureConfig);
try
{
handler.Execute(localContext, repositoryFactory);
}
catch (Exception ex)
{
handler.Trace(ex);
handler.Trace($"Context: {localContext.Context.InputParameters.ToJSON()}");
throw;
}
}
private IRepositoryFactory GetRepositoryFactory(IServiceProvider serviceProvider)
{
var factory = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
return new RepositoryFactory(factory);
}
private ILocalPluginExecutionContext GetLocalPluginContext(IServiceProvider serviceProvider)
{
var context = this.GetPluginExecutionContext(serviceProvider);
return new LocalPluginExecutionContext(context);
}
private ITracingService GetTracingService(IServiceProvider serviceProvider)
{
return (ITracingService)serviceProvider.GetService(typeof(ITracingService));
}
}
}