View AddCommentHandler.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
public class AddCommentHandler : IHandleMessages<AddComment> | |
{ | |
public async Task Handle(AddComment message, IMessageHandlerContext context) | |
{ | |
var cosmosSession = context.SynchronizedStorageSession.CosmosPersistenceSession(); | |
var postResource = await cosmosSession.Container.ReadItemAsync<Post>(message.PostId, | |
new PartitionKey(message.PostId)); | |
if (postResource == null) | |
{ |
View AddPostHandler.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
public class AddPostHandler : IHandleMessages<AddPost> | |
{ | |
public async Task Handle(AddPost message, IMessageHandlerContext context) | |
{ | |
var cosmosSession = context.SynchronizedStorageSession.CosmosPersistenceSession(); | |
var post = new Post(message.PostId, message.Title, message.Description, message.Author); | |
cosmosSession.Batch.CreateItem(post); | |
await context.Publish(new PostCreated | |
{ | |
PostId = post.PostId, |
View 1_Post.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
public class Post | |
{ | |
public Post(string postId, | |
string title, | |
string description, | |
string author) | |
{ | |
Id = postId; | |
PostId = postId; | |
Title = title; |
View Program.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
await Host.CreateDefaultBuilder(args) | |
.UseNServiceBus((_ => | |
{ | |
var endpointConfiguration = new EndpointConfiguration("Samples.Worker"); | |
var transport = endpointConfiguration.UseTransport<LearningTransport>(); | |
transport.Transactions(TransportTransactionMode.SendsAtomicWithReceive); | |
endpointConfiguration.UsePersistence<CosmosPersistence>() | |
// Using Cosmos emulator | |
.CosmosClient(new CosmosClient( |
View PostIdAsPartitionKeyBehaviour.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
public class PostIdAsPartitionKeyBehavior: Behavior<IIncomingLogicalMessageContext> | |
{ | |
public override Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next) | |
{ | |
if (context.Message.Instance is IPostIdPartitionKey partitionKey) | |
{ | |
var partitionKeyValue = partitionKey.PostId; | |
context.Extensions.Set(new PartitionKey(partitionKeyValue)); | |
return next(); |
View 1_IPostIdPartitionKey.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
public interface IPostIdPartitionKey | |
{ | |
string PostId { get; set; } | |
} |
View Program.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
app.MapPost("/create", async (IMessageSession messageSession, [FromBody] Post post) => | |
{ | |
var addPostCommand = new Messages.AddPost(post.Title, post.Description, post.Author); | |
await messageSession.Send(addPostCommand); | |
return Results.Accepted(null, new { addPostCommand.PostId }); | |
}); | |
app.MapPost("/add-comment", async (IMessageSession messageSession, [FromBody] Comment comment) => | |
{ | |
var addCommentCommand = new Messages.AddComment(comment.PostId, comment.Content, comment.CommentBy); |
View Program.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
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddSwaggerGen(options => | |
{ | |
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Posts Api", Version = "v1" }); | |
}); | |
var app = builder.Build(); | |
. | |
. | |
app.UseHttpsRedirection() |
View Program.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
var builder = WebApplication.CreateBuilder(args); | |
builder.Host.UseNServiceBus(_ => | |
{ | |
var endpointConfiguration = new EndpointConfiguration("Samples.Api"); | |
var transport = endpointConfiguration.UseTransport<LearningTransport>(); | |
var routing = transport.Routing(); | |
routing.RouteToEndpoint(Assembly.Load("Messages"), "Samples.Worker"); | |
endpointConfiguration.SendOnly(); | |
return endpointConfiguration; | |
}); |
View SwitchExpressionTest.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
using System; | |
Console.WriteLine("GetBoolean 1:" + (GetBoolean1("Nah") == null)); | |
Console.WriteLine("GetBoolean 2:" + (GetBoolean2("Nah") == null)); | |
static Boolean? GetBoolean2(string boolString) | |
{ | |
return boolString switch | |
{ |
NewerOlder