Wim Jongeneel
1 min readFeb 17, 2021

--

I don't think you should ever use the null object pattern for something like a repository. There just not being a value in the DB is a very normal case when running a query. The caller of GetOrder is always going to have to do something with it in one way or another. If the caller wants to do anything with the null-order it is still going to have to write an if-statement in almost every case.

Otherwise you will have to first do a query if it exists, but then you can get a race condition if someone removes it between your two queries (the old check-then-act problem of parallel programming).

The null object pattern is however useful if you have a interface that abstracts something, but you also want a turn-off option without writing an if in every place where you use that interface. For example like the NullLogger in dotnet where you can have an implementation of ILogger that actually doesn't log. https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.logging.abstractions.nulllogger?view=dotnet-plat-ext-5.0.

--

--

Wim Jongeneel
Wim Jongeneel

Written by Wim Jongeneel

Software Engineer at Mendix (Rotterdam, The Netherlands) • Student MSc Software Engineering • Functional programming enthusiast

No responses yet