Stand with Ukraine. Save peace in the world!

The founder of the project //devdigest, Andrey Gubskiy, launched a charity initiative to raise funds to support Ukraine.  Help »

stream:// .net


October 14, 2020
  661
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 will use are shells with no behavior.  Why Use the Null Object Pattern? If you inject dependencies into your classes, you need to validate against null or perform null checking throughout your code. The null conditional operator ?. helps, but it is still very easy to miss one. Every single missed question mark is a bug in the code. The null object pattern gives you a third option of using a dummy object instead. This reduces the number of code paths and therefore decreases the chance of NullReferenceExceptions while still allowing the implementor to instantiate the class without creating an instance of the dependency. In the case of ILogger, it is quite onerous to create an implementation. Simply put, you shouldn't do it. If you want to implement logging, you should use an existing logging library. It becomes even more onerous if the dependency is ILoggerFactory. The implementor needs to pull in external dependencies or create a cascading set of classes that they may have no idea how to implement. It gets much worse when you try to mock ILogger or ILoggerFactory dependencies. Although it is still important to verify that logging gets called. You can read about that here.  The Basics The good news is that the Microsoft.Extensions.Logging.Abstractions namespace comes with null objects right out of the box. All you need to do is use NullLogger.Instance and NullLoggerFactory.Instance as default instances in your constructor. That's it. Your class can now depend on these instances, as though there is a real instance.  This example guarantees that the logger will never be null without forcing the code to supply a logger. The readonly modifier ensures that the instance cannot be set to null after construction. The code does not throw a NullReferenceException: using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using System; namespace ConsoleApp { class Program { static void Main(string[] args) { new Example().Print(Hello World!); } } public class Example { readonly ILogger _logger; public Example(ILogger logger = null) { _logger = logger ?? NullLogger.Instance; } public void Print(string message) { _logger.LogTrace(Logged message: {message}, message); Console.WriteLine(message); } } } In some cases, your class should take an ILoggerFactory instance because it may need to pass loggers to child dependencies in the future. You can use the same approach. namespace ConsoleApp { class Program { static void Main(string[] args) { new Example().Print(Hello World!); } } public class Example { readonly ILogger _logger; readonly ILoggerFactory _loggerFactory; public Example(ILoggerFactory loggerFactory = null) { _loggerFactory = loggerFactory ?? NullLoggerFactory.Instance; _logger = _loggerFactory.CreateLogger<Example>(); } public void Print(string message) { _logger.LogTrace(Logged message: {message}, message); Console.WriteLine(message); } } } Notice that we use the static property instance of both classes. We could create new instances of these objects in each case, but this would consume extra memory CPU to construct. The static instances only instantiate once i...
October 13, 2020
  500
Sometimes Blazor needs a nudge to render
October 12, 2020
  514
Launching .NET for game development that runs on a wide variety of platform using purpose built gaming engines. Develop games on Windows, Linux, or mcaOS.
October 06, 2020
  557
The post describes JetBrains Rider as an alternate to Visual Studio and how Rider can help mitigate some of the Visual Studio pain-points.
October 05, 2020
  563
I have been a developer for the past 7 years. I’ve gone through a lot of code reviews during that time. I have devoted my time to learn good software engineering practices through C#. Last ye…
October 04, 2020
  484
Microsoft's .NET Core team admits it needs to be better at supporting contributors to its open-source projects.
October 01, 2020
  599
How to use System.IO.Pipelines and System.Threading.Channels APIs to speed up processing
September 29, 2020
  515
C# 8 will ship soon with .Net Core 5, and one of the most exciting features is Source Generators, a great new addition to the .Net ecosystem.
September 28, 2020
  1085
Tracking the cause of my first memory leak written in C# (that I know of)
September 23, 2020
  450
Visual Studio 2019 v16.8 Preview 3.1 brings Git Integration, C++20 conformance, .NET Productivity, and more. Plus, sign-up for Visual Studio Codespaces!

Hot Vacancies

.NETBack End Developer

Field Complete
.NET

Field Complete is a team of passionate, young & fun-loving professionals looking to change the uneffective way that Servicing Industry works on US markets. Field Complete is growing really fast. We are looking for a Back End Developer to build a top-level modern API, ready for high load. Strong expertise with:

Senior Xamarin Developer

DraftKings
Mobile

You will join a mobile team which is working on two very exciting projects, Sportsbook and Casino. The apps are used by users in the US, where we are working on the regulated markets. We are releasing apps every two weeks. Our apps are generating almost 75% of the company revenue and the user base is growing daily. Technical stack on the project: Xamarin.Forms, MVVM with DI, NewRelic, Azure + App Center etc. Switching to .Net MAUI in the nearest 2-3 months.

Senior .NET Engineer

DraftKings
.NET

You will be working in a large US-oriented company that puts as a priority: security, performance, and stability. The candidate will work on pushing a huge number of changes (several thousand per sec) to several thousand clients in a near real-time manner.

Middle strong .NET developer

SoftServe
.NET

Our customer is an American company that develops software for businesses to help manage their networks, systems, and information technology infrastructure. The company provides purpose-built products for IT professionals, MSPs, and DevOps pros.

Junior .NET Developer

Chudovo OU
.NET

We are looking for a Junior .Net developer for being involved in to further development of the B2B platform for IT companies. You'll work on mostly back-end tasks closely with a Senior level developer.