本地事件的设计与实现
This commit is contained in:
parent
348ea248a6
commit
fb424749c0
@ -5,13 +5,14 @@ using AutoMapper;
|
|||||||
using HelloShop.OrderingService.Entities.Buyers;
|
using HelloShop.OrderingService.Entities.Buyers;
|
||||||
using HelloShop.OrderingService.Entities.Orders;
|
using HelloShop.OrderingService.Entities.Orders;
|
||||||
using HelloShop.OrderingService.Infrastructure;
|
using HelloShop.OrderingService.Infrastructure;
|
||||||
|
using HelloShop.OrderingService.LocalEvents;
|
||||||
using HelloShop.OrderingService.Services;
|
using HelloShop.OrderingService.Services;
|
||||||
using MediatR;
|
using MediatR;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace HelloShop.OrderingService.Commands.Orders
|
namespace HelloShop.OrderingService.Commands.Orders
|
||||||
{
|
{
|
||||||
public class CreateOrderCommandHandler(OrderingServiceDbContext dbContext, IMapper mapper) : IRequestHandler<CreateOrderCommand, bool>
|
public class CreateOrderCommandHandler(IMediator mediator, OrderingServiceDbContext dbContext, IMapper mapper) : IRequestHandler<CreateOrderCommand, bool>
|
||||||
{
|
{
|
||||||
public async Task<bool> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
|
public async Task<bool> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
@ -42,6 +43,8 @@ namespace HelloShop.OrderingService.Commands.Orders
|
|||||||
await dbContext.AddAsync(order, cancellationToken);
|
await dbContext.AddAsync(order, cancellationToken);
|
||||||
await dbContext.SaveChangesAsync(cancellationToken);
|
await dbContext.SaveChangesAsync(cancellationToken);
|
||||||
|
|
||||||
|
await mediator.Publish(new OrderStartedLocalEvent(order), cancellationToken);
|
||||||
|
|
||||||
return await Task.FromResult(true);
|
return await Task.FromResult(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -33,6 +33,8 @@ namespace HelloShop.OrderingService.Extensions
|
|||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddModelMapper().AddModelValidator();
|
builder.Services.AddModelMapper().AddModelValidator();
|
||||||
|
|
||||||
|
builder.Services.AddTransient<ISmsSender, MessageService>().AddTransient<IEmailSender, MessageService>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WebApplication MapApplicationEndpoints(this WebApplication app)
|
public static WebApplication MapApplicationEndpoints(this WebApplication app)
|
||||||
|
@ -0,0 +1,18 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
using HelloShop.OrderingService.Services;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.LocalEvents
|
||||||
|
{
|
||||||
|
public class EmailWhenOrderStartedLocalEventHandler(IEmailSender emailSender) : INotificationHandler<OrderStartedLocalEvent>
|
||||||
|
{
|
||||||
|
public Task Handle(OrderStartedLocalEvent notification, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
emailSender.SendEmailAsync(notification.Order.BuyerId.ToString(), "Order started", $"Your order {notification.Order.Id} has started", cancellationToken);
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
using HelloShop.OrderingService.Entities.Orders;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.LocalEvents
|
||||||
|
{
|
||||||
|
public record class OrderStartedLocalEvent(Order Order) : INotification;
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
using HelloShop.OrderingService.Services;
|
||||||
|
using MediatR;
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.LocalEvents
|
||||||
|
{
|
||||||
|
public class SmsWhenOrderStartedLocalEventHandler(ISmsSender smsSender) : INotificationHandler<OrderStartedLocalEvent>
|
||||||
|
{
|
||||||
|
public Task Handle(OrderStartedLocalEvent notification, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
smsSender.SendSmsAsync(notification.Order.BuyerId.ToString(), $"Your order {notification.Order.Id} has started", cancellationToken);
|
||||||
|
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
src/HelloShop.OrderingService/Services/IEmailSender.cs
Normal file
10
src/HelloShop.OrderingService/Services/IEmailSender.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.Services
|
||||||
|
{
|
||||||
|
public interface IEmailSender
|
||||||
|
{
|
||||||
|
Task SendEmailAsync(string email, string subject, string message, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
}
|
10
src/HelloShop.OrderingService/Services/ISmsSender.cs
Normal file
10
src/HelloShop.OrderingService/Services/ISmsSender.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.Services
|
||||||
|
{
|
||||||
|
public interface ISmsSender
|
||||||
|
{
|
||||||
|
Task SendSmsAsync(string number, string message, CancellationToken cancellationToken = default);
|
||||||
|
}
|
||||||
|
}
|
22
src/HelloShop.OrderingService/Services/MessageService.cs
Normal file
22
src/HelloShop.OrderingService/Services/MessageService.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||||
|
// See the license file in the project root for more information.
|
||||||
|
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace HelloShop.OrderingService.Services
|
||||||
|
{
|
||||||
|
public class MessageService(ILogger<MessageService> logger) : IEmailSender, ISmsSender
|
||||||
|
{
|
||||||
|
public Task SendEmailAsync(string email, string subject, string message, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Sending email to {Email} with subject {Subject} and message {Message}", email, subject, message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task SendSmsAsync(string number, string message, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
logger.LogInformation("Sending SMS to {Number} with message {Message}", number, message);
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user