Skip to content

Commit 9c0c2d6

Browse files
committed
imported all message types from shared project
1 parent e0e5517 commit 9c0c2d6

24 files changed

+1340
-26
lines changed

src/clustering/sharding-sqlserver/SqlSharding.Shared/SqlSharding.Shared.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
<ItemGroup>
1010
<PackageReference Include="Akka.Cluster.Sharding" Version="$(AkkaVersion)" />
1111
<PackageReference Include="Akka.Cluster.Hosting" Version="$(AkkaHostingVersion)" />
12-
<PackageReference Include="Grpc.AspNetCore" Version="2.61.0" />
12+
<PackageReference Include="Grpc.Tools" Version="2.62.0">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
1316
<PackageReference Include="Bogus" Version="35.5.0" />
1417
</ItemGroup>
1518

src/cqrs/cqrs-sqlserver/src/CqrsSqlServer.Backend/CqrsSqlServer.Backend.csproj src/cqrs/cqrs-sqlserver/CqrsSqlServer.Backend/CqrsSqlServer.Backend.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
</ItemGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="Akka.Cluster.Hosting" Version="1.5.18"/>
1615
<PackageReference Include="Akka.Persistence.SqlServer.Hosting" Version="1.5.15"/>
1716
<PackageReference Include="Petabridge.Cmd.Cluster" Version="$(PbmVersion)"/>
1817
<PackageReference Include="Petabridge.Cmd.Cluster.Sharding" Version="$(PbmVersion)"/>
1918
<PackageReference Include="Petabridge.Cmd.Host" Version="$(PbmVersion)"/>
2019
</ItemGroup>
2120

21+
<ItemGroup>
22+
<Protobuf Include="Serialization\Proto\SqlSharding.Messages.proto" GrpcServices="None" />
23+
</ItemGroup>
24+
2225
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using CqrsSqlServer.Shared.Events;
2+
3+
namespace CqrsSqlServer.Shared.Commands;
4+
5+
/// <summary>
6+
/// Used to distinguish product events from commands
7+
/// </summary>
8+
public interface IProductCommand : IWithProductId{}
9+
10+
public record CreateProduct(string ProductId, string ProductName, decimal Price, int InitialQuantity, string[] Tags) : IProductCommand;
11+
12+
public record SupplyProduct(string ProductId, int AdditionalQuantity) : IProductCommand;
13+
14+
public record PurchaseProduct(ProductOrder NewOrder) : IProductCommand, IWithOrderId
15+
{
16+
public string ProductId => NewOrder.ProductId;
17+
public string OrderId => NewOrder.OrderId;
18+
}
19+
20+
public record ProductCommandResponse(string ProductId, IReadOnlyCollection<IProductEvent> ResponseEvents, bool Success = true, string Message = "") : IWithProductId;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Akka.Cluster.Sharding" Version="$(AkkaVersion)"/>
11+
<PackageReference Include="Akka.Cluster.Hosting" Version="$(AkkaHostingVersion)"/>
12+
<PackageReference Include="Grpc.Tools" Version="2.62.0">
13+
<PrivateAssets>all</PrivateAssets>
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
</PackageReference>
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<Protobuf Include="Serialization\Proto\CqrsSqlServer.Messages.proto" GrpcServices="None" />
20+
</ItemGroup>
21+
22+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
namespace CqrsSqlServer.Shared.Events;
2+
3+
/// <summary>
4+
/// Used to distinguish product events from commands
5+
/// </summary>
6+
public interface IProductEvent : IWithProductId
7+
{
8+
}
9+
10+
public record ProductCreated(string ProductId, string ProductName, decimal Price) : IProductEvent;
11+
12+
public record ProductSold(ProductOrder Order, decimal UnitPrice, bool BackOrdered = false) : IProductEvent,
13+
IWithOrderId, IComparable<ProductSold>
14+
{
15+
public string ProductId => Order.ProductId;
16+
17+
public string OrderId => Order.OrderId;
18+
19+
public decimal TotalPrice => Order.Quantity * UnitPrice;
20+
21+
public int CompareTo(ProductSold? other)
22+
{
23+
if (other == null) return 1;
24+
25+
return Order.Timestamp.CompareTo(other.Order.Timestamp);
26+
}
27+
}
28+
29+
public enum InventoryChangeReason
30+
{
31+
Fulfillment,
32+
SupplyIncrease,
33+
34+
/// <summary>
35+
/// i.e. Theft or spoilage
36+
/// </summary>
37+
Lost
38+
}
39+
40+
public record ProductInventoryChanged(string ProductId, int Quantity, DateTime Timestamp,
41+
InventoryChangeReason Reason = InventoryChangeReason.Fulfillment) : IProductEvent,
42+
IComparable<ProductInventoryChanged>
43+
{
44+
public int CompareTo(ProductInventoryChanged? other)
45+
{
46+
if (ReferenceEquals(this, other)) return 0;
47+
if (ReferenceEquals(null, other)) return 1;
48+
return Timestamp.CompareTo(other.Timestamp);
49+
}
50+
}
51+
52+
public enum ProductWarningReason
53+
{
54+
/// <summary>
55+
/// Warning once inventory is running low.
56+
/// </summary>
57+
LowSupply,
58+
NoSupply
59+
}
60+
61+
public record ProductInventoryWarningEvent(string ProductId, ProductWarningReason Reason, DateTime Timestamp,
62+
string Message) : IProductEvent, IComparable<ProductInventoryWarningEvent>
63+
{
64+
public int CompareTo(ProductInventoryWarningEvent? other)
65+
{
66+
if (ReferenceEquals(this, other)) return 0;
67+
return ReferenceEquals(null, other) ? 1 : Timestamp.CompareTo(other.Timestamp);
68+
}
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace CqrsSqlServer.Shared;
2+
3+
/// <summary>
4+
/// Marker interface for all events / apps / state in the domain.
5+
///
6+
/// Used by Akka.NET to select the correct serializer.
7+
/// </summary>
8+
public interface ISqlShardingProtocolMember
9+
{
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace CqrsSqlServer.Shared;
2+
3+
public interface IWithOrderId
4+
{
5+
string OrderId { get; }
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace CqrsSqlServer.Shared;
2+
3+
/// <summary>
4+
/// Marker interface for all commands and events associated with a product.
5+
/// </summary>
6+
public interface IWithProductId : ISqlShardingProtocolMember
7+
{
8+
string ProductId { get; }
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using Akka.Persistence.Journal;
2+
using CqrsSqlServer.Shared.Events;
3+
4+
namespace CqrsSqlServer.Shared;
5+
6+
public class MessageTagger : IWriteEventAdapter
7+
{
8+
public const string ChangedEventTag = "Changed";
9+
public const string SoldEventTag = "Sold";
10+
public const string WarningEventTag = "Warning";
11+
12+
public string Manifest(object evt)
13+
{
14+
return string.Empty;
15+
}
16+
17+
public object ToJournal(object evt)
18+
{
19+
return evt switch
20+
{
21+
ProductInventoryChanged pic => new Tagged(pic, new[] { ChangedEventTag, pic.Reason.ToString() }),
22+
ProductSold sold => new Tagged(sold, new[] { SoldEventTag }),
23+
ProductInventoryWarningEvent warning => new Tagged(warning, new [] { WarningEventTag }),
24+
_ => evt
25+
};
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace CqrsSqlServer.Shared;
2+
3+
/// <summary>
4+
/// Datatype used to encapsulate order values.
5+
/// </summary>
6+
public sealed record ProductOrder
7+
(string OrderId, string ProductId, int Quantity, DateTime Timestamp) : IWithProductId, IWithOrderId;

0 commit comments

Comments
 (0)