A more convenient way to handle null references in an object hierarchy

Im looking for a good way to handle null reference in an object hierarchy.

t

if(null == Object1.Object2.Object3.Property) 

This example throws a Null Reference exception if it is indicated that Object2 is null.

In my case, I don't care what null is, there is just something. I really don't want to put try / catch in every place, that I want to do something like this, so I was looking for an alternative.

I experimented with the operator, but this leads to some ugly code after two levels.

Any ideas appreciated.

+4
source share
6 answers

Now this may be tangent ... but I would suggest a design change to avoid ugliness and pain.

Calling Object1.Object2.Object3.Property violates the demeter law. Instead, if you need to get to this property, Object1 should open the Property property ... So you should call Object1.RetrievedFromTheDepthsProperty
Why is this necessary .. is that if the Type Object2 constructor changes the type of the object returned by the field / Object3 to the one that does not have the Property you are looking for, you would be hosed. The client knows too much about the internal structure of Object1. If Object1 is encapsulated where the data is inside, you will be safe from future changes. Also this property can do all null checking as needed ... leaving you with much cleaner

 if (Object1.RetrievedFromTheDepthsProperty == null) {...} 
+6
source

This (null-safe dereferencing) is what sometimes arises, and not; There is currently no accurate answer, except:

 if(Object1 == null || Object1.Object2 == null || Object1.Object2.Object3 == null || Object1.Object2.Object3.Property == null) 

You can do a little caching if you need (by entering variables), but it gets even uglier:

 SomeType2 obj2; SomeType3 obj3; if(Object1 == null || (obj2 = Object1.Object2) == null || (obj3 = obj2.Object3) == null || obj3.Property == null) 

In general, I would advise you to disagree with the above if you really do not want to name the property twice (because it works more than the property)

+2
source

You can use Null Object pattern .

 class Car { Engine engine = null; public Engine Engine { get { return engine ?? new NullEngine(); } } } class Engine { string make; public virtual string Make { get { return make; } } } class NullEngine : Engine { public override string Make { get { return null; } } } 

Then you could do:

 Car car; if (car.Engine.Make != null) Console.WriteLine(car.Engine.Make); 

Instead:

 Car car; if (car.Engine != null && car.Engine.Make != null) Console.WriteLine(car.Engine.Make); 

Please note that quite a lot of work to define “null objects” for your entire model. You also need to be very careful not to confuse users of your model. The code is usually quickly exploded if you exchange zero and forget to check it, but a “null object” can more easily survive a deep call in a callstack and cause subtle problems because it is not a real object.

+2
source

Directly answering your question - just tips:

  • try using a null object
  • it’s just not possible to go through the hierarchy so far (three levels), maybe some method on the first / second level that will answer this condition will help
+1
source

In general, if you don't care what property was null in the object hierarchy, just don't check it. Use a global error handler for the application (this will depend on the type of application ASP.NET, WinForms, ...) and say that the user did something wrong.

0
source

I would strongly suggest you take a look at the following publication: http://www.hardcodet.net/2008/12/observe-dependencies-through-lambda-expressions-part1

This applies to a similar problem and also makes it easy to handle changes.

0
source

All Articles