0

I have a json body with 1 different property in 3 different situations

I tried some custom methods but none of them worked. This is my jsonBody

var postParameters = new CredoPayRequest()
        {
            amount = amount,
            birthDate = Dob,
            currency = "GEL",
            currencyRate = 1,
            depositId = credoPayId,
            fee = 0,
            paymentDate = trx_date,
            personalNumber = personalNumber,
            terminalId = terminalId.ToString(),
            transactionId = invoiceID
        };

depositId is the thing which will be different in different ways sometimes it will be depositId sometimes utilityId and loanId . what can i do to just change that value and not create 3 different bodies ? My Class is

public class CredoPayRequest
{
    public string personalNumber { get; set; }
    public string transactionId { get; set; }
    public string terminalId { get; set; }
    public string paymentDate { get; set; }
    public string birthDate { get; set; }
    public string amount { get; set; }
    public int fee { get; set; }
    public string currency { get; set; }
    public int currencyRate { get; set; }
    public string accountId { get; set; }
    public string depositId { get; set; }
    public string utilityId { get; set; }

}

Utility must be like this:

var postParameters = new CredoPayRequest()
    {
        amount = amount,
        birthDate = Dob,
        currency = "GEL",
        currencyRate = 1,
        utilityId = credoPayId,
        fee = 0,
        paymentDate = trx_date,
        personalNumber = personalNumber,
        terminalId = terminalId.ToString(),
        transactionId = invoiceID
    };

then accounts must be

var postParameters = new CredoPayRequest()
    {
        amount = amount,
        birthDate = Dob,
        currency = "GEL",
        currencyRate = 1,
        accountId = credoPayId,
        fee = 0,
        paymentDate = trx_date,
        personalNumber = personalNumber,
        terminalId = terminalId.ToString(),
        transactionId = invoiceID
    };
LTsetskhla
  • 33
  • 7
  • *"it will be `depositId` sometimes `utilityId` and `loanId`"* - what types are those? Can you show DTOs and jsons for all cases? – Sinatr Apr 11 '19 at 11:28
  • Is there an issue of sending parameters and just setting the values to a default value? – jdweng Apr 11 '19 at 11:30
  • yes . in one particular situation i must have all parameters but loanId in another all parameters and utilityId instead of loanId . i cant send more or less parameters. – LTsetskhla Apr 11 '19 at 11:35
  • i edited my question u can see all cases bodies. – LTsetskhla Apr 11 '19 at 11:36

1 Answers1

1

There are a couple of solutions. The simplest one would be to create all three properties and assign the appropriate one:

public class CredoPayRequest
{
    // properties shared between all requests
    // ...
    public string depositId { get; set; }
    public string utilityId { get; set; }
    public string loanId { get; set; }
}

var request = new CredoPayRequest
{
    // assign shared properties
    // ...
    utilityId = "Foo"
};

But this will by default serialize all three properties, two of them having a null value, and allow the developer to accidentally assign none, or more than one, which might be an error.

Alternatively, you could create a class per request, inheriting from the base request:

public abstract class CredoPayRequest
{
    // properties shared between all requests
}

public class DepositRequest : CredoPayRequest
{   
    public string depositId { get; set; }
}

public class UtilityRequest : CredoPayRequest
{   
    public string utilityId { get; set; }
}

public class LoanRequest : CredoPayRequest
{   
    public string loanId { get; set; }
}

var request = new DepositRequest
{
    // assign shared properties
    // ...
    depositId = "Foo"
};

This prevents the useless serialization of empty properties (which could be avoided in various ways, see for example NewtonSoft add JSONIGNORE at runTime), but more importantly, forces the developer to explicitly instantiate the request they want to issue. No room for mistakes.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • as u see my body depositId must be changed in utilityId if there aare utilities and not deposits. so basic thing i want to do is make if statemant or something like that only on that one particular property not on the whole body. – LTsetskhla Apr 11 '19 at 11:32
  • Which is addressed in the second part of my answer. – CodeCaster Apr 11 '19 at 11:34
  • yes but still i have to created 3 bodies no ? var request = new DepositRequest {this one then var request = new UtilityRequest and so on. – LTsetskhla Apr 11 '19 at 11:37
  • look at my edited question i have 3 bodies now but i want to have only one . for simple code view. – LTsetskhla Apr 11 '19 at 11:38
  • 1
    "Alternatively, you could create a class per request, inheriting from the base request...but more importantly, forces the developer to explicitly instantiate the request they want to issue. No room for mistakes." In other words : sticking with Liskov's substitution principle now, you're saving yourself a pain in the butt later. Saved me from falling into common pitfalls countless times. – Thomas Luijken Apr 11 '19 at 11:43
  • i have to create var request = new DepositRequest { // assign shared properties // ... depositId = "Foo" }; three times for 3 different situations . this doenst help me – LTsetskhla Apr 11 '19 at 11:49
  • @LTsetskhla then you create a method which eats a `CredoPayRequest` to assign all shared properties, and put your newly instantiated request through that method. – CodeCaster Apr 11 '19 at 12:00
  • 1
    It's undesirable have three properties, each of which is used only in a particular context. What happens is that you have an instance of the class and unless you know who sent it and why you don't know which are populated and which is null. The person creating an instance might also be confused, not knowing what happens if they don't populate certain properties. If you have three separate classes (which could include using inheritance) that problem goes away. The type of the dto tells you what it is and it doesn't have extra properties. – Scott Hannen Apr 11 '19 at 17:03