📦 This repository has been archived and is no longer maintained.
Please note that this project is no longer supported or actively developed. The new and actively maintained repository can be found here: Funcfy
This package encapsulates responses between methods preventing traffic from null objects in an application.
Message.Response.Maybe is available on NuGet. You can find the raw NuGet file here or install it by the commands below depending on your platform:
- Package Manager
pm> Install-Package Message.Response.Maybe -Version 1.0.0
- via the .NET Core CLI:
> dotnet add package Message.Response.Maybe --version 1.0.0
- PackageReference
<PackageReference Include="Message.Response.Maybe" Version="1.0.0" />
- PaketCLI
> paket add Message.Response.Maybe --version 1.0.0
There is no need to configure dependency injection. Just install it in the project you want to use.
When using a method that has the possibility of returning a null, the envelopment with Maybe indicates that the return must be verified, preventing the application from break down due to the absence of the object value.
public class RepositoryAsync<Entity> : SpecificMethods<Entity>, IRepositoryAsync<Entity>
{
...
public async Task<Maybe<Entity>> FindAsync(int id)
=> await DbSet.FirstOrDefaultAsync(entity => entity.Id.Equals(id));
...
}
The developer can act as follows when receiving the response:
...
var entity = await EntityRepository.FindAsync(entityId);
if (!entity.HasValue)
return response.WithBusinessError("Not found");
...The response is not only intended to encapsulate the objects exchanged between the application's methods, but it also encapsulates information about the status of this response, error messages and can also indicate which was the property where the problem happened.
...
public static Response<Entity> Create(string name, string document)
{
var response = Response<Entity>.Create();
if (string.IsNullOrEmpty(name))
response.WithBusinessError(nameof(name), $"{nameof(name)} is invalid");
if (string.IsNullOrEmpty(document))
response.WithBusinessError(nameof(role), $"{nameof(document)} is invalid");
if (response.HasError)
return response;
return response.SetValue(new Entity(name, document));
}
...The project is under MIT License, so it grants you permission to use, copy, and modify a piece of this software free of charge, as is, without restriction or warranty.