Link to home
Start Free TrialLog in
Avatar of Member_2_7967608
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(GetTransactionResponse 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_data.attributes)
                {
                    if(!String.IsNullOrEmpty(quoteAttr.value))
                    {
                        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_data.sub_documents)
                {
                    SalesOrder salesOrder = new SalesOrder();
                    AttributeType prodAttrs = subDoc.attributes[0];
                    if (prodAttrs != null && !string.IsNullOrEmpty(prodAttrs.value))
                    {
                        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(values[4])?int.Parse(values[4]):0);
                       
                        foreach (string scn in values[5].Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries))
                        {
                            scnListCode.Add(new acsModels.ScnCode { scnCode =scn });
                        }
                        salesOrder.scnCodes = scnListCode;
                        salesOrders.Add(salesOrder);
                    }
                }

                req.salesOrders = salesOrders;
            }
            return req;

        }
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Member_2_7967608
Member_2_7967608

ASKER

The code works well. Thank you very much. I see an exception as shown in attached, even though the error doesn't propagate to UI.
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-
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-
Thanks saige.  Do you see any performance issue. This is encapsulated part of service .
But total # of Fields would be not more than 10.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 .
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-
Excellent answer provided.
Even when it is already solved: Have you took a look at AutoMapper?
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.