From 7c3dd84971f70d82ea7e8282fd76c19def79490c Mon Sep 17 00:00:00 2001 From: JustFixMe Date: Sun, 2 Feb 2025 11:51:55 +0400 Subject: [PATCH] readme and publish workflow --- .gitea/workflows/publish-nuget.yaml | 38 +++++++++++++++ readme.md | 76 +++++++++++++++++++++++++++-- 2 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 .gitea/workflows/publish-nuget.yaml diff --git a/.gitea/workflows/publish-nuget.yaml b/.gitea/workflows/publish-nuget.yaml new file mode 100644 index 0000000..2686fe9 --- /dev/null +++ b/.gitea/workflows/publish-nuget.yaml @@ -0,0 +1,38 @@ +name: .NET Publish + +on: + push: + tags: + - 'v[0-9]+.[0-9]+.[0-9]+' + - 'v[0-9]+.[0-9]+.[0-9]+-rc[0-9]+' + +jobs: + publish: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v4 + + - name: Setup .NET + uses: https://github.com/actions/setup-dotnet@v4 + with: + dotnet-version: 9.x + + - name: Restore dependencies + run: dotnet restore --nologo + + - name: Test + run: dotnet test --nologo --no-restore --configuration Release + + - name: Create the package + env: + RELEASE_VERSION: ${{ gitea.ref_name }} + run: > + dotnet pack --no-restore --configuration Release --output nupkgs + `echo $RELEASE_VERSION | sed -E 's|^(v([0-9]+(\.[0-9]+){2}))(-([a-z0-9]+)){1}|/p:ReleaseVersion=\2 /p:VersionSuffix=\5|; s|^(v([0-9]+(\.[0-9]+){2}))$|/p:ReleaseVersion=\2|'` + + - name: Publish the package to Gitea + run: dotnet nuget push --source ${{ vars.OUTPUT_NUGET_REGISTRY }} --api-key ${{ secrets.LOCAL_NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg + + - name: Publish the package to NuGet.org + run: dotnet nuget push --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_PACKAGE_TOKEN }} nupkgs/*.nupkg diff --git a/readme.md b/readme.md index 00d21bf..52a8435 100644 --- a/readme.md +++ b/readme.md @@ -7,24 +7,90 @@ Inspired by [MediatR](https://github.com/jbogard/MediatR) ## Features * Separate dispatching of Commands/Queries -* Middleware-like behaviours +* Middleware-like Behaviors + +## Compatibility + +**Just.Cqrs** is built for .Net Standard 2.1 and .NET 8.0 and 9.0. ## Getting Started ### Install from NuGet.org -``` +```bash # install the package using NuGet dotnet add package Just.Cqrs ``` ### Register in DI with ```IServiceCollection``` -```cs +```csharp services.AddCqrs(opt => opt .AddQueryHandler() .AddCommandHandler() - .AddBehaviour() - .AddOpenBehaviour(typeof(SomeOpenBehaviour<,>)) + .AddBehavior() + .AddOpenBehavior(typeof(LoggingBehavior<,>)) ); ``` + +## Example Usage + +### Define a Query and Handler + +```csharp +record GetUserByIdQuery(int UserId) : IKnownQuery; + +class GetUserByIdQueryHandler : IQueryHandler +{ + public ValueTask Handle(GetUserByIdQuery query, CancellationToken cancellationToken) + { + // Fetch user logic here + } +} + +// Use Dispatcher to execute the query +class GetUserByIdUseCase(IQueryDispatcher dispatcher) +{ + public async Task Execute(int userId, CancellationToken cancellationToken) + { + var user = await dispatcher.Dispatch(new GetUserByIdQuery(userId), cancellationToken); + } +} +``` + +\* *the same principles apply to commands* + +### Define a Behavior + +```csharp +class LoggingBehavior(ILogger logger) : IDispatchBehavior + where TRequest: notnull +{ + public async ValueTask Handle( + TRequest request, + DispatchFurtherDelegate next, + CancellationToken cancellationToken) + { + logger.LogInformation("Handling request: {RequestType}", typeof(TRequest).Name); + var result = await next(); + logger.LogInformation("Request handled: {RequestType}", typeof(TRequest).Name); + return result; + } +} + +class SomeQueryBehavior : IDispatchBehavior +{ + public async ValueTask Handle( + SomeQuery request, + DispatchFurtherDelegate next, + CancellationToken cancellationToken) + { + // do something + return await next(); + } +} +``` + +## License + +**Just.Cqrs** is licensed under the [MIT License](LICENSE).