-1

This is a question out of curious more so than actual need, but I thought it would be interesting to find out, since I have classes soo deep I need 10 lines of instantiation just to set a single property.

The property [MyObj3] functionally works as I want it to, where on the get property function it checks if the object is null and instantiates it. Whereas [MyObj2] will throw a null reference exception when trying to reference its properties.

But to make every single property in this way is a bit of a bigger nightmare than I want to tackle. So the question here is if there is a simple inline way to handle this. Or any other ways I may not be thinking of.

-- UPDATE: I forgot to mention, I wanted to keep the object null until it is needed, otherwise it gets included in xml serialization.

public class Obj1
{
    public Obj2 MyObj2 { get; set; }

    private Obj3 _obj3;
    public Obj3 MyObj3
    {
        get => _obj3 == null ? _obj3 = new Obj3() : _obj3;
        set => _obj3 = value;
    }
}

public class Obj2
{
    public string MyProp1 { get; set; }
}

public class Obj3
{
    public string MyProp1 { get; set; }
}

public void test()
{
    var obj = new Obj1();
    obj.MyObj2.MyProp1 = "test";//null exception
    obj.MyObj3.MyProp1 = "test";//works fine
}
5tar-Kaster
  • 910
  • 2
  • 12
  • 30
  • @OlivierRogier, the getter does perform the get part on the _obj3, after it has done a null check and instantiates it. The code here works 100% fine, I'm just wanting to know if there is a better / easier way to do this that doesn't add a massive amount of lines to the project. – 5tar-Kaster May 31 '21 at 08:13
  • `obj.MyObj2.MyProp1 = "test";` - this in itself is a code smell in my book. – Fildor May 31 '21 at 08:14
  • 1
    [`Lazy` maybe?](https://learn.microsoft.com/en-us/dotnet/api/system.lazy-1?view=net-5.0) – Charlieface May 31 '21 at 08:15
  • I don't understand the question. Do you ask how to refactor something? Indeed, MyObj2 is null, therefore the exception. Do you want a method that check if null and create the instance of any property, to call it from any property? Why not just t assign them when declaring? –  May 31 '21 at 08:17
  • why can't you just new up any child properties in the constructor? or even `public ObjX MyObjX {get;set;} = new ObjX()`? –  May 31 '21 at 08:18
  • To me this seems an x-y-Question. If I were to review this, I'd say `Obj1` should have a method (let's call it) `SetSomeStringValue(string)` that hides the knowledge about `Obj1`'s internals from the caller. (See "Law of demeter") This could also be hidden in a Property of `Obj1` that spans the internal `_myObj2` Field, of course. But you get the idea. – Fildor May 31 '21 at 08:44
  • ^^ Maybe I'd even create a "Convenience-Wrapper", so it can be handled comfortably while maintaining a clean datastructure inside for Serialization. – Fildor May 31 '21 at 08:53

1 Answers1

0

You could instantiate Obj2 and Obj3 when you instantiate Obj1 like this

public class Obj1
{
    public Obj2 MyObj2 { get; set; } = new Obj2();
    public Obj3 MyObj3 { get; set; } = new Obj3();
}
Hans Kilian
  • 18,948
  • 1
  • 26
  • 35
  • I forgot about this when making the question, but the reason I didn't want to do this was because it will get included when doing xml serialization. I wanted to keep the object null until it gets used – 5tar-Kaster May 31 '21 at 08:22
  • In that case I can't see an easier way than what you've already done – Hans Kilian May 31 '21 at 08:27