Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having 4 similar models 2 requests, 2 response, and having trouble with duplicate codes, how to refactor models for clean code?


//The first Request
    public class RequestModel
    {
        public Participant Sender { get; set; }
        public Participant Receiver { get; set; }
        public Transaction Transaction { get; set; }
        public Security Security { get; set; }
    }
    public class Participant
    {
        public Institute Institute { get; set; }
        public Client Client { get; set; }
        public Account Account { get; set; }
        public Device Device { get; set; }
    }
    public class Institute
    {
        public long ID { get; set; }
        public string Type { get; set; }
    }
    public class Client
    {
        public long ID { get; set; }
        public string Type { get; set; }
    }
    public class Account
    {
        public long ID { get; set; }
        public string Type { get; set; }
    }
    public class Device
    {
        public long ID { get; set; }
        public string Type { get; set; }
        public string AcceptorDeviceAddress { get; set; }
    }
    public class Transaction
    {
        public string TRNType { get; set; }
        public int Amount { get; set; }
        public string Currency { get; set; }
    }

    public class Security
    {
        public string SecurityKey { get; set; }
    }


The second Request is the same as the first only difference is in this class

public class Transaction
{
    public string TRNType { get; set; }
    public int Amount { get; set; }
    public string Currency { get; set; }
    // new property
    public string OTP{ get; set; }
}


The Responses are the same as the first RequestModel class only difference is

public class Transaction
{
    public string TRNType { get; set; }
    public int Amount { get; set; }
    public string Currency { get; set; }
    // new property
    public string ResponseCode{ get; set; }
}


What I have tried:

Tried nesting classes, inheritance, abstraction but didn't succeed with normal result.
Posted
Updated 22-Jan-21 9:13am
v2

1 solution

You could have just added the new "string" field to the original Transaction; content depending on the "TRNType" (or not).

It's not like an optional / "empty" string is going to consume a lot of memory / disk space.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900