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