我有一个 json body ,它有相似的参数和一个在 3 种不同情况下不同的参数,如何避免创建三个不同的主体
I have a json body which has similar parameters and one different in 3 different situations , how to avoid creating three different bodies
我有一个 json body 在 3 种不同的情况下有 1 个不同的 属性
我尝试了一些自定义方法,但 none 有效。这是我的json身体
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 是在不同方面会有所不同的东西,有时它是 depositId 有时是 utilityId 和 loanId 。我该怎么做才能改变这个价值而不是创造 3 个不同的身体?
我的 Class 是
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; }
}
实用程序必须是这样的:
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
};
那么帐户必须是
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
};
有几个解决方案。最简单的方法是创建所有三个属性并分配适当的一个:
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"
};
但这将默认序列化所有三个属性,其中两个具有 null
值,并允许开发人员意外分配 none 或多个,这可能是一个错误.
或者,您可以为每个请求创建一个 class,继承自基本请求:
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"
};
这可以防止对空属性进行无用的序列化(这可以通过多种方式避免,例如参见 [=13=]),但更重要的是,强制开发人员显式实例化他们想要发出的请求。不允许犯错。
我有一个 json body 在 3 种不同的情况下有 1 个不同的 属性
我尝试了一些自定义方法,但 none 有效。这是我的json身体
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 是在不同方面会有所不同的东西,有时它是 depositId 有时是 utilityId 和 loanId 。我该怎么做才能改变这个价值而不是创造 3 个不同的身体? 我的 Class 是
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; }
}
实用程序必须是这样的:
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
};
那么帐户必须是
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
};
有几个解决方案。最简单的方法是创建所有三个属性并分配适当的一个:
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"
};
但这将默认序列化所有三个属性,其中两个具有 null
值,并允许开发人员意外分配 none 或多个,这可能是一个错误.
或者,您可以为每个请求创建一个 class,继承自基本请求:
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"
};
这可以防止对空属性进行无用的序列化(这可以通过多种方式避免,例如参见 [=13=]),但更重要的是,强制开发人员显式实例化他们想要发出的请求。不允许犯错。