Member_2_7967608
asked on
Refactor C# code
I want to refactor the below C# code. (Using .NET 4.5) Any suggestions or directions for refactoring the entire code
I am copying data from GetTransactionResponse "resp" to Request "req" object.
a>resp has some properties and some arrays coming from service call
b>req is class I want to convert ultimately to Json for communication
I am using 4.6
public Request ConvertToRequest(GetTransa ctionRespo nse resp)
{
Request req = new Request();
List<SalesOrder> salesOrders = new List<SalesOrder> { };
if (resp.transaction != null & resp.status.success)
{
foreach (AttributeType quoteAttr in resp.transaction.quote_dat a.attribut es)
{
if(!String.IsNullOrEmpty(q uoteAttr.v alue))
{
if(quoteAttr.field == "processing_quote")
{
req.regionCode = quoteAttr.value;
}
else if (quoteAttr.field == "salesOffice_quote")
{
req.regionalSalesOffice = quoteAttr.value;
}
else if(quoteAttr.field == "s_quote")
{
req.regionalSalesOffice = quoteAttr.value;
}
else if (quoteAttr.field == "_quote_company_name")
{
req.companyName = quoteAttr.value;
}
else if (quoteAttr.field == "_email")
{
req.contactEmailAddress = quoteAttr.value;
}
else if (quoteAttr.field == "preparedBy_quote")
{
req.dmName = quoteAttr.value;
}
}
}
salesOrders = new List<SalesOrder>();
List<ScnCode> scnListCode = new List<ScnCode>();
foreach (SubDocType subDoc in resp.transaction.quote_dat a.sub_docu ments)
{
SalesOrder salesOrder = new SalesOrder();
AttributeType prodAttrs = subDoc.attributes[0];
if (prodAttrs != null && !string.IsNullOrEmpty(prod Attrs.valu e))
{
string[] values = prodAttrs.value.Split(new string[] { "**" }, StringSplitOptions.None);
salesOrder.productCode = values[1];
salesOrder.salesOrderType = values[2];
salesOrder.startDate = values[3];
salesOrder.numberOfPays = (!string.IsNullOrEmpty(val ues[4])?in t.Parse(va lues[4]):0 );
foreach (string scn in values[5].Split(new string[] { "$$" }, StringSplitOptions.RemoveE mptyEntrie s))
{
scnListCode.Add(new acsModels.ScnCode { scnCode =scn });
}
salesOrder.scnCodes = scnListCode;
salesOrders.Add(salesOrder );
}
}
req.salesOrders = salesOrders;
}
return req;
}
I am copying data from GetTransactionResponse "resp" to Request "req" object.
a>resp has some properties and some arrays coming from service call
b>req is class I want to convert ultimately to Json for communication
I am using 4.6
public Request ConvertToRequest(GetTransa
{
Request req = new Request();
List<SalesOrder> salesOrders = new List<SalesOrder> { };
if (resp.transaction != null & resp.status.success)
{
foreach (AttributeType quoteAttr in resp.transaction.quote_dat
{
if(!String.IsNullOrEmpty(q
{
if(quoteAttr.field == "processing_quote")
{
req.regionCode = quoteAttr.value;
}
else if (quoteAttr.field == "salesOffice_quote")
{
req.regionalSalesOffice = quoteAttr.value;
}
else if(quoteAttr.field == "s_quote")
{
req.regionalSalesOffice = quoteAttr.value;
}
else if (quoteAttr.field == "_quote_company_name")
{
req.companyName = quoteAttr.value;
}
else if (quoteAttr.field == "_email")
{
req.contactEmailAddress = quoteAttr.value;
}
else if (quoteAttr.field == "preparedBy_quote")
{
req.dmName = quoteAttr.value;
}
}
}
salesOrders = new List<SalesOrder>();
List<ScnCode> scnListCode = new List<ScnCode>();
foreach (SubDocType subDoc in resp.transaction.quote_dat
{
SalesOrder salesOrder = new SalesOrder();
AttributeType prodAttrs = subDoc.attributes[0];
if (prodAttrs != null && !string.IsNullOrEmpty(prod
{
string[] values = prodAttrs.value.Split(new string[] { "**" }, StringSplitOptions.None);
salesOrder.productCode = values[1];
salesOrder.salesOrderType = values[2];
salesOrder.startDate = values[3];
salesOrder.numberOfPays = (!string.IsNullOrEmpty(val
foreach (string scn in values[5].Split(new string[] { "$$" }, StringSplitOptions.RemoveE
{
scnListCode.Add(new acsModels.ScnCode { scnCode =scn });
}
salesOrder.scnCodes = scnListCode;
salesOrders.Add(salesOrder
}
}
req.salesOrders = salesOrders;
}
return req;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
File Attached
There is no attachment. Try taking a screen shot or use the snipping tool to cut out a screenshot of the exception. You can try to copy and paste the exception.
-saige-
-saige-
ASKER
Attached
Testexception.png
Testexception.png
That error is perfectly normal (and expected). The exception itself is being set because the DeclaringMethod is not a generic (and it isn't); refer to - https://msdn.microsoft.com/en-us/library/system.type.declaringmethod(v=vs.110).aspx for more information.
-saige-
-saige-
ASKER
Thanks saige. Do you see any performance issue. This is encapsulated part of service .
But total # of Fields would be not more than 10.
But total # of Fields would be not more than 10.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
We have the following structure.
1. We have Service A which gives Response A
2. From Response A we create a new request B. The Code for refactor in this Question
This is where I have to do the conversion from one Service response to Another request object and they have different structure.
3. This Request B will be used to make another service call.
All proxies included in Wrapper.dll .
We have the following structure.
1. We have Service A which gives Response A
2. From Response A we create a new request B. The Code for refactor in this Question
This is where I have to do the conversion from one Service response to Another request object and they have different structure.
3. This Request B will be used to make another service call.
All proxies included in Wrapper.dll .
The process you have described is fairly straightforward. There should not be any performance concerns with the refactored code. If you are concerned about performance, then you could always perform some unit tests and analysis between both methods to see if there are any potential bottlenecks.
-saige-
-saige-
ASKER
Excellent answer provided.
Even when it is already solved: Have you took a look at AutoMapper?
ASKER
No I haven't used Automapper. Just quickly browsed it. It looks cool and seems to reduce code lot. I think field names should map to each other.
But my DTO has different names than the webservice object.
And some fields are field values like Value = "1**8**NOM**04/29/2016**8* *1$$2$$3$$ 4$$5$$6$$7 $$8" .
Can I map this to class object using Automapper.
But my DTO has different names than the webservice object.
And some fields are field values like Value = "1**8**NOM**04/29/2016**8*
Can I map this to class object using Automapper.
ASKER