This is a Basic Wrapper for use case Outputs, commom used with Mediatr Output or CQRS Patterns
you can use in your project by nuget.org
dotnet add package Notim.Outputs --version 3.0.0
the usage is very simple, you only need to instace Output with classe that you need to transport.
var output = new Output<ClassThatYouNeedToTransport>();
if (something is not Good) {
output.AddFaultMessage("something is not good");
}
else{
output.AddMessage("Success");
output.AddResult(new ClassThatYouNeedToTransport(argsThenYouNeed));
}
return output;
you have the functionality to determine the Fault type to filter if you want to return a especific status code or retries on your topic consumers
var output = new Output<ClassThatYouNeedToTransport>();
if (something is not Good && externalServiceIsOffline)
{
output.AddFault(new Fault(FaultType.ExternalServiceUnavailable, "customer service is down"));
}
else if (something is not Good && cannotFindResultOnDatabase)
{
output.AddFault(new Fault(FaultType.ResourceNotFound, "the user with id xxx cannot be find"));
}
else if (something is not Good && invalidInputReceived)
{
output.AddFault(new Fault(FaultType.InvalidInput, "invalid fields"));
}
else
{
output.AddMessage("Success");
output.AddResult(new ClassThatYouNeedToTransport(argsThenYouNeed));
}
return output;
Asp .Net WebApi Controller using "use case" pattern example:
[HttpGet("/")]
public async Task<IActionResult> GetOrder(ClassThatYouReceiveDataInput input, CancelationToken cancelationToken)
Output<ClassThatYouNeedToTransport> output = await _findUserByIdUseCase.Handle(input, cancelationToken);
if (!output.IsValid && output.Fault?.FaultType is FaultType.ExternalServiceUnavailable)
return BadGateway(output.Fault);
if (!output.IsValid && output.Fault?.FaultType is FaultType.ResourceNotFound)
return NotFound(output.Fault);
if (!output.IsValid && output.Fault?.FaultType is FaultType.InvalidInput)
return UnprocessableEntity(output.Fault);
if (!output.IsValid && output.Fault?.FaultType is FaultType.InvalidOperation)
return UnprocessableEntity(output.Fault);
return Ok(output.GetResult());
}
success use case output
var output = Output<ClassThatYouNeedToTransport>.WithSuccess("use case finished with success", new ClassThatYouNeedToTransport());
Fault use case output
var output = Output<ClassThatYouNeedToTransport>.WithFault("An Fault was ocurred");
Fault object use case output
var output = Output<ClassThatYouNeedToTransport>.WithFault(new Fault(FaultType.ExternalServiceUnavailable, "external service is unavaiable"));
var output = Output();
output.AddFault("simple error");
var output = Output();
output.AddFault(new Fault(ErrorType.ExternalServiceUnavailable), "external error");
you can use FluentValidation library "ValidationResult" to create an Output with FaultType.InvalidInput, you just only need to install the extension package:
dotnet add package Notim.Outputs.FluentValidation --version 3.0.0
The usage is very simple:
IValidator<SomeClass> validator = new SomeClassValidator<SomeClass>();
var validationResult = validator.Validate(someObject);
var output = new Output<SomeClassThatUseToTransport>();
output.AddValidationResult(validationResult);
If you would like to contribute to this project, follow these steps:
- Create a branch for your changes (
git checkout -b feature/MyFeature
) - Commit your changes (
git commit -am 'feat: add a feature'
) - Push to the branch (
git push origin feature/MyFeature
) - Open a Pull Request
some issues you can talk with me [email protected]