触发 API 时调用事件
Calling an event when triggering an API
我创建了一个通知事件,我需要在 post API 完成后触发它。
事件参数是:
public class OrderCreationEvents : EventArgs
{
public string PickUpLocation { get; set; }
public string CustumerId { get; set; }
public OrderCreationEvents(string _pickUpLocation, string _custumerId)
{
PickUpLocation = _pickUpLocation;
CustumerId = _custumerId;
}
}
通知服务接口为:
public interface IOrderCreatedService
{
void OnOrderCreation(object sender, OrderCreationEvents args);
}
接口的实现是:
public class OrderNotificationService : IOrderCreatedService
{
private readonly IUnitOfWork _unitOfWork;
public OrderNotificationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async void OnOrderCreation(object sender, OrderCreationEvents args)
{
var note = new Notification { CustomerId = args.CustumerId, PickUpLocation = args.PickUpLocation };
await _unitOfWork.Notifications.Insert(note);
await _unitOfWork.Save();
}
}
最后,当我创建post端点时,我需要触发这个事件
[HttpPost]
public async Task<IActionResult> Create(OrderIDTO orderIDTO)
{
var order = new Order {...};
await _unitOfWork.Orders.Insert(order);
//Calling the Notification Service
var note = new OrderNotificationService(_unitOfWork);
return RedirectToAction("index");
}
如何在 Create 端点内触发 OnOrderCreation 方法?
您还可以在 Notification 之后使用 MediatR,这非常易于使用和理解。
这 Link 可以帮助在您的控制器中实现 MediatR 通知。
但是,如果您想在您的场景中使用 MediatR 通知,请遵循以下步骤:
public class OrderNotification : IAsyncNotification
{
public string PickUpLocation { get; set; }
public string CustumerId { get; set; }
}
public class OrderNotificationService : IAsyncNotificationHandler<OrderNotification>
{
private readonly IUnitOfWork _unitOfWork;
public OrderNotificationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task Handle(OrderNotification notification)
{
var note = new Notification { CustomerId = notification.CustumerId, PickUpLocation = notification.PickUpLocation };
await _unitOfWork.Notifications.Insert(note);
await _unitOfWork.Save();
}
}
和您的控制器:
private IMediator _mediator;
public constructor(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> Create(OrderIDTO orderIDTO)
{
var order = new Order { ...};
await _unitOfWork.Orders.Insert(order);
var note = new OrderNotification
{
CustomerId = order.CustomerId,
PickUpLocation = order.PickUpLocation
};
await _mediator.PublishAsync(note);
return RedirectToAction("index");
}
请记住,在此之前,您需要安装和设置软件包:
假设您已经在 Visual Studio 中创建了一个 ASP.Net 核心项目,下一步是安装以下 NuGet 包。
MediatR
MediatR.Extensions.Microsoft.DependencyInjection
为此,您可以使用 NuGet Package Manager
或 NuGet Package Manager Console
。
现在在 ASP.Net 核心中配置 MediatR
在您的项目中成功安装上一节中提到的两个包后,下一步是在 Startup class 中配置 MediatR。为此,您应该在 ConfigureServices 方法中编写以下代码。请注意,ConfigureServices 方法用于在运行时向容器添加服务。
public void ConfigureServices(IServiceCollection services)
{
services.AddMediatR(typeof(Startup));
services.AddMvc().SetCompatibilityVersion
(CompatibilityVersion.Version_2_2);
}
我不是 100% 确定我是否得到了你想要的。但我认为你不想使用事件。您可以像这样简单地调用 OnOrderCreation 方法:
var note = new OrderNotificationService(_unitOfWork);
await note.OnOrderCreation(this, new OrderCreationEvents {.....})
我创建了一个通知事件,我需要在 post API 完成后触发它。
事件参数是:
public class OrderCreationEvents : EventArgs
{
public string PickUpLocation { get; set; }
public string CustumerId { get; set; }
public OrderCreationEvents(string _pickUpLocation, string _custumerId)
{
PickUpLocation = _pickUpLocation;
CustumerId = _custumerId;
}
}
通知服务接口为:
public interface IOrderCreatedService
{
void OnOrderCreation(object sender, OrderCreationEvents args);
}
接口的实现是:
public class OrderNotificationService : IOrderCreatedService
{
private readonly IUnitOfWork _unitOfWork;
public OrderNotificationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async void OnOrderCreation(object sender, OrderCreationEvents args)
{
var note = new Notification { CustomerId = args.CustumerId, PickUpLocation = args.PickUpLocation };
await _unitOfWork.Notifications.Insert(note);
await _unitOfWork.Save();
}
}
最后,当我创建post端点时,我需要触发这个事件
[HttpPost]
public async Task<IActionResult> Create(OrderIDTO orderIDTO)
{
var order = new Order {...};
await _unitOfWork.Orders.Insert(order);
//Calling the Notification Service
var note = new OrderNotificationService(_unitOfWork);
return RedirectToAction("index");
}
如何在 Create 端点内触发 OnOrderCreation 方法?
您还可以在 Notification 之后使用 MediatR,这非常易于使用和理解。
这 Link 可以帮助在您的控制器中实现 MediatR 通知。
但是,如果您想在您的场景中使用 MediatR 通知,请遵循以下步骤:
public class OrderNotification : IAsyncNotification
{
public string PickUpLocation { get; set; }
public string CustumerId { get; set; }
}
public class OrderNotificationService : IAsyncNotificationHandler<OrderNotification>
{
private readonly IUnitOfWork _unitOfWork;
public OrderNotificationService(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public async Task Handle(OrderNotification notification)
{
var note = new Notification { CustomerId = notification.CustumerId, PickUpLocation = notification.PickUpLocation };
await _unitOfWork.Notifications.Insert(note);
await _unitOfWork.Save();
}
}
和您的控制器:
private IMediator _mediator;
public constructor(IMediator mediator)
{
_mediator = mediator;
}
[HttpPost]
public async Task<IActionResult> Create(OrderIDTO orderIDTO)
{
var order = new Order { ...};
await _unitOfWork.Orders.Insert(order);
var note = new OrderNotification
{
CustomerId = order.CustomerId,
PickUpLocation = order.PickUpLocation
};
await _mediator.PublishAsync(note);
return RedirectToAction("index");
}
请记住,在此之前,您需要安装和设置软件包:
假设您已经在 Visual Studio 中创建了一个 ASP.Net 核心项目,下一步是安装以下 NuGet 包。
MediatR
MediatR.Extensions.Microsoft.DependencyInjection
为此,您可以使用 NuGet Package Manager
或 NuGet Package Manager Console
。
现在在 ASP.Net 核心中配置 MediatR
在您的项目中成功安装上一节中提到的两个包后,下一步是在 Startup class 中配置 MediatR。为此,您应该在 ConfigureServices 方法中编写以下代码。请注意,ConfigureServices 方法用于在运行时向容器添加服务。
public void ConfigureServices(IServiceCollection services)
{
services.AddMediatR(typeof(Startup));
services.AddMvc().SetCompatibilityVersion
(CompatibilityVersion.Version_2_2);
}
我不是 100% 确定我是否得到了你想要的。但我认为你不想使用事件。您可以像这样简单地调用 OnOrderCreation 方法:
var note = new OrderNotificationService(_unitOfWork);
await note.OnOrderCreation(this, new OrderCreationEvents {.....})