Skip to content

Commit d7255e8

Browse files
committed
CHORE: Update README.md
1 parent 5a5514e commit d7255e8

File tree

1 file changed

+78
-24
lines changed

1 file changed

+78
-24
lines changed

README.md

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# XrmPluginCore
1+
# XrmPluginCore
22
![XrmPluginCore NuGet Version](https://img.shields.io/nuget/v/XrmPluginCore?label=XrmPluginCore%20NuGet) ![XrmPluginCore.Abstractions NuGet Version](https://img.shields.io/nuget/v/XrmPluginCore.Abstractions?label=Abstractions%20NuGet)
33

44
XrmPluginCore provides base functionality for developing plugins and custom APIs in Dynamics 365. It includes context wrappers and registration utilities to streamline the development process.
@@ -14,49 +14,82 @@ XrmPluginCore provides base functionality for developing plugins and custom APIs
1414
### Creating a Plugin
1515

1616
1. Create a new class that inherits from `Plugin`.
17-
2. Register the plugin using the `RegisterPlugin` helper methods.
17+
2. Register the plugin using the `RegisterStep` helper method.
1818
3. Implement the function in the custom action
1919

20-
#### Using the IServiceProvider wrapper
20+
#### Using the a service
21+
22+
Create a service interface and concrete implementation:
2123

2224
```csharp
2325
namespace Some.Namespace {
24-
using System;
25-
using XrmFramework.BusinessDomain.ServiceContext;
26-
using XrmPluginCore;
27-
using XrmPluginCore.Enums;
28-
29-
public class AccountChainPostPlugin : Plugin {
30-
31-
public AccountChainPostPlugin() {
32-
RegisterStep<Account>(
33-
EventOperation.Update,
34-
ExecutionStage.PostOperation,
35-
ExecutePlugin)
36-
.AddFilteredAttributes(x => x.Fax);
26+
interface IMyService {
27+
void DoSomething();
28+
}
3729

38-
}
30+
public class MyService : IMyService {
31+
private readonly IOrganizationService _service;
32+
private readonly IPluginExecutionContext _context;
3933

40-
protected void ExecutePlugin(IServiceProvider serviceProvider) {
41-
var serviceFactory = serviceProvider.GetService<IOrganizationServiceFactory>();
42-
var executionContext = serviceProvider.GetService<IPluginExecutionContext>();
43-
var service = serviceFactory.CreateOrganizationService(executionContext.UserId);
34+
public MyService(IOrganizationServiceFactory serviceFactory, IPluginExecutionContext pluginExecutionContext) {
35+
// Store references to the services if needed
36+
_service = _serviceFactory.CreateOrganizationService(pluginExecutionContext.UserId);
37+
_context = pluginExecutionContext;
38+
});
4439

40+
public void DoSomething() {
41+
// Implementation here
4542
var rand = new Random();
4643

47-
var newAcc = new Account(localContext.PluginExecutionContext.PrimaryEntityId) {
44+
var newAcc = new Account(_context.PrimaryEntityId) {
4845
Fax = rand.Next().ToString()
4946
};
50-
service.Update(newAcc);
47+
48+
_service.Update(newAcc);
49+
}
50+
}
51+
}
52+
```
53+
54+
Create a base-plugin class that registers the service. Only do this once per assembly, and register all your custom services here:
55+
```csharp
56+
using XrmPluginCore;
57+
58+
namespace Some.Namespace {
59+
public class BasePlugin : Plugin {
60+
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
61+
{
62+
return services.AddScoped<IMyService, MyService>();
5163
}
5264
}
5365
}
5466
```
5567

56-
**NOTE** It is recommended to use dependency injection based plugins instead of blindly using the IServiceProvider. See the example in [XrmBedrock](https://github.com/delegateas/XrmBedrock), or the sample plugin in the test project for more information.
68+
Finally, create your plugin class that inherits from the base-plugin, and use dependency injection to get your service:
69+
70+
```csharp
71+
using System;
72+
using XrmFramework.BusinessDomain.ServiceContext;
73+
using XrmPluginCore;
74+
using XrmPluginCore.Enums;
75+
76+
namespace Some.Namespace {
77+
public class AccountChainPostPlugin : BasePlugin {
78+
public AccountChainPostPlugin() {
79+
RegisterStep<Account, IMyService>(
80+
EventOperation.Update,
81+
ExecutionStage.PostOperation,
82+
s => s.DoSomething())
83+
.AddFilteredAttributes(x => x.Fax);
84+
}
85+
}
86+
}
87+
```
5788

5889
#### Using the LocalPluginContext wrapper
5990

91+
**NOTE**: This is only support to support legacy DAXIF/XrmFramework style plugins. It is recommended to use dependency injection based plugins instead.
92+
6093
```csharp
6194
namespace Some.Namespace {
6295
using System;
@@ -93,6 +126,27 @@ namespace Some.Namespace {
93126
}
94127
```
95128

129+
### Injected Services
130+
The following services are available for injection into your plugin or custom API classes:
131+
132+
| Service | Description |
133+
|---------|-------------|
134+
| [IExtendedTracingService](XrmPluginCore/IExtendedTracingService.cs) | Extension to ITracingService with additional helper methods. |
135+
| [ILogger 🔗](https://learn.microsoft.com/en-us/power-apps/developer/data-platform/application-insights-ilogger) | The Plugin Telemetry Service logger interface. |
136+
| [IOrganizationServiceFactory 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.iorganizationservicefactory) | Represents a factory for creating IOrganizationService instances. |
137+
| [IPluginExecutionContext 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext) | The plugin execution context provides information about the current plugin execution, including input and output parameters, the message name, and the stage of execution. |
138+
| [IPluginExecutionContext2 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext2) | Extension to IPluginExecutionContext with additional properties and methods. |
139+
| [IPluginExecutionContext3 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext3) | Extension to IPluginExecutionContext2 with additional properties and methods. |
140+
| [IPluginExecutionContext4 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext4) | Extension to IPluginExecutionContext3 with additional properties and methods. |
141+
| [IPluginExecutionContext5 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext5) | Extension to IPluginExecutionContext4 with additional properties and methods. |
142+
| [IPluginExecutionContext6 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext6) | Extension to IPluginExecutionContext5 with additional properties and methods. |
143+
| [IPluginExecutionContext7 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.ipluginexecutioncontext7) | Extension to IPluginExecutionContext6 with additional properties and methods. |
144+
| [ITracingService 🔗](https://learn.microsoft.com/en-us/dotnet/api/microsoft.xrm.sdk.itracingservice) | The tracing service interface. The actual class is the [ExtendedTracingService](https://github.com/delegateas/XrmPluginCore/blob/main/XrmPluginCore/ExtendedTracingService.cs) wrapping the built-in `ITracingService` |
145+
146+
*Note:* Links marked with 🔗 point to official Microsoft documentation.
147+
148+
To register additional services, override the `OnBeforeBuildServiceProvider` method in your plugin or custom API class.
149+
96150
### Registering a Plugin
97151

98152
Use [XrmSync](https://github.com/delegateas/XrmSync) to automatically register relevant plugin steps and images.

0 commit comments

Comments
 (0)