-
Notifications
You must be signed in to change notification settings - Fork 7
[ScalarDB .NET Client] Add microservice transactions sample with Shared-cluster pattern with LINQ #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dima-X
wants to merge
2
commits into
main
Choose a base branch
from
add-dotnet-microservice-transactions-sample-with-shared-cluster-with-linq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[ScalarDB .NET Client] Add microservice transactions sample with Shared-cluster pattern with LINQ #72
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
3 changes: 3 additions & 0 deletions
3
dotnet/microservice-transactions-sample-with-shared-cluster-with-linq/.dockerignore
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| **/bin/ | ||
| **/obj/ | ||
| **/.DS_Store |
23 changes: 23 additions & 0 deletions
23
dotnet/microservice-transactions-sample-with-shared-cluster-with-linq/Client/Client.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <LangVersion>12</LangVersion> | ||
| <RootNamespace>MicroserviceTransactionsSample.Client</RootNamespace> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="Grpc.Net.Client" Version="2.66.0"/> | ||
| <PackageReference Include="Grpc.Net.ClientFactory" Version="2.66.0"/> | ||
| <PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/> | ||
| <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="8.0.0"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <None Remove="Commands\"/> | ||
| </ItemGroup> | ||
| <ItemGroup> | ||
| <ProjectReference Include="..\Rpc\Rpc.csproj"/> | ||
| </ItemGroup> | ||
| </Project> |
22 changes: 22 additions & 0 deletions
22
...mple-with-shared-cluster-with-linq/Client/Commands/Binders/CustomerServiceClientBinder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System.CommandLine.Binding; | ||
| using Grpc.Net.Client; | ||
| using static MicroserviceTransactionsSample.Rpc.CustomerService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands.Binders; | ||
|
|
||
| public class CustomerServiceClientBinder : BinderBase<CustomerServiceClient> | ||
| { | ||
| private const string CustomerServiceUrl = "http://localhost:10010"; | ||
|
|
||
| protected override CustomerServiceClient GetBoundValue(BindingContext bindingContext) | ||
| { | ||
| var customerServiceUrl = Environment.GetEnvironmentVariable("CUSTOMER_SERVICE_URL"); | ||
| if (String.IsNullOrEmpty(customerServiceUrl)) | ||
| customerServiceUrl = CustomerServiceUrl; | ||
|
|
||
| var grpcOptions = new GrpcChannelOptions { LoggerFactory = Logging.GetLoggerFactory() }; | ||
| var grpcChannel = GrpcChannel.ForAddress(customerServiceUrl, grpcOptions); | ||
|
|
||
| return new CustomerServiceClient(grpcChannel); | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
...-sample-with-shared-cluster-with-linq/Client/Commands/Binders/OrderServiceClientBinder.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| using System.CommandLine.Binding; | ||
| using Grpc.Net.Client; | ||
| using static MicroserviceTransactionsSample.Rpc.OrderService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands.Binders; | ||
|
|
||
| public class OrderServiceClientBinder : BinderBase<OrderServiceClient> | ||
| { | ||
| private const string OrderServiceUrl = "http://localhost:10020"; | ||
|
|
||
| protected override OrderServiceClient GetBoundValue(BindingContext bindingContext) | ||
| { | ||
| var orderServiceUrl = Environment.GetEnvironmentVariable("ORDER_SERVICE_URL"); | ||
| if (String.IsNullOrEmpty(orderServiceUrl)) | ||
| orderServiceUrl = OrderServiceUrl; | ||
|
|
||
| var grpcOptions = new GrpcChannelOptions { LoggerFactory = Logging.GetLoggerFactory() }; | ||
| var grpcChannel = GrpcChannel.ForAddress(orderServiceUrl, grpcOptions); | ||
|
|
||
| return new OrderServiceClient(grpcChannel); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...ansactions-sample-with-shared-cluster-with-linq/Client/Commands/GetCustomerInfoCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands.Binders; | ||
| using MicroserviceTransactionsSample.Rpc; | ||
| using static MicroserviceTransactionsSample.Rpc.CustomerService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| public static class GetCustomerInfoCommand | ||
| { | ||
| private const string Name = "GetCustomerInfo"; | ||
| private const string Description = "Get customer information"; | ||
|
|
||
| private const string ArgName = "id"; | ||
| private const string ArgDescription = "customer ID"; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var customerIdArg = new Argument<int>(ArgName, ArgDescription); | ||
| var getCustomerInfoCommand = new Command(Name, Description) | ||
| { | ||
| customerIdArg | ||
| }; | ||
|
|
||
| getCustomerInfoCommand.SetHandler(getCustomerInfo, | ||
| customerIdArg, | ||
| new CustomerServiceClientBinder()); | ||
| return getCustomerInfoCommand; | ||
| } | ||
|
|
||
| private static async Task getCustomerInfo(int customerId, CustomerServiceClient client) | ||
| { | ||
| var request = new GetCustomerInfoRequest { CustomerId = customerId }; | ||
| var response = await client.GetCustomerInfoAsync(request); | ||
|
|
||
| Console.WriteLine(response); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...vice-transactions-sample-with-shared-cluster-with-linq/Client/Commands/GetOrderCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands.Binders; | ||
| using MicroserviceTransactionsSample.Rpc; | ||
| using static MicroserviceTransactionsSample.Rpc.OrderService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| public static class GetOrderCommand | ||
| { | ||
| private const string Name = "GetOrder"; | ||
| private const string Description = "Get order information by order ID"; | ||
|
|
||
| private const string ArgName = "id"; | ||
| private const string ArgDescription = "order ID"; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var orderIdArg = new Argument<string>(ArgName, ArgDescription); | ||
| var getOrderCommand = new Command(Name, Description) | ||
| { | ||
| orderIdArg | ||
| }; | ||
|
|
||
| getOrderCommand.SetHandler(getOrder, | ||
| orderIdArg, | ||
| new OrderServiceClientBinder()); | ||
| return getOrderCommand; | ||
| } | ||
|
|
||
| private static async Task getOrder(string orderId, OrderServiceClient client) | ||
| { | ||
| var request = new GetOrderRequest { OrderId = orderId }; | ||
| var response = await client.GetOrderAsync(request); | ||
|
|
||
| Console.WriteLine(response); | ||
| } | ||
| } |
37 changes: 37 additions & 0 deletions
37
...ice-transactions-sample-with-shared-cluster-with-linq/Client/Commands/GetOrdersCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands.Binders; | ||
| using MicroserviceTransactionsSample.Rpc; | ||
| using static MicroserviceTransactionsSample.Rpc.OrderService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| public static class GetOrdersCommand | ||
| { | ||
| private const string Name = "GetOrders"; | ||
| private const string Description = "Get orders information by customer ID"; | ||
|
|
||
| private const string ArgName = "customer_id"; | ||
| private const string ArgDescription = "customer ID"; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var customerIdArg = new Argument<int>(ArgName, ArgDescription); | ||
| var getOrdersCommand = new Command(Name, Description) | ||
| { | ||
| customerIdArg | ||
| }; | ||
|
|
||
| getOrdersCommand.SetHandler(getOrders, | ||
| customerIdArg, | ||
| new OrderServiceClientBinder()); | ||
| return getOrdersCommand; | ||
| } | ||
|
|
||
| private static async Task getOrders(int customerId, OrderServiceClient client) | ||
| { | ||
| var request = new GetOrdersRequest { CustomerId = customerId }; | ||
| var response = await client.GetOrdersAsync(request); | ||
|
|
||
| Console.WriteLine(response); | ||
| } | ||
| } | ||
67 changes: 67 additions & 0 deletions
67
...ce-transactions-sample-with-shared-cluster-with-linq/Client/Commands/PlaceOrderCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands.Binders; | ||
| using MicroserviceTransactionsSample.Rpc; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using static MicroserviceTransactionsSample.Rpc.OrderService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| public static class PlaceOrderCommand | ||
| { | ||
| private const string Name = "PlaceOrder"; | ||
| private const string Description = "Place an order"; | ||
|
|
||
| private const string ArgName1 = "customer_id"; | ||
| private const string ArgDescription1 = "customer ID"; | ||
| private const string ArgName2 = "orders"; | ||
| private const string ArgDescription2 = "orders. The format is \"<Item ID>:<Count>,<Item ID>:<Count>,...\""; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var customerIdArg = new Argument<int>(ArgName1, ArgDescription1); | ||
| var ordersArg = new Argument<Dictionary<int, int>>( | ||
| name: ArgName2, | ||
| parse: arg => | ||
| { | ||
| var argStr = arg.Tokens.First().Value; | ||
| var orders = argStr | ||
| .Split(',') | ||
| .Select(s => s.Split(':')) | ||
| .ToDictionary( | ||
| s => Int32.Parse(s[0]), | ||
| s => Int32.Parse(s[1]) | ||
| ); | ||
|
|
||
| return orders; | ||
| }, | ||
| description: ArgDescription2) | ||
| { | ||
| Arity = ArgumentArity.ExactlyOne | ||
| }; | ||
|
|
||
| var placeOrderCommand = new Command(Name, Description) | ||
| { | ||
| customerIdArg, | ||
| ordersArg | ||
| }; | ||
|
|
||
| placeOrderCommand.SetHandler(placeOrder, | ||
| customerIdArg, | ||
| ordersArg, | ||
| new OrderServiceClientBinder()); | ||
| return placeOrderCommand; | ||
| } | ||
|
|
||
| private static async Task placeOrder(int customerId, | ||
| Dictionary<int, int> orders, | ||
| OrderServiceClient client) | ||
| { | ||
| var request = new PlaceOrderRequest { CustomerId = customerId }; | ||
| foreach (var order in orders) | ||
| request.ItemOrder.Add(new ItemOrder { ItemId = order.Key, Count = order.Value }); | ||
|
|
||
| var response = await client.PlaceOrderAsync(request); | ||
|
|
||
| Console.WriteLine(response); | ||
| } | ||
| } |
42 changes: 42 additions & 0 deletions
42
...ice-transactions-sample-with-shared-cluster-with-linq/Client/Commands/RepaymentCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands.Binders; | ||
| using MicroserviceTransactionsSample.Rpc; | ||
| using static MicroserviceTransactionsSample.Rpc.CustomerService; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| public static class RepaymentCommand | ||
| { | ||
| private const string Name = "Repayment"; | ||
| private const string Description = "Make a repayment"; | ||
|
|
||
| private const string ArgName1 = "customer_id"; | ||
| private const string ArgDescription1 = "customer ID"; | ||
| private const string ArgName2 = "amount"; | ||
| private const string ArgDescription2 = "repayment amount"; | ||
|
|
||
| public static Command Create() | ||
| { | ||
| var customerIdArg = new Argument<int>(ArgName1, ArgDescription1); | ||
| var amountArg = new Argument<int>(ArgName2, ArgDescription2); | ||
| var repaymentCommand = new Command(Name, Description) | ||
| { | ||
| customerIdArg, | ||
| amountArg | ||
| }; | ||
|
|
||
| repaymentCommand.SetHandler(repay, | ||
| customerIdArg, | ||
| amountArg, | ||
| new CustomerServiceClientBinder()); | ||
| return repaymentCommand; | ||
| } | ||
|
|
||
| private static async Task repay(int customerId, int amount, CustomerServiceClient client) | ||
| { | ||
| var request = new RepaymentRequest { CustomerId = customerId, Amount = amount }; | ||
| var response = await client.RepaymentAsync(request); | ||
|
|
||
| Console.WriteLine(response); | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
dotnet/microservice-transactions-sample-with-shared-cluster-with-linq/Client/Logging.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client; | ||
|
|
||
| public static class Logging | ||
| { | ||
| public static ILoggerFactory GetLoggerFactory() | ||
| => LoggerFactory.Create(builder => | ||
| { | ||
| builder.SetMinimumLevel(LogLevel.Warning); | ||
| builder.AddSimpleConsole(options => { options.TimestampFormat = "HH:mm:ss "; }); | ||
| }); | ||
| } |
21 changes: 21 additions & 0 deletions
21
dotnet/microservice-transactions-sample-with-shared-cluster-with-linq/Client/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System.CommandLine; | ||
| using MicroserviceTransactionsSample.Client.Commands; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Client; | ||
|
|
||
| class Program | ||
| { | ||
| static async Task Main(string[] args) | ||
| { | ||
| var rootCommand = new RootCommand("MicroserviceTransactionsSample.Client") | ||
| { | ||
| GetCustomerInfoCommand.Create(), | ||
| GetOrderCommand.Create(), | ||
| GetOrdersCommand.Create(), | ||
| PlaceOrderCommand.Create(), | ||
| RepaymentCommand.Create() | ||
| }; | ||
|
|
||
| await rootCommand.InvokeAsync(args); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
dotnet/microservice-transactions-sample-with-shared-cluster-with-linq/Common/Common.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <RootNamespace>MicroserviceTransactionsSample.Common</RootNamespace> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="ScalarDB.Client" Version="3.13.0"/> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
21 changes: 21 additions & 0 deletions
21
...vice-transactions-sample-with-shared-cluster-with-linq/Common/CustomerService/Customer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| using System.ComponentModel.DataAnnotations.Schema; | ||
| using ScalarDB.Client.DataAnnotations; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Common.CustomerService; | ||
|
|
||
| [Table("customer_service.customers")] | ||
| public class Customer | ||
| { | ||
| [PartitionKey] | ||
| [Column("customer_id", Order = 0)] | ||
| public int Id { get; set; } | ||
|
|
||
| [Column("name", Order = 1)] | ||
| public string Name { get; set; } = ""; | ||
|
|
||
| [Column("credit_limit", Order = 2)] | ||
| public int CreditLimit { get; set; } | ||
|
|
||
| [Column("credit_total", Order = 3)] | ||
| public int CreditTotal { get; set; } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...e-transactions-sample-with-shared-cluster-with-linq/Common/FailedPreconditionException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Grpc.Core; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Common; | ||
|
|
||
| public class FailedPreconditionException : RpcException | ||
| { | ||
| public FailedPreconditionException(string message) | ||
| : base(new Status(StatusCode.FailedPrecondition, message)) | ||
| { } | ||
| } |
10 changes: 10 additions & 0 deletions
10
...icroservice-transactions-sample-with-shared-cluster-with-linq/Common/InternalException.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| using Grpc.Core; | ||
|
|
||
| namespace MicroserviceTransactionsSample.Common; | ||
|
|
||
| public class InternalException : RpcException | ||
| { | ||
| public InternalException(string message, Exception? innerEx) | ||
| : base(new Status(StatusCode.Internal, message, innerEx)) | ||
| { } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.