0

我知道如何创建虚拟方法并覆盖它们。例子

基类:

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);
    }
4

1 回答 1

1

如果我正确理解了您的要求,那么您正在寻找的是更改生成的 json 对象的结构,具体取决于创建 json 消息的子类。

这有帮助吗?

// this is defined in a superclass
public virtual JObject NewMessage() {
   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),

        });
   return message;
}

public StepResult Process(IWorkflowClient workflowClient, ITransactionStep configuration, TContext workspaceContext)
{
    // instead of hardcoding the message structure, call an overridable function
    // so we may provide a different message structure if needed
    var message = NewMessage();
    ...
}

然后子类可以覆盖 NewMesage() 并提供不同的结构

public override JObject NewMessage() {
   var message = JObject.FromObject(new
     {
            Component = GetComponentName(),
            WorkflowName = workspaceContext.WorkItem.Workflow.TransactionName,
            RecordType = GetRecordType(configuration, workspaceContext),
            RecordId = GetRecordId(configuration, workspaceContext),
            RecordStatus = GetRecordStatus(configuration, workspaceContext),
            FileName = "something else"  // FileName instead of ICN

     });
   return message;
}

在这里您有很大的灵活性,因为Get...如果您想定制特定成员如何获取其值,您可以覆盖任何方法,或者如果需要,您可以覆盖该NewMessage()方法并提供完全不同的消息结构

于 2020-06-08T17:18:24.863 回答