Calling an event when triggering an API

Viewed 79

I created a notification Event that I need to trigger within my post API after its completion.

The event Args are:

public class OrderCreationEvents : EventArgs
{
   public string PickUpLocation { get; set; }
   public string CustumerId { get; set; }

   public OrderCreationEvents(string _pickUpLocation, string _custumerId)
   {
       PickUpLocation = _pickUpLocation;
       CustumerId = _custumerId;
   }
}

The notification Service interface is:

public interface IOrderCreatedService
{
    void OnOrderCreation(object sender, OrderCreationEvents args);
}

The Interface's implementation is:

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();
    }
}

Finally, when I create the post endpoint, I need to trigger this event

[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");
}

How do I trigger the OnOrderCreation mehtod inside the Create endpoint?

2 Answers

You also can follow Notification with MediatR which is really easy to use and understand.

This Link Can help to implement MediatR notification in your controller.

but, if you want to use MediatR notification in your scenario follow this:

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();
    }
}

and your controller :

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");
}

Remember before this, you need to install and setup the packages:

Assuming you have created an ASP.Net Core project in Visual Studio, the next step is installing the following NuGet packages.

  1. MediatR
  2. MediatR.Extensions.Microsoft.DependencyInjection

To do that, you can either use the NuGet Package Manager or the NuGet Package Manager Console.

now Configure MediatR in ASP.Net Core

Once the two packages mentioned in the earlier section have been successfully installed in your project, the next step is to configure MediatR in the Startup class. To do this, you should write the following code in the ConfigureServices method. Note that the ConfigureServices method is used to add services at runtime to the container.

 public void ConfigureServices(IServiceCollection services)
 {
  services.AddMediatR(typeof(Startup));
  services.AddMvc().SetCompatibilityVersion
  (CompatibilityVersion.Version_2_2);
 }

I not 100% sure if i got what you want. but i think you do not want to use events. simply you can call the OnOrderCreation method like this:

var note = new OrderNotificationService(_unitOfWork);
await note.OnOrderCreation(this, new OrderCreationEvents {.....})
Related