SG.NullableExtensions 0.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package SG.NullableExtensions --version 0.1.0
                    
NuGet\Install-Package SG.NullableExtensions -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SG.NullableExtensions" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SG.NullableExtensions" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="SG.NullableExtensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SG.NullableExtensions --version 0.1.0
                    
#r "nuget: SG.NullableExtensions, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=SG.NullableExtensions&version=0.1.0
                    
Install SG.NullableExtensions as a Cake Addin
#tool nuget:?package=SG.NullableExtensions&version=0.1.0
                    
Install SG.NullableExtensions as a Cake Tool

Nullable Extensions

This is a set of extension methods for C# nullable types (reference types and Nullable<T>) that allow you to ger rid of null checks using if statements and ternary operators. This draws inspiration from Rust.

Methods

Inspect

The Inspect() methods performs an action against the value if the value is not null.

var product = products.FirstOrDefault(p => p.Code == Code);
product.Inspect(product => product.UpdatedDate = DateTime.NowUtc);

In addition to that Inspect() has async overloads.

var delivery = await delivery.GetProductAsync(id);
product.InspectAsync(p => {
    p.Address = await addressService.GetByPostalCodeAsync(p.PostlCode)
});

Some of the overloads actually extend the ValueTask<T> class, so instead of inspecting a task they inspect the output of the task:

await serializer.SerializeAsync()
    .InspectAsync(message => processor.ProcessAsync(message));
await serializer.SerializeAsync()
    .InspectAsync(message => logger.LogInformation(message));

WhenNull

This method as opposed to Inspect() call the delegate when the value is null.

await serializer.Serialize()
    .WhenNull(message => logger.LogError("Failed to serialize message!"));

Since Inspect() and WhenNull() return the unchanged value you can combine these methods:

await serializer.Serialize()
    .WhenNull(() => logger.LogError("Failed to serialize message!"))
    .Inspect(message => logger.LogInformation(message))
    .Inspect(message => processor.Process(message));

WhenNull() has the same set of async overloads as Inspect().

await serializer.SerializeAsync()
    .WhenNullAsync(() => logger.NotifyAsync("Failed to serialize a message!"));
});
await serializer.SerializeAsync()
    .WhenNullAsync(() => logger.LogError("Failed to serialize a message!"));
await serializer.Serialize()
    .WhenNullAsync(() => notifier.NotifyAsync("Failed to serialize message!"));

Is

Checks if the underlying value is not null. If so, it'll run specified predicate.

var user = userService.GetUser(id);
user.Is(u => u.EmailAddress == emailAddress);

Also, async overloads available.

var user = userService.GetUser(id);
user.IsAsync(u => userOrderService.HasPendingOrdersAsync(u.Id));
userService.GetUserAsync(id).IsAsync(u => userOrderService.HasPendingOrdersAsync(u.Id));

Map

Transforms the underlying value if it is not null.

var user = dbContext.User.FirstOrDefaultAsync(u => u.Id == id)
    .MapAsync(u => new UserModel(u));

ValueOr

Tries to unwrap the underlying value. If the value is null, it will return specified default value.

var address = employee.Address.ValueOr(new Address());

Also, there is an overload that uses a factory instead of plain value.

var address = employee.Address.ValueOr(() => addressService.GetDefaultAddress());

Async overloads are available as well.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 96 9 months ago
0.1.0 98 9 months ago