Skip to content

Instantly share code, notes, and snippets.

public class OpportunityRepository : IOpportunityRepository
{
protected IOrganizationService orgService;
public OpportunityRepository(IOrganizationService service)
{
this.orgService = service;
}
public Opportunity GetById(Guid id)
public class OpportunityRepository : IOpportunityRepository
{
protected Dyn365ServiceContext serviceContext;
public OpportunityRepository(IOrganizationService service)
{
this.serviceContext = new Dyn365ServiceContext(service);
}
public Opportunity GetById(Guid id)
public interface IOpportunityRepository
{
Opportunity GetById(Guid id);
Guid Create(Opportunity opportunity);
void SaveChanges();
void Dispose();
}
public class FooPlugin : PluginBase
{
public FooPlugin(string unsecureString, string secureString) : base(unsecureString, secureString)
{
}
public override bool IsContextValid(IPluginExecutionContext context)
{
//TODO: Add your validation code here…
}
try
{
//Validate input and run business logic here…
}
catch (InvalidPluginExecutionException e)
{
throw;
}
catch (CustomIntegrationException e)
{
var targetAccount = this.GetTargetEntity<Account>(pluginExecutionContext);
protected T GetTargetEntity<T>(IPluginExecutionContext pluginExecutionContext) where T: Entity
{
if (pluginExecutionContext.InputParameters.Contains(TargetAttributeName) && pluginExecutionContext.InputParameters[TargetAttributeName] is Entity)
{
return ((Entity)pluginExecutionContext.InputParameters[TargetAttributeName])?.ToEntity<T>();
}
else
{
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Missing target value or target is not an Entity");
}
if (this.IsContextValid(pluginExecutionContext))
{
this.Execute(pluginExecutionContext, tracingService);
}
else
{
tracingService.Trace($"Invalid plugin execution context detected (Plugin: {this.Name})");
tracingService.Trace($"Execution context: [{pluginExecutionContext.ToFormattedString()}]");
tracingService.Trace("Plugin execution aborted");
throw new InvalidPluginExecutionException("Invalid plugin execution context detected");
public void Execute(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (tracingService == null)
{
[TestFixture]
public class TestProgram
{
private string expectedText = "Expected text";
private string initialText = "Initial text";
[Test]
public void TestChangeValue()
{
var mySample = new SampleClass(initialText);