Using xml: Is there an automap solution for this ETL code? on newest questions tagged xml – Stack Overflow

I have been seeing alot of “trend” lately to automate mapping of fields from one data context to another.

Here I am doing the following:

  • moving fields from an XML file to a database table.

  • The desired operation is “upsert” – existing records are updated and new records are created.

  • uses “external key” (as opposed to PK row id) to determine if record exists

I find this code to be exremely annoying to write.

Is something like automapper good fit to improve this, or if I should be using some other approach to the whole process?

(or maybe I’m coming up against the reason that people use things like SSIS to do tasks like this)

Here’s my code that processes an XDocument of Customer records and Upserts them using an EF data context.

    private void PushCustomers(XDocument data)
        var customers = data.Root.Elements("GetCustomers");

        using (var model = new DirectAgentsEntities())
            foreach (var customer in customers)
                Log("ListID: {0}", customer.Element("ListID"));

                // These are always present in the XML element for a customer
                string companyFileName = customer.Element("QuickBooksCompanyFileName").Value;
                string listID = customer.Element("ListID").Value;

                // The company file record already exists
                var companyFile = model.QuickBooksCompanyFiles.First(c => c.Name == companyFileName);

                // The customer may or may not exist
                var qbCustmer = model.QuickBooksCustomers.FirstOrDefault(c => c.ListId == listID && c.QuickBooksCompanyFile.Name == companyFileName);

                if (qbCustmer == null)
                    qbCustmer = new QuickBooksCustomer();

                // Populate customer fields
                qbCustmer.ListId = listID;
                qbCustmer.FullName = customer.Element("FullName").Value;
                qbCustmer.CompanyName = customer.Element("CompanyName") != null ? customer.Element("CompanyName").Value : "unknown";
                qbCustmer.Phone = customer.Element("Phone") != null ? customer.Element("Phone").Value : "unknown";
                qbCustmer.Email = customer.Element("Email") != null ? customer.Element("Email").Value : "unknown";
                qbCustmer.TermsRefFullName = customer.Element("TermsRefFullName") != null ? customer.Element("TermsRefFullName").Value : "unknown";
                qbCustmer.QuickBooksCompanyFile = companyFile;

See Answers

Using xml: using-xml

online applications demo