Skip to content

An example of using the MediatR and Hangfire libraries to create a simple invoicing application

License

Notifications You must be signed in to change notification settings

kubagdynia/MediatR-Hangfire

Repository files navigation

MediatR-Hangfire

CI

An example of using the MediatR and Hangfire (background processing) libraries to create a simple invoicing application

Introduction

This is a simple invoicing application that demonstrates how to use the MediatR and Hangfire libraries to create a simple invoicing application. The application is built using .NET 8 and uses the MediatR library to implement the mediator pattern. The application also uses the Hangfire library to implement background processing.

Getting Started

To get started with the application, clone the repository to your local machine and run the application using the following steps:

  • Clone the repository to your local machine using the following command:

    git clone https://github.com/kubagdynia/MediatR-Hangfire.git
  • Navigate to the directory with the application's web api:

      cd ./MediatR-Hangfire/MediatRTest.Api/
  • Run the application using the following command:

      dotnet run
  • The application will start, and you can access it by navigating to the following URL in your browser:

        http://localhost:5190/swagger

    Hangfire Dashboard

  • By default, you can also access the Hangfire dashboard by navigating to the following URL in your browser:

        http://localhost:5190/hangfire

    Hangfire Dashboard

Design Overview

The communication between the API and the domain layer is divided into commands and queries. Commands are used to perform actions that change the state of the system, while queries are used to retrieve data from the system. The MediatR library is used to implement the mediator pattern, which allows the API to send commands and queries to the domain layer without having to know the details of how they are handled.

Invoice creation process:

When an attempt is made to create an invoice, a command to create the invoice is sent. The data validation process is implemented using a MediatR pipeline, with validation handled by the FluentValidation library. If validation fails, a DomainException is thrown. This exception is then caught by the global exception handler (GlobalExceptionHandler), which creates an appropriate error description and returns it to the API user.

If the validation succeeds, the appropriate handler processes the invoice creation request. The invoice is created and saved in the database, and an event signaling the invoice creation is emitted. If the Hangfire system is enabled (configuration), this event is queued for background processing. There are two handlers attached to the invoice creation event:

This model allows for separation of application logic and ensures scalability and ease of system expansion.

Invoice creation process

Project structure

The project is structured as follows:

  • MediatRTest.Api - Web API project
  • MediatRTest.Api.Tests - Integration tests for the Web API project
  • MediatRTest.Core - Core project containing the core logic of the application
  • MediatRTest.Data - Data access project containing the database context and data access logic
  • MediatRTest.Invoices - Project containing the invoice processing logic (domain logic)
  • MediatRTest.Invoices.Tests - Unit tests for the invoice processing logic

Technologies

List of technologies, frameworks and libraries used for implementation:

License

This project is licensed under the MIT License.

About

An example of using the MediatR and Hangfire libraries to create a simple invoicing application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages