Decode.Notifications
3.0.0
dotnet add package Decode.Notifications --version 3.0.0
NuGet\Install-Package Decode.Notifications -Version 3.0.0
<PackageReference Include="Decode.Notifications" Version="3.0.0" />
<PackageVersion Include="Decode.Notifications" Version="3.0.0" />
<PackageReference Include="Decode.Notifications" />
paket add Decode.Notifications --version 3.0.0
#r "nuget: Decode.Notifications, 3.0.0"
#:package Decode.Notifications@3.0.0
#addin nuget:?package=Decode.Notifications&version=3.0.0
#tool nuget:?package=Decode.Notifications&version=3.0.0
Decode.Notifications
A simple and lightweight library for domain notifications using the notification pattern in .NET.
📦 Installation
dotnet add package Decode.Notifications
🛠️ Usage
1. Dependency Injection Configuration
In your Program.cs or Startup.cs:
using Decode.Notifications.Extensions;
// You can use AddDomainNotifications() or the alias AddDecodeNotifications()
builder.Services.AddDomainNotifications();
2. Using in Services or Domain Logic
public class UserService
{
private readonly IDomainNotificationContext _notificationContext;
public UserService(IDomainNotificationContext notificationContext)
{
_notificationContext = notificationContext;
}
public void RegisterUser(string username)
{
if (string.IsNullOrWhiteSpace(username))
{
_notificationContext.Add("Username is required.", HttpStatusCode.BadRequest, nameof(username));
return;
}
// Logic...
}
}
3. The DomainNotification Model & Context Operations
Under the hood, notifications are stored as DomainNotification objects in the context.
DomainNotification Class Structure
- Message (
string): The validation or error message. - StatusCode (
int): The HTTP status code representation (defaults to400). - Key (
string?): Optional parameter representing the field name or key (e.g.nameof(username)).
Additional Context Methods
You can also add individual notifications directly or batch multiple notifications:
// Add an explicit DomainNotification object
_notificationContext.Add(new DomainNotification("Email is invalid", 422, "Email"));
// Add a collection of DomainNotifications at once (useful for batch validations)
List<DomainNotification> notificationBatch =
[
new DomainNotification("Password is too short", 400, "Password"),
new DomainNotification("Password must contain a number", 400, "Password")
];
_notificationContext.AddRange(notificationBatch);
// Retrieve all raw notifications currently in the context
IReadOnlyCollection<DomainNotification> rawNotifications = _notificationContext.Notifications;
GetStatusCode() returns the highest code, not the first
With notifications carrying different status codes, GetStatusCode() returns the maximum, and
200 OK when the context is empty. So a 400 and a 422 together yield 422. That is a deliberate
choice — the more specific code wins — but it means the response code does not necessarily match the
first notification added.
4. Handling Notifications in Controllers/Middleware
[ApiController]
[Route("[controller]")]
public class UsersController : ControllerBase
{
private readonly IDomainNotificationContext _notificationContext;
public UsersController(IDomainNotificationContext notificationContext)
{
_notificationContext = notificationContext;
}
[HttpPost]
public IActionResult Create(UserDto user)
{
// Call service...
if (_notificationContext.HasNotifications)
{
return StatusCode(_notificationContext.GetStatusCode(), _notificationContext.GetMessages());
}
return Ok();
}
}
📄 License
MIT License.
| 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 is compatible. 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. 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
-
net8.0
-
net9.0
NuGet packages (2)
Showing the top 2 NuGet packages that depend on Decode.Notifications:
| Package | Downloads |
|---|---|
|
Decode.AspNetCore
ASP.NET Core extensions for the Decode ecosystem, including global exception handling and base controllers. |
|
|
Decode.Notifications.FluentValidation
FluentValidation extensions for Decode Notifications library. |
GitHub repositories
This package is not used by any popular GitHub repositories.