-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tore Nestenius
committed
Dec 4, 2020
1 parent
44d57ef
commit a0f1213
Showing
20 changed files
with
907 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
# SourceGenerator-MediatR-CQRS | ||
This is the repository for the .NET 5 Source Generators - MediatR - CQRS - OMG! blog post | ||
# .NET 5 Source Generators - MediatR - CQRS - OMG! | ||
|
||
This is the source code repository for the .NET 5 Source Generators - MediatR - CQRS - OMG! blog post [Link] | ||
|
This file contains 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,36 @@ | ||
|
||
Microsoft Visual Studio Solution File, Format Version 12.00 | ||
# Visual Studio Version 16 | ||
VisualStudioVersion = 16.0.30709.64 | ||
MinimumVisualStudioVersion = 10.0.40219.1 | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator-MediatR-CQRS", "SourceGenerator-MediatR-CQRS\SourceGenerator-MediatR-CQRS.csproj", "{7B1BF07F-0E9B-4BE7-A764-83AF081F8D5B}" | ||
EndProject | ||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SourceGenerator", "SourceGenerator\SourceGenerator.csproj", "{11AA9EBB-D2CC-46FC-BB39-4CC3D773C43B}" | ||
EndProject | ||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{21B7559B-3E9B-4F3E-8C32-DC2C5AFB158B}" | ||
ProjectSection(SolutionItems) = preProject | ||
README.md = README.md | ||
EndProjectSection | ||
EndProject | ||
Global | ||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | ||
Debug|Any CPU = Debug|Any CPU | ||
Release|Any CPU = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(ProjectConfigurationPlatforms) = postSolution | ||
{7B1BF07F-0E9B-4BE7-A764-83AF081F8D5B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{7B1BF07F-0E9B-4BE7-A764-83AF081F8D5B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{7B1BF07F-0E9B-4BE7-A764-83AF081F8D5B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{7B1BF07F-0E9B-4BE7-A764-83AF081F8D5B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
{11AA9EBB-D2CC-46FC-BB39-4CC3D773C43B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | ||
{11AA9EBB-D2CC-46FC-BB39-4CC3D773C43B}.Debug|Any CPU.Build.0 = Debug|Any CPU | ||
{11AA9EBB-D2CC-46FC-BB39-4CC3D773C43B}.Release|Any CPU.ActiveCfg = Release|Any CPU | ||
{11AA9EBB-D2CC-46FC-BB39-4CC3D773C43B}.Release|Any CPU.Build.0 = Release|Any CPU | ||
EndGlobalSection | ||
GlobalSection(SolutionProperties) = preSolution | ||
HideSolutionNode = FALSE | ||
EndGlobalSection | ||
GlobalSection(ExtensibilityGlobals) = postSolution | ||
SolutionGuid = {F730E4E6-C15D-476B-A600-96B9D9BB5CFA} | ||
EndGlobalSection | ||
EndGlobal |
39 changes: 39 additions & 0 deletions
39
SourceGenerator-MediatR-CQRS/BusinessLogic/CommandHandlers.cs
This file contains 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,39 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SourceGenerator_MediatR_CQRS | ||
{ | ||
/// <summary> | ||
/// This class will handle the commands that are sent to the system | ||
/// </summary> | ||
public class CommandHandlers : IRequestHandler<CreateOrder, string>, | ||
IRequestHandler<AddProduct, string>, | ||
IRequestHandler<RemoveProduct, string>, | ||
IRequestHandler<CancelOrder, string> | ||
{ | ||
public Task<string> Handle(CreateOrder request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult("Order created"); | ||
} | ||
|
||
public Task<string> Handle(AddProduct request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult("Product added"); | ||
} | ||
|
||
public Task<string> Handle(RemoveProduct request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult("Product removed"); | ||
} | ||
|
||
public Task<string> Handle(CancelOrder request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult("Order cancelled"); | ||
|
||
} | ||
} | ||
} |
This file contains 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,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace SourceGenerator_MediatR_CQRS | ||
{ | ||
public class Order | ||
{ | ||
/// <summary> | ||
/// The Order ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// CustomerID | ||
/// </summary> | ||
/// <example>1234</example> | ||
public int CustomerId { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// OrderLines | ||
/// </summary> | ||
public List<OrderLine> OrderLines { get; set; } | ||
|
||
|
||
/// <summary> | ||
/// The total value of this order | ||
/// </summary> | ||
/// <remarks>This is the total value exclusive VAT</remarks> | ||
/// <example>995.95</example> | ||
public Decimal OrderTotal { get; set; } | ||
} | ||
} |
This file contains 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; | ||
|
||
namespace SourceGenerator_MediatR_CQRS | ||
{ | ||
public class OrderLine | ||
{ | ||
/// <summary> | ||
/// The OrderLine Id | ||
/// </summary> | ||
/// <example>1001</example> | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// The Order ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
public int OrderId { get; set; } | ||
|
||
/// <summary> | ||
/// The Product ID | ||
/// </summary> | ||
/// <example>2048</example> | ||
public int ProductId { get; set; } | ||
|
||
/// <summary> | ||
/// The Quantity of products | ||
/// </summary> | ||
/// <example>10</example> | ||
public int Quantity { get; set; } | ||
|
||
/// <summary> | ||
/// The Quantiy of products | ||
/// </summary> | ||
/// <example>9.95</example> | ||
public Decimal Price { get; set; } | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
SourceGenerator-MediatR-CQRS/BusinessLogic/QueryHandlers.cs
This file contains 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,55 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace SourceGenerator_MediatR_CQRS | ||
{ | ||
/// <summary> | ||
/// This class will handle the queries that are sent to the system | ||
/// </summary> | ||
public class QueryHandlers : IRequestHandler<ListAllOrders, List<Order>>, | ||
IRequestHandler<ListAllOrdersForCustomer, List<Order>>, | ||
IRequestHandler<ListTodaysOrders, List<Order>>, | ||
IRequestHandler<GetOrder, Order> | ||
{ | ||
public Task<List<Order>> Handle(ListAllOrders request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new List<Order>() { | ||
new Order() { CustomerId = 1234, Id = 2201, OrderTotal = 9.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1235, Id = 2201, OrderTotal = 29.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1236, Id = 2201, OrderTotal = 1219.95m, OrderLines = new List<OrderLine>() } | ||
}); | ||
} | ||
|
||
public Task<List<Order>> Handle(ListAllOrdersForCustomer request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new List<Order>() { | ||
new Order() { CustomerId = 1235, Id = 2201, OrderTotal = 9.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1236, Id = 2201, OrderTotal = 29.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1237, Id = 2201, OrderTotal = 100.0m, OrderLines = new List<OrderLine>() } | ||
}); | ||
} | ||
|
||
public Task<List<Order>> Handle(ListTodaysOrders request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new List<Order>() { | ||
new Order() { CustomerId = 1231, Id = 2201, OrderTotal = 92.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1232, Id = 2201, OrderTotal = 591.95m, OrderLines = new List<OrderLine>() }, | ||
new Order() { CustomerId = 1233, Id = 2201, OrderTotal = 192.95m, OrderLines = new List<OrderLine>() } | ||
}); | ||
} | ||
|
||
public Task<Order> Handle(GetOrder request, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new Order() | ||
{ | ||
CustomerId = 1234, | ||
Id = 2201, | ||
OrderTotal = 9.95m, | ||
OrderLines = new List<OrderLine>() | ||
}); | ||
} | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
SourceGenerator-MediatR-CQRS/CommandAndQueries/Commands.cs
This file contains 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,125 @@ | ||
using MediatR; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace SourceGenerator_MediatR_CQRS | ||
{ | ||
/// <summary> | ||
/// Interface that represents a command to the system | ||
/// </summary> | ||
public interface ICommand<T> : IRequest<T> | ||
{ | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Create a new order | ||
/// </summary> | ||
/// <remarks> | ||
/// Send this command to create a new order in the system for a given customer | ||
/// </remarks> | ||
public record CreateOrder : ICommand<string> | ||
{ | ||
/// <summary> | ||
/// OrderId | ||
/// </summary> | ||
/// <remarks>This is the customers internal ID of the order.</remarks> | ||
/// <example>123</example> | ||
[Required] | ||
public int Id { get; set; } | ||
|
||
/// <summary> | ||
/// CustomerID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int CustomerId { get; set; } | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Add a new product to existing order | ||
/// </summary> | ||
/// <remarks> | ||
/// Send this command to add a new product to existing order. The order must still be open for this command to be accepted. | ||
/// </remarks> | ||
public record AddProduct : ICommand<string> | ||
{ | ||
/// <summary> | ||
/// The Order ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int OrderId { get; set; } | ||
|
||
/// <summary> | ||
/// The Product ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int ProductId { get; set; } | ||
|
||
/// <summary> | ||
/// The Quantity of products to add | ||
/// </summary> | ||
/// <example>10</example> | ||
[Required] | ||
public int Quantity { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Remove product from order | ||
/// </summary> | ||
/// <remarks> | ||
/// Removes the product from the provided order. The order must still be open for this command to be accepted. | ||
/// </remarks> | ||
public record RemoveProduct : ICommand<string> | ||
{ | ||
/// <summary> | ||
/// The Order ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int OrderId { get; set; } | ||
|
||
/// <summary> | ||
/// The Product ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int ProductId { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Cancels an order | ||
/// </summary> | ||
/// <remarks> | ||
/// This command will cancel the order. The order must still be open for this command to be accepted. | ||
/// </remarks> | ||
public record CancelOrder : ICommand<string> | ||
{ | ||
/// <summary> | ||
/// The Order ID | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public int OrderId { get; set; } | ||
|
||
/// <summary> | ||
/// The reason for why this order is cancelled | ||
/// </summary> | ||
/// <example>1234</example> | ||
[Required] | ||
public string Reason { get; set; } | ||
|
||
/// <summary> | ||
/// Who cancelled this order? | ||
/// </summary> | ||
/// <example>Tore Nestenius</example> | ||
[Required] | ||
public string CancelledBy { get; set; } | ||
} | ||
} |
Oops, something went wrong.