HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

3 constructors that accept 3 different types of arguments

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
constructorsargumentsacceptdifferentthattypes

Problem

As I understand, the constructor chaining can be applied to cases when we have some common data to initialize. I have 3 constructors that accept 3 different types of arguments and have some common data. I'm not sure if I can do constructor chaining.

```
public class RStandart
{
private readonly SqlConnection _objConn;
private readonly RequestData _requestData = new RequestData();

public string CardNo { get; set; }
public string PaymentType { get; set; }
public double Total { get; set; }
public DateTime TransactionDate { get; set; }
public string TransactionId { get; set; }

public class RequestData
{
// Request data (just a properties}
}

public RequestData GetRequestData
{
get { return _requestData; }
}

public RStandart(Int64 orderID, string total)
{
_requestData.VpcVirtualPaymentClientURL = string.Format("https://{0}/vpcpay", ConfigurationManager.AppSettings["vpc_VirtualPaymentClientURL"]);
_requestData.VpcVersion = ConfigurationManager.AppSettings["vpc_Version"];
_requestData.VpcCommand = ConfigurationManager.AppSettings["vpc_Command"];
_requestData.VpcAccessCode = ConfigurationManager.AppSettings["vpc_AccessCode"];
_requestData.VpcOrderInfo = orderID;
_requestData.VpcMerchant = ConfigurationManager.AppSettings["vpc_Merchant"];
_requestData.VpcAmount = int.Parse(total.Replace(".", string.Empty).Replace(",", string.Empty));
_requestData.VpcLocale = ConfigurationManager.AppSettings["vpc_Locale"];
_requestData.VpcReturnURL = ConfigurationManager.AppSettings["vpc_ReturnURL"];
_requestData.RequestParams = string.Format(
"vpc_Version-{0}|vpc_Command-{1}|vpc_AccessCode-{2}|vpc_MerchTxnRef-{3}|vpc_Merchant-{4}|vpc_Amount-{5}|vpc_Locale-{6}|vpc_ReturnURL-{7}|vpc_OrderInfo-{8}",
_requestData.VpcVersion, _requestData.VpcCommand, _requestData.VpcAccessCode, _requestData.VpcMerchTxnRef, _requestD

Solution

You can use constructor chaining on the first two constructors but the last one differs a lot.

What i think is more important is that you give the RStandart class a lot of knowledge and responsibility to setup the RequestData class. Are you sure this class needs to know how to initialize a RequestData class?

I would move all the code that depends on the ConfigurationManager to the RequestData class and it in turn would make your constructors simpler and you would remove that duplicate code.

Context

StackExchange Code Review Q#46701, answer score: 3

Revisions (0)

No revisions yet.