BryanCM.AspNet.Utils
1.0.2
dotnet add package BryanCM.AspNet.Utils --version 1.0.2
NuGet\Install-Package BryanCM.AspNet.Utils -Version 1.0.2
<PackageReference Include="BryanCM.AspNet.Utils" Version="1.0.2" />
<PackageVersion Include="BryanCM.AspNet.Utils" Version="1.0.2" />
<PackageReference Include="BryanCM.AspNet.Utils" />
paket add BryanCM.AspNet.Utils --version 1.0.2
#r "nuget: BryanCM.AspNet.Utils, 1.0.2"
#:package BryanCM.AspNet.Utils@1.0.2
#addin nuget:?package=BryanCM.AspNet.Utils&version=1.0.2
#tool nuget:?package=BryanCM.AspNet.Utils&version=1.0.2
BryanCM.AspNet.Utils
This is a project dedicated only to define a set of common functionalities that can be used across multiple projects. These functionalities can be:
- Definitions of Result Pattern.
- Abstraction for Error Definitions.
- Helpers for:
- Enums (e.g.: for get a Enum's value from a string)
- JSON Serialization/Deserialization
- Regex definitions.
- Injection of Helpers as
Services (IServiceCollection).
This project is created as a Package in NuGet and GitHub Packages as BryanCM.AspNet.Utils.
Table of Content
Installation
You can install the package using NuGet. Open the terminal and execute the following command:
dotnet add package BryanCM.AspNet.Utils
Features
Implementation with Enums
Imagine that you're creating an API that has an endpoint that receives a Status Field in its payload. Instead of sending a number value you can receive a string value. But, how can you get the real Enum's value?.
Simple. If you define/declare a Enum like this:
public enum Status
{
Created,
InProgress,
Done
}
... you can use this:
using BryanCM.AspNet.Utils.Helpers;
var statusReceived = "created";
EnumHelper.GetFromString<Status>(statusReceived);
And how can you determine if a value is defined within an Enum?, just do the following putting all together:
using BryanCM.AspNet.Utils.Helpers;
var statusReceived = "created";
if (!EnumHelper.IsInEnumNames<Status>(statusReceived)) { // Verification of Existence
return;
}
var status = EnumHelper.GetFromString<Status>(statusReceived);
Definition of Errors
You can define your own errors using an Abstract Class as the Base:
using BryanCM.AspNet.Utils.Abstractions
public sealed static class PersonErrors : BaseErrors
{
private static readonly string _scope = "Person";
public static readonly Error NotFound = GetError(_scope, nameof(NotFound), "Person not found.");
}
Usage of Result Pattern
In case of Success
using BryanCM.AspNet.Utils.Abstractions;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Result.Success(); // Only to specify a case of success
Result<Person>.Success(new Person{
Name = "Bryan Carrera",
Age = 23
}); // To specify a case of success with data as the operation's outcome.
In case of Failure
using BryanCM.AspNet.Utils.Abstractions;
class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
Result.Failure(PersonErrors.NotFound); // or 👇
Result<Person>.Failure(PersonErrors.NotFound);
Usage of Regex
using BryanCM.AspNet.Utils.Regex;
var email = "john.doe@example.com";
if (RegexPatterns.EmailAddress.IsMatch(email))
{
Console.WriteLine("[x]. Valid");
return;
}
Console.WriteLine("[x]. Invalid");
Contribution
Contributions are welcome! Please fork the repository and submit a pull request.
Tests
This project uses xUnit for Unit Tests.
To run the tests:
cd src\BryanCM.AspNet.Utils\Tests
dotnet tests
| 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.DependencyInjection (>= 10.0.1)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on BryanCM.AspNet.Utils:
| Package | Downloads |
|---|---|
|
Security.Authz.Api
.NET Package that allows developers configure Authorization in a fast way only providing the Identity Provider to use. |
GitHub repositories
This package is not used by any popular GitHub repositories.