5

I'm getting a massive payload of XML from my WCF service, and I need to write it into a SQL database. I'm using the latest version of .NET and Entity Framework 6.

"Okay, that's great," you may say, "but what's the question?"

Well, the XML is being deserialized into C# objects (generated from paste-special) and they're working just great. However, whenever the payload from the service does not contain some field, I get a null reference exception when I'm writing the XML object over to the EF object (This is a class method):

public ICollection<object> GetObjects()
{
    List<object> objs = new List<object>();

    foreach (var i in XmlObject.SubObj.SubObj.SubObj)
    {
        objs.Add(new MyEfObject() {
            Prop1 = XmlObject.SubObj.SubObj.SubObj.ObjProperty // If "ObjProperty" is null,
                                                               // I get a null reference exception
        });
    }

    return objs;
}

So, I have really inelegant code to check

if (!ReferenceEquals(XmlObject.SubObj.SubObj.SubObj.ObjProperty, null) {
    // Do stuff
}

This would normally be fine, but the object is so large and I want to avoid typing this 150+ times (and for all the object properties of the object).

There has to be a more elegant way, no?

Ellis
  • 173
  • 13
  • 1
    You may simply use `==` but IMO the inelegant part is not check for `null` (which may be avoided using C#6 `?,` or null-object pattern) but the fact you're doing `XmlObject.SubObj.SubObj.SubObj.ObjProperty`, don't you think your classes are _little bit_ too coupled? BTW you **may** `XmlObject.SubObj.SubObj.SubObj.Where(x => x.property != null).Select(x => new MyEfObject { Prop1 = x.property }).ToList()` – Adriano Repetti Dec 23 '15 at 14:36
  • @AdrianoRepetti How do I avoid this? I'm using generated classes (from XSD.exe). – Ellis Dec 23 '15 at 14:38
  • Ellis, updated comment. You may use LINQ – Adriano Repetti Dec 23 '15 at 14:40
  • @AdrianoRepetti Ah, that would work, thanks. Yeah, the classes are so coupled because that's the way it was generated (the XML document I'm parsing has 200+ nested and nested-nested fields...). – Ellis Dec 23 '15 at 14:42
  • Wow, if you can't manually update generated code then you're unlucky and you have to stick with that _paths_. – Adriano Repetti Dec 23 '15 at 14:47
  • @AdrianoRepetti I'm not too happy with my work situation right now, believe me. – Ellis Dec 23 '15 at 14:48

1 Answers1

1

You may simply use == but IMO the inelegant part is not check for null (which may be avoided using null-object pattern) but the fact you're accessing XmlObject.SubObj.SubObj.SubObj.ObjProperty, I think your classes are really too coupled.

That said, if you can't change this, then you may use LINQ to make your code more readable:

public ICollection<MyEfObject> GetObjects() {
    return XmlObject.SubObj.SubObj.SubObj
        .Where(x => x.property != null)
        .Select(x => new MyEfObject { Prop1 = x.property })
        .ToList();
}
Adriano Repetti
  • 65,416
  • 20
  • 137
  • 208