The need for nullable reference types to advertise optional constructor dependencies

In "traditional" OOP, you advertise your class required dependencies via constructor arguments:

public Foo(IOutput output, ILogger logger, ...)

Typically, the first few lines of code will check that these dependencies are not null.

Optional dependencies may be provided as properties, which you can leave unset (null).

Internally, in order to avoid a multitude of conditionals checking for nulls, you might have your own "null" implementations of the dependencies' interfaces (i.e. NullLogger which does nothing). This way the code is more readable, and you can always assume the dependencies are non-null and you'll never get a NullReferenceException ;)...

Read full article

No Comments