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.
License
—
Deps
3
Install Size
—
Vulns
✓ 0
Published
Apr 9, 2024
$ dotnet add package AsserterNemagusAsserterNemagus 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.
Add the library to your project
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
});
// Program.cs
builder.Services.AddControllers(options =>
{
options.Filters.Add<AsserterExceptionFilter>();
});
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);
}
}
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();
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.