SG.NullableExtensions
0.1.0
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
<PackageReference Include="SG.NullableExtensions" Version="0.1.0" />
<PackageVersion Include="SG.NullableExtensions" Version="0.1.0" />
<PackageReference Include="SG.NullableExtensions" />
paket add SG.NullableExtensions --version 0.1.0
#r "nuget: SG.NullableExtensions, 0.1.0"
#addin nuget:?package=SG.NullableExtensions&version=0.1.0
#tool nuget:?package=SG.NullableExtensions&version=0.1.0
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 | Versions 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. |
-
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.