Moller.Utilities
1.0.2
See the version list below for details.
dotnet add package Moller.Utilities --version 1.0.2
NuGet\Install-Package Moller.Utilities -Version 1.0.2
<PackageReference Include="Moller.Utilities" Version="1.0.2" />
<PackageVersion Include="Moller.Utilities" Version="1.0.2" />
<PackageReference Include="Moller.Utilities" />
paket add Moller.Utilities --version 1.0.2
#r "nuget: Moller.Utilities, 1.0.2"
#:package Moller.Utilities@1.0.2
#addin nuget:?package=Moller.Utilities&version=1.0.2
#tool nuget:?package=Moller.Utilities&version=1.0.2
Moller.Utilities
Moller.Utilities is a small .NET utility library that currently provides:
- Fluent validation helpers for arguments and state
- A thread-safe
PeriodicActionRunnerfor running repeated background work
Target Framework
This library currently targets net10.0.
Installation
Once published to NuGet, install it with:
dotnet add package Moller.Utilities
Features
Fluent Validation
The Validation API provides a chainable way to validate values and preserve the caller argument name automatically.
using Moller.Utilities;
string name = "Alice";
var validatedName = Validation.For(name)
.IsNotNull()
.IsNotEmpty()
.IsNotNullOrWhiteSpace()
.Ensure(x => x!.Length <= 50, "Name must be 50 characters or fewer.")
.Value();
You can also use the instance-style extension syntax:
using Moller.Utilities;
string connectionName = "Primary";
var value = connectionName
.Validate()
.IsNotNull()
.IsNotEmpty()
.Value();
For simple reference-type null checks:
using Moller.Utilities;
MyService service = maybeNullService.RequireNotNull();
Supported validation helpers include:
IsNotNull()IsNotDefault()IsNotEmpty()IsNotNullOrWhiteSpace()Ensure(...)IsInRange(...)forint,long,float,double, anddecimalIsPositive()IsNegative()IsNotZero()Value()
PeriodicActionRunner
PeriodicActionRunner runs an Action immediately, then repeats it on a fixed interval using a background task.
using Moller.Utilities;
using var runner = new PeriodicActionRunner();
runner.Start(() =>
{
Console.WriteLine($"Heartbeat: {DateTimeOffset.Now}");
}, intervalMilliseconds: 1000);
await Task.Delay(5000);
await runner.StopAsync();
You can also pass your own CancellationToken. In that case, the runner will observe that token, but it will not cancel or dispose it for you.
using Moller.Utilities;
using var cts = new CancellationTokenSource();
using var runner = new PeriodicActionRunner();
runner.Start(
action: () => Console.WriteLine("Polling..."),
intervalMilliseconds: 2000,
cancellationToken: cts.Token);
cts.Cancel();
await runner.StopAsync();
Logging
PeriodicActionRunner accepts an optional ILogger<PeriodicActionRunner> and logs start, stop, execution, and exception events through Microsoft.Extensions.Logging.
using Microsoft.Extensions.Logging;
using Moller.Utilities;
ILogger<PeriodicActionRunner> logger = loggerFactory.CreateLogger<PeriodicActionRunner>();
using var runner = new PeriodicActionRunner(logger);
Notes
PeriodicActionRunner.Start(...)throws if the runner is already active.StopAsync()is safe to call even if the runner was never started.Stop()blocks synchronously and is best avoided on UI threads.- Exceptions thrown by the repeated action are logged and the loop continues running.
License
MIT License
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Logging (>= 10.0.3)
GitHub repositories
This package is not used by any popular GitHub repositories.