ILogger and Null Object Pattern

TheĀ Null Object PatternĀ is a pattern that uses objects with null behavior instead of performing null checks throughout the codebase. ILogger and ILoggerFactory are dependencies that often require a lot of null checking, so they are perfect candidates for the null object pattern. Suppose your classes take ILogger or ILoggerFactory as a dependency, and you are not using the null object pattern. In that case, you will probably find that your code is either subject to NullReferenceExceptions, or forcing implementors to supply loggers as arguments. Use the null object pattern to avoid both these problems. This article teaches you how in C#. Null object might be confusing for some people because it seems to imply that the object reference might be null. However, the opposite is true. The object will never be a null reference. Null refers to the behavior of the object - not the reference itself. I think that a better name for the pattern would be Dummy Object Pattern since the objects you
October 14, 2020
871
741