From 76d32ee954713a595514d683dc2bea097f0656f3 Mon Sep 17 00:00:00 2001 From: Maksym Sadovnychyy Date: Thu, 14 Nov 2024 21:53:40 +0100 Subject: [PATCH] (refactor): project naming and readme --- README.md | 166 +++++++++++++++++- src/{MaksIT.Core.Dapr.sln => MaksIT.Dapr.sln} | 10 +- .../DaprService.cs | 4 +- .../Extensions/ServiceCollectionExtensions.cs | 2 +- .../Extensions/WebApplicationExtensions.cs | 2 +- .../MaksIT.Dapr.csproj} | 8 +- src/Release-NuGetPackage.ps1 | 2 +- src/Release-NuGetPackage.sh | 2 +- 8 files changed, 179 insertions(+), 17 deletions(-) rename src/{MaksIT.Core.Dapr.sln => MaksIT.Dapr.sln} (63%) rename src/{MaksIT.Core.Dapr => MaksIT.Dapr}/DaprService.cs (96%) rename src/{MaksIT.Core.Dapr => MaksIT.Dapr}/Extensions/ServiceCollectionExtensions.cs (92%) rename src/{MaksIT.Core.Dapr => MaksIT.Dapr}/Extensions/WebApplicationExtensions.cs (96%) rename src/{MaksIT.Core.Dapr/MaksIT.Core.Dapr.csproj => MaksIT.Dapr/MaksIT.Dapr.csproj} (82%) diff --git a/README.md b/README.md index 04b2ded..9fb3758 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,168 @@ -# MaksIT.Core.Dapr +# MaksIT.Dapr + +This repository hosts the `maksit-dapr` project, which utilizes [Dapr](https://dapr.io/) (Distributed Application Runtime) to facilitate building and managing microservices with ease. The project focuses on implementing a robust, scalable solution leveraging Dapr's building blocks and abstractions. + +## Table of Contents + +- [Overview](#overview) +- [Features](#features) +- [Getting Started](#getting-started) + - [Prerequisites](#prerequisites) + - [Installation](#installation) +- [Configuration](#configuration) +- [Usage](#usage) + - [Running the Project](#running-the-project) + - [Environment Variables](#environment-variables) +- [Testing](#testing) +- [Deployment](#deployment) +- [Contributing](#contributing) +- [License](#license) + +## Overview + +`maksit-dapr` serves as a foundational project to explore and implement Dapr-based microservices, demonstrating the integration of Dapr’s pub-sub, bindings, state management, and other building blocks in a distributed system environment. + +## Features + +- **Pub-Sub Integration**: Uses Dapr's pub-sub component for seamless event-driven communication. +- **State Management**: Efficient, distributed state handling across microservices. + +## Getting Started + +Ensure that you have the following installed: + +- [.NET 8.0 SDK](https://dotnet.microsoft.com/download) +- [Docker](https://www.docker.com/get-started) + +## Installation + +To install MaksIT.Core, add the package to your project via NuGet: + +```powershell +dotnet add package MaksIT.Dapr +``` + +Or manually add it to your .csproj file: + +```powershell + +``` + +## Usage + +### Registering Dapr Services + +To use `maksit-dapr` in your application, you must register the provided services for dependency injection. Follow these steps to integrate Dapr's pub-sub and state management capabilities in your ASP.NET Core application: + +1. **Register the Publisher and State Store Services**: Add these services in `Program.cs` or `Startup.cs`. + +```csharp +using MaksIT.Dapr.Extensions; + +var builder = WebApplication.CreateBuilder(args); + +// Register Dapr services +builder.Services.RegisterPublisher(); +builder.Services.RegisterStateStore(); + +var app = builder.Build(); + +// Set up Dapr subscriber middleware (optional) +// Only after Webapi Authorization services +app.RegisterSubscriber(); +``` + +2. **Use Controller as a Dapr Subscriber**: + +To designate a controller as a Dapr subscriber, annotate it with the `[Topic("pubsubName", "name")]` attribute: + +```csharp +using Dapr; + +[Topic("my-pubsub", "my-topic")] +public class MyController : ControllerBase +{ + [HttpPost("/my-endpoint")] + public IActionResult ReceiveMessage([FromBody] MyCommand payload) + { + + + // Handle message + return Ok(); + } +} +``` + +### Injecting and Using Dapr Services + +With the services registered, you can inject `IDaprPublisherService` and `IDaprStateStoreService` into controllers or other services as needed: + +```csharp +using MaksIT.Dapr; + +public class MyService +{ + private readonly IDaprPublisherService _publisher; + private readonly IDaprStateStoreService _stateStore; + + public MyService(IDaprPublisherService publisher, IDaprStateStoreService stateStore) + { + _publisher = publisher; + _stateStore = stateStore; + } + + public async Task PublishEventAsync() + { + var command = new MyCommand + { + CommandId = Guid.NewGuid(), + CommandName = "SampleCommand", + Timestamp = DateTime.UtcNow + }; + + var options = new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + }; + + var payload = JsonSerializer.Serialize(command, options); + + var result = await _publisher.PublishEventAsync("my-pubsub", "my-topic", payload); + if (!result.IsSuccess) + { + // Handle error + } + } + + public async Task SetStateAsync() + { + var saveResult = await _stateStore.SetStateAsync("my-store", "my-key", "my-value"); + if (!saveResult.IsSuccess) + { + // Handle error + } + } + + public async Task GetStateAsync() + { + var stateResult = await _stateStore.GetStateAsync("my-store", "my-key"); + return stateResult.IsSuccess ? stateResult.Value : null; + } + + public async Task DeleteStateAsync() + { + var deleteResult = await _stateStore.DeleteStateAsync("my-store", "my-key"); + if (!deleteResult.IsSuccess) + { + // Handle error + } + } +} +``` + +This setup enables your ASP.NET Core application to utilize Dapr's pub-sub, state management, and other building blocks with minimal boilerplate. -MaksIT.Core.Dapr is a facade library for Dapr. ## Contributing diff --git a/src/MaksIT.Core.Dapr.sln b/src/MaksIT.Dapr.sln similarity index 63% rename from src/MaksIT.Core.Dapr.sln rename to src/MaksIT.Dapr.sln index 39b4b27..9adcd66 100644 --- a/src/MaksIT.Core.Dapr.sln +++ b/src/MaksIT.Dapr.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.11.35327.3 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MaksIT.Core.Dapr", "MaksIT.Core.Dapr\MaksIT.Core.Dapr.csproj", "{25AE0805-E014-4945-8BE8-D0DC2B4EB0FF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MaksIT.Dapr", "MaksIT.Dapr\MaksIT.Dapr.csproj", "{D6A8FD32-11E6-422E-9C33-B2D302B87562}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -11,10 +11,10 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {25AE0805-E014-4945-8BE8-D0DC2B4EB0FF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {25AE0805-E014-4945-8BE8-D0DC2B4EB0FF}.Debug|Any CPU.Build.0 = Debug|Any CPU - {25AE0805-E014-4945-8BE8-D0DC2B4EB0FF}.Release|Any CPU.ActiveCfg = Release|Any CPU - {25AE0805-E014-4945-8BE8-D0DC2B4EB0FF}.Release|Any CPU.Build.0 = Release|Any CPU + {D6A8FD32-11E6-422E-9C33-B2D302B87562}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D6A8FD32-11E6-422E-9C33-B2D302B87562}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D6A8FD32-11E6-422E-9C33-B2D302B87562}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D6A8FD32-11E6-422E-9C33-B2D302B87562}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/src/MaksIT.Core.Dapr/DaprService.cs b/src/MaksIT.Dapr/DaprService.cs similarity index 96% rename from src/MaksIT.Core.Dapr/DaprService.cs rename to src/MaksIT.Dapr/DaprService.cs index 87324bb..67be652 100644 --- a/src/MaksIT.Core.Dapr/DaprService.cs +++ b/src/MaksIT.Dapr/DaprService.cs @@ -4,7 +4,7 @@ using Dapr.Client; using MaksIT.Results; -namespace MaksIT.Core.Dapr; +namespace MaksIT.Dapr; public interface IDaprPublisherService { Task PublishEventAsync(string pubSubName, string topicName, string payload); @@ -17,7 +17,7 @@ public interface IDaprStateStoreService { } public class DaprService : IDaprPublisherService, IDaprStateStoreService { - private const string _errorMessage = "MaksIT.Core.Dapr - Data provider error"; + private const string _errorMessage = "MaksIT.Dapr - Data provider error"; private readonly DaprClient _client; private readonly ILogger _logger; diff --git a/src/MaksIT.Core.Dapr/Extensions/ServiceCollectionExtensions.cs b/src/MaksIT.Dapr/Extensions/ServiceCollectionExtensions.cs similarity index 92% rename from src/MaksIT.Core.Dapr/Extensions/ServiceCollectionExtensions.cs rename to src/MaksIT.Dapr/Extensions/ServiceCollectionExtensions.cs index f943d40..098aeaa 100644 --- a/src/MaksIT.Core.Dapr/Extensions/ServiceCollectionExtensions.cs +++ b/src/MaksIT.Dapr/Extensions/ServiceCollectionExtensions.cs @@ -1,6 +1,6 @@ using Microsoft.Extensions.DependencyInjection; -namespace MaksIT.Core.Dapr.Extensions; +namespace MaksIT.Dapr.Extensions; public static class ServiceCollectionExtensions { public static void RegisterPublisher(this IServiceCollection services) { services.AddDaprClient(); diff --git a/src/MaksIT.Core.Dapr/Extensions/WebApplicationExtensions.cs b/src/MaksIT.Dapr/Extensions/WebApplicationExtensions.cs similarity index 96% rename from src/MaksIT.Core.Dapr/Extensions/WebApplicationExtensions.cs rename to src/MaksIT.Dapr/Extensions/WebApplicationExtensions.cs index a4c25d3..a971866 100644 --- a/src/MaksIT.Core.Dapr/Extensions/WebApplicationExtensions.cs +++ b/src/MaksIT.Dapr/Extensions/WebApplicationExtensions.cs @@ -1,7 +1,7 @@ using Microsoft.AspNetCore.Builder; -namespace MaksIT.Core.Dapr.Extensions; +namespace MaksIT.Dapr.Extensions; public static class WebApplicationExtensions { /// diff --git a/src/MaksIT.Core.Dapr/MaksIT.Core.Dapr.csproj b/src/MaksIT.Dapr/MaksIT.Dapr.csproj similarity index 82% rename from src/MaksIT.Core.Dapr/MaksIT.Core.Dapr.csproj rename to src/MaksIT.Dapr/MaksIT.Dapr.csproj index 881f37c..8315353 100644 --- a/src/MaksIT.Core.Dapr/MaksIT.Core.Dapr.csproj +++ b/src/MaksIT.Dapr/MaksIT.Dapr.csproj @@ -6,12 +6,12 @@ enable - MaksIT.Core.Dapr - 1.0.3 + MaksIT.Dapr + 1.0.4 Maksym Sadovnychyy MAKS-IT - MaksIT.Core.Dapr - MaksIT.Core.Dapr is a facade library for Dapr. + MaksIT.Dapr + MaksIT.Dapr is a facade library for Dapr. dotnet;dapr; https://github.com/MAKS-IT-COM/maksit-core-dapr MIT diff --git a/src/Release-NuGetPackage.ps1 b/src/Release-NuGetPackage.ps1 index e5f98fe..562c5f8 100644 --- a/src/Release-NuGetPackage.ps1 +++ b/src/Release-NuGetPackage.ps1 @@ -10,7 +10,7 @@ $nugetSource = "https://api.nuget.org/v3/index.json" # Define paths $solutionDir = Split-Path -Parent $MyInvocation.MyCommand.Path -$projectDir = "$solutionDir\MaksIT.Core.Dapr" +$projectDir = "$solutionDir\MaksIT.Dapr" $outputDir = "$projectDir\bin\Release" # Clean previous builds diff --git a/src/Release-NuGetPackage.sh b/src/Release-NuGetPackage.sh index 9b2b42b..e0a2595 100644 --- a/src/Release-NuGetPackage.sh +++ b/src/Release-NuGetPackage.sh @@ -13,7 +13,7 @@ nugetSource="https://api.nuget.org/v3/index.json" # Define paths scriptDir=$(dirname "$0") solutionDir=$(realpath "$scriptDir") -projectDir="$solutionDir/MaksIT.Core.Dapr" +projectDir="$solutionDir/MaksIT.Dapr" outputDir="$projectDir/bin/Release" # Clean previous builds