AsserterNemagus 1.0.1

dotnet add package AsserterNemagus --version 1.0.1                
NuGet\Install-Package AsserterNemagus -Version 1.0.1                
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="AsserterNemagus" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AsserterNemagus --version 1.0.1                
#r "nuget: AsserterNemagus, 1.0.1"                
#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.
// Install AsserterNemagus as a Cake Addin
#addin nuget:?package=AsserterNemagus&version=1.0.1

// Install AsserterNemagus as a Cake Tool
#tool nuget:?package=AsserterNemagus&version=1.0.1                

AsserterNemagus

AsserterNemagus is a library designed to facilitate assertion and validation tasks within .NET applications. It provides an intuitive API for performing assertions on various conditions, making it easier to ensure the correctness of your code.

Usage

  1. Add the library to your project

  2. Add the Asserter service to your application

// In your Program.cs
builder.Services.AddAsserter();
// or with options
builder.Services.AddAsserter(new AsserterOptions()
{
    EnableLogging = true,
    // Add other options
});
  1. (Optional) You can add the default exception filter to your controllers if you are developing a web application and want to return ProblemDetails. Otherwise you will be able to catch the AssertException and handle it yourself.
// Program.cs
builder.Services.AddControllers(options =>
{
    options.Filters.Add<AsserterExceptionFilter>();
});
  1. Inject the service into your classes
public class BlogService
{
    private readonly IAsserter _asserter;
    private readonly IBlogRepository _blogRepository;

    public BlogService(IAsserter asserter, IBlogRepository blogRepository)
    {
        _asserter = asserter;
        _blogRepository = blogRepository;
    }

    public List<Blog> GetBlogs(string searchTerm)
    {
        _asserter
            .NotNull(searchTerm)
            .NotEmpty(searchTerm)
            .Message("Error occurred while retrieving blog posts.")
            .Log($"Error while searching blogs, {nameof(searchTerm)} must not be null or empty.")
            .Assert();

	return _blogRepository.GetBlogs(searchTerm);
    }
}

Logging

You can enable or disable logging using the AsserterOptions.EnableLogging. If enabled, breaking assertions like .True() will log to Error level, while non breaking ones like .TrueContinue() to Warning level. Each assertions logging default can be overridden by:

_asserter
    .NotNull(searchTerm)
    .Log(LogLevel.Critical, "Logging message")
    .Assert();
// OR
_asserter
    .NotNull(searchTerm)
    .Log("Logging message", LogLevel.Critical)
    .Assert();

Assertion error handling

If a breaking assertion fails, a AssertException is throw. If you are using the AsserterExceptionFilter, the response will be in the form of a BadRequestObjectResult with ProblemDetails. Otherwise you can catch the exception and use the AssertException.ProblemDetails data to handle it yourself.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.1 154 4/9/2024
1.0.0 129 4/9/2024