我知道如何创建虚拟方法并覆盖它们。例子
基类:
var service = GetConnectionKeyString();
protected virtual string GetConnectionKeyString()
{
return "wpshook";
}
子类:
protected override string GetConnectionKeyString()
{
return "newthing";
}
但是,我想更改一条消息,其中包含带有方法调用和其他对象的 JObject
目前在抽象基类方法中就是这个代码
var message = JObject.FromObject(new
{
Component = GetComponentName(),
WorkflowName = workspaceContext.WorkItem.Workflow.TransactionName,
RecordType = GetRecordType(configuration, workspaceContext),
RecordId = GetRecordId(configuration, workspaceContext),
RecordStatus = GetRecordStatus(configuration, workspaceContext),
ICN = GetICN(configuration, workspaceContext),
});
所以我添加了一个虚拟方法来覆盖这个消息
protected virtual string OverRideNotificationMessage()
{
return ""; //open/close principle - don't effect the other subclasses
}
所以在我的子类中,有一个覆盖 - 我如何能够替换“var message”中近一半的代码?
例子
//replace
ICN = GetICN(configuration, workspaceContext)
//with
FileName = .....
根据评论...
var message = JObject.FromObject(new
{
Component = GetComponentName(),
WorkflowName = workspaceContext.WorkItem.Workflow.TransactionName,
RecordType = GetRecordType(configuration, workspaceContext),
RecordId = GetRecordId(configuration, workspaceContext),
RecordStatus = GetRecordStatus(configuration, workspaceContext),
//per comment replace ICN with FileName, and possibly replace other lines
FileName = GetFileName(configuration, workspaceContext),
});
更新具体示例:
基类:
public abstract class BaseStatusNotification<TContext> : IWorkflowRule<TContext> where TContext : IWorkflowContext
{
public StepResult Process(IWorkflowClient workflowClient, ITransactionStep configuration, TContext workspaceContext)
{
var message = JObject.FromObject(new
{
Component = GetComponentName(),
WorkflowName = workspaceContext.WorkItem.Workflow.TransactionName,
RecordType = GetRecordType(configuration, workspaceContext),
RecordId = GetRecordId(configuration, workspaceContext),
RecordStatus = GetRecordStatus(configuration, workspaceContext),
ICN = GetICN(configuration, workspaceContext),
});
// this most likely should not be here, but only in a subclass
// wondering if I should have if statement is override is not null?
OverRideNotificationMessage(configuration, workspaceContext, message);
var serviceBusClient = QueueManagerFactory.GetQueueSender(workspaceContext.InstanceId,
workspaceContext.WorkItem.Workflow.Component,
GetConnectionKeyString(), null);
if (serviceBusClient == null)
{
return StepResult.Error;
}
serviceBusClient.Enqueue(TimeSpan.Zero, message);
return StepResult.Success;
}
protected virtual string GetConnectionKeyString()
{
return "wpshook";
}
protected virtual string OverRideNotificationMessage(ITransactionStep configuration, TContext workspaceContext, JObject message)
{
return "";
}
然后是一个典型的子类:
public class SendClaimStatusNotification : BaseStatusNotification<IBizClaimWorkflowContext>
{
protected override string GetICN(ITransactionStep configuration, IBizClaimWorkflowContext workspaceContext)
{
var claimHeader = workspaceContext.GetClaimHeader();
return claimHeader.AdditionalClaimId;
}
protected override string GetRecordStatus(ITransactionStep configuration, IBizClaimWorkflowContext workspaceContext)
{
var claimHeader = workspaceContext.GetClaimHeader();
return claimHeader.StatusCode;
}
protected override string GetRecordId(ITransactionStep configuration, IBizClaimWorkflowContext workspaceContext)
{
var claimHeader = workspaceContext.GetClaimHeader();
return claimHeader.ClaimId;
}
protected override string GetRecordType(ITransactionStep configuration, IBizClaimWorkflowContext workspaceContext)
{
return "Claim";
}
protected override string GetComponentName()
{
return FrameworkConstants.Components.VidaClaim;
}
protected override string GetConnectionKeyString()
{
return "wpshook";
}
}
现在,我想要一个覆盖来换出 var 消息....
我不清楚如何替换产生 json 的 JObject 中的几个匿名类型和方法。我正在玩一个新的子类并创建它
protected override string OverRideNotificationMessage(ITransactionStep configuration, ITEDTransactionWorkflowContext workspaceContext, JObject message)
{
var messageq = JObject.FromObject(new
{
Component = GetComponentName(),
WorkflowName = workspaceContext.WorkItem.Workflow.TransactionName,
RecordType = GetRecordType(configuration, workspaceContext),
RecordId = GetRecordId(configuration, workspaceContext),
RecordStatus = GetRecordStatus(configuration, workspaceContext),
//ICN = GetICN(configuration, workspaceContext),
FileName = "something else"
});
return base.OverRideNotificationMessage(configuration, workspaceContext, message);
}