C# 当变量/对象涉及特定方法时重写

C# Override when there are specific methods involved in a variable / object

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

基类:

var service = GetConnectionKeyString();

protected virtual string GetConnectionKeyString()
{
    return "wpshook";
}

子类:

protected override string GetConnectionKeyString()
{
   return "newthing";
} 

但是,我想更改一条消息,其中包含带有方法调用的 JObject 和其他对象

当前在抽象基class方法中是这段代码

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
 }

所以在我的 subclass 中,有一个覆盖 - 我怎样才能替换 "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),

        });

更新具体示例:

基地class:

 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 "";
    }

然后是一个典型的subclass:

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 中的几个匿名类型和方法。我正在玩一个新的 subclass 并创建这个

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

如果我对您的要求理解正确,您正在寻找的是更改生成的 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() 方法并提供一个完全不同的消息结构,如果这是要求的话