user9054710
user9054710

Reputation:

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

I know how to create virtual methods and override them. Example

BaseClass:

var service = GetConnectionKeyString();

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

SubClass:

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

However, I want to change a message in which it has JObject with method calls and other objects

Currently in the abstract base class method is this code

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),

        });

So I was adding a virtual method to override this message

 protected virtual string OverRideNotificationMessage()
 {
    return "";   //open/close principle  -  don't effect the other subclasses
 }

So in my subclass, with an override - how would I be able to replace nearly half the code in "var message" ?

Example

    //replace  
    ICN = GetICN(configuration, workspaceContext) 
    //with 
    FileName = .....  

Per the comments...

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),

        });

UPDATE with specific example:

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

Then a typical 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";
    }
}

NOW, I am wanting to have an override to swap out the var message....

It isn't clear to me how I can replace the several of those anonymous types and methods in the JObject that produces json .. I was playing with a new subclass and creating this

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

Upvotes: 0

Views: 257

Answers (1)

BlueStrat
BlueStrat

Reputation: 2304

If I understood correctly your requirement, what you are looking for is changing the structure of the generated json object, depending on the subclass that is creating the json message.

Does this help?

// 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();
    ...
}

Then a subclass could override NewMesage() and provide a different structrure

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

Here you have a lot of flexibility, since you can override any of the Get... methods if you want to tailor how a specific member gets its value, or you can override the NewMessage() method and provide a whole different message structure if that is the requirement

Upvotes: 1

Related Questions