【发布时间】:2020-06-05 23:37:00
【问题描述】:
我知道如何创建虚拟方法并覆盖它们。示例
基类:
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 = .....
根据 cmets...
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);
}
【问题讨论】:
-
您的最后一个问题与其余的解释完全脱节。你想达到什么目的?
-
@trailmax 抱歉 - ICN = GetICN 在 var 消息内......例如,我想用 FileName 替换 ICN 的 json 结构......以及其他字段。我只是迷失了如何替换 var 消息 - JObject 等......通过覆盖
-
真的不确定您在这里要问什么,所以这是一个最好的猜测 - 您是否考虑过从您用自定义逻辑覆盖的另一种方法返回
message?你不能为了改变状态而在函数中途中断代码流 - 不能没有来自在派生类型上实现的虚拟或抽象方法的状态。 -
@ColinM - 您是否考虑过查看粘贴的基类和子类并提供答案而不是添加到 cmets?
-
不知何故,我认为您应该将消息的发送和不同类的格式分开。也许创建一个 IMessageFormatter 接口和不同的实现,它们会生成不同的 JSON 消息。 MessageSender 可以使用 IMessageFormatter...
标签: c# oop abstract-base-class