/serviceresult

A generic service layer result library

Primary LanguageC#MIT LicenseMIT

build Coverage Status SonarQube Code smells

Service Result

ServiceResult is a minimalistic generic service result library for your service (domain) layer. By using a generic service result, your application will have a good interface towards the service layer. The service layer will also be easy to unit test.

The library consists of ~150 lines of code, is 100% unit tested and proven in use since 2008 at multiple big companies. It's well suited for critical enterprise systems.

Add Mimiware.ServiceResult to your project

Add directly to your csproj-file:

<PackageReference Include="Mimiware.ServiceResult" Version="1.4.1" />

or through Package Manager in Visual Studio:

Install-Package Mimiware.ServiceResult -Version 1.4.1

or through .NET Cli:

dotnet add package Mimiware.ServiceResult --version 1.4.1

Example usage

Example of a service method in a user service file.

public IServiceResult<UsersViewModel> GetUsers(string searchString)
{
    var result = new ServiceResult<UsersViewModel>();
    var users = _userRepository.GetUsers(searchString);

    try
    {
        var response = UsersViewModelFactory.Create(users);
        return result.Ok(response);
    }
    catch (Exception)
    {
        return result.Error(message: "Failed to get users");
    }
}

Corresponding call to the service method from a Controller.

[HttpGet]
public ActionResult<UsersViewModel> GetUsers(string searchString)
{
    var result = _userService.GetUsers(searchString);

    return result.IsSuccessCode
        ? Ok(result.Data)
        : StatusCode(result.Code, result.Data);
}

Example of the service layer in a RESTful microservice

Usage Context

Getting started

ServiceResult is available as NuGet, https://www.nuget.org/packages/Mimiware.ServiceResult/.