gpt4 book ai didi

c# - 插件和工作流的抽象类

转载 作者:太空狗 更新时间:2023-10-30 00:49:29 24 4
gpt4 key购买 nike

我创建了以下两个在我的插件和工作流程中使用的抽象类:

/// <summary>
/// Base plugin class. Provides access to most often used Xrm resources.
/// </summary>
public abstract class BasePlugin : IPlugin
{

public IServiceProvider ServiceProvider { get; set; }
public ITracingService TracingService { get; set; }
public IPluginExecutionContext PluginContext { get; set; }
public IOrganizationService Service { get; set; }

public void Execute(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
TracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

PluginContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
Service = serviceFactory.CreateOrganizationService(PluginContext.UserId);

ExecutePluginLogic();
}

public virtual void ExecutePluginLogic()
{
throw new NotImplementedException();
}
}

/// <summary>
/// Base workflow class. Provides access to most often used Xrm resources.
/// </summary>
public abstract class BaseWorkflow : CodeActivity
{
public CodeActivityContext CodeActivityContext { get; set; }
public IWorkflowContext WorkflowContext { get; set; }
public ITracingService TracingService { get; set; }
public IOrganizationService Service { get; set; }

protected override void Execute(CodeActivityContext context)
{
IOrganizationServiceFactory serviceFactory = context.GetExtension<IOrganizationServiceFactory>();
CodeActivityContext = context;
TracingService = context.GetExtension<ITracingService>();
WorkflowContext = context.GetExtension<IWorkflowContext>();
Service = serviceFactory.CreateOrganizationService(WorkflowContext.UserId);

ExecuteWorkflowLogic();
}

public virtual void ExecuteWorkflowLogic()
{
throw new NotImplementedException();
}
}

下面是我将如何创建一个插件:

public class CalculateTaxesOnUpdate : BasePlugin
{

public override void ExecutePluginLogic()
{
//From there I don't need to instanciate anything...neat!
}
}

这似乎工作正常,并有助于在启动 IOrganizationService 实例时减少样板代码。和 ITracingService即。

但我注意到,在某些消息(即 Updateinvoicedetail )上,在第一次执行时会短暂延迟触发, BasePlugin 的公共(public)属性是null (这是预期的)然后在接下来的执行中,它们已经启动(??)。我注意到这是一个问题,因为我有一个 Dispose基类中的方法,在执行 ExecutePluginLogic 后将属性设置为 null然后其他线程将尝试使用 null特性。

因为我没有重用它们并重新启动它们(不管怎样,当你在 Execute 中实例化所有东西时会发生这种情况),我不知道这是否是一个问题,但我是否违背了这里的最佳实践?

最佳答案

仅仅因为它是一个基类并不能消除 CRM 插件(和工作流)中类级别变量的问题。

来自 https://msdn.microsoft.com/en-us/library/gg328263.aspx#bkmk_writingbasic :

For improved performance, Microsoft Dynamics CRM caches plug-in instances. The plug-in's Execute method should be written to be stateless because the constructor is not called for every invocation of the plug-in. Also, multiple system threads could execute the plug-in at the same time. All per invocation state information is stored in the context, so you should not use global variables or attempt to store any data in member variables for use during the next plug-in invocation unless that data was obtained from the configuration parameter provided to the constructor. Changes to a plug-ins registration will cause the plug-in to be re-initialized.

拥有类级变量违反了这种无状态要求。

我的建议是重写插件(然后对工作流做同样的事情)以拥有一个对象来保存每次调用 Execute 的引用,从而使代码满足无状态要求.

public class CrmObjects
{
public IServiceProvider ServiceProvider { get; set; }
public ITracingService TracingService { get; set; }
public IPluginExecutionContext PluginContext { get; set; }
public IOrganizationService Service { get; set; }
}

public abstract class BasePlugin : IPlugin
{

public void Execute(IServiceProvider serviceProvider)
{

var crmObjects = new CrmObjects();

crmObjects.ServiceProvider = serviceProvider;
crmObjects.TracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

crmObjects.PluginContext = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
crmObjects.Service = serviceFactory.CreateOrganizationService(crmObjects.PluginContext.UserId);

ExecutePluginLogic(crmObjects);
}

public virtual void ExecutePluginLogic(CrmObjects crmObjects)
{
throw new NotImplementedException();
}
}

我写了一篇关于做类似事情的博客文章,http://nicknow.net/dynamics-crm-2011-abstracting-plugin-setup/ ,几年前。在我描述的模型中,它不依赖于基类,而是使用在 Execute 方法的第一行实例化的类来完成相同的概念。从那以后,我转向了基类模型——类似于这个设计。如果有机会,我会把它发布到 GitHub 上。

关于c# - 插件和工作流的抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37792030/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com