INSERT : PostBus Subscriber

BasicCRUD Insert Query with Bridge

Now we will look at the subscriber, will go back to our solution. If you notice, we have the Handler executed and the Handler raises the event called CustomerAdded.

So if we go to the endpoint, we have our subscriber configured to listen to the Handler End point. Whatever events are being fired from the Handler will be executed by the subscriber and the subscriber has the bus gamma default subscriber. This is configured in the common messages That all the subscriber interfaces will be available in the messages, which is used commonly across all the other Solutions.

public class SendEmailToCustomerOnAddedBusGammaSubscriber : IAmBusGammaSubscriber<CustomerAdded>
    {
        ILogger<SendEmailToCustomerOnAddedBusGammaSubscriber> _logger;
        ISendEmailToCustomerOnAdded _subscriber;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        public SendEmailToCustomerOnAddedBusGammaSubscriber(ILogger<SendEmailToCustomerOnAddedBusGammaSubscriber> logger, ISendEmailToCustomerOnAdded subscriber)
        {
            _logger = logger;
            _subscriber = subscriber;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="message"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public async Task Handle(CustomerAdded message, IMessageHandlerContext context)
        {
            await _subscriber.Execute(message, new BusGammaHandlerContextBridge(context, message.HostContextInfo));
        }
    }

Bus gamma subscriber receives the event and the subscriber's execute method is called where the way it is written and it is like this because again we are decoupling from the bus and the logic relies with the subscriber file, this can be swapped with any other bus implementation.

public async Task Execute(CustomerAdded @event, IFlexServiceBusContext serviceBusContext)
        {
            //TODO: Write your business logic here:

            _logger.LogInformation(@event.Id);

            //TODO: Specify your condition to raise event here...
            //TODO: Set the value of OnRaiseEventCondition according to your business logic

            OnRaiseEventCondition = CONDITION_ONSUCCESS;

            RaiseEventCondition raiseEventCondition = new RaiseEventCondition(OnRaiseEventCondition);
            await raiseEventCondition.RaiseEvent<SendEmailToCustomerOnAdded>(this, new object[] { serviceBusContext });
        }
private async Task OnSuccess(IFlexServiceBusContextBridge serviceBusContext)
        {

            EmailSentToCustomerOnAdded @event = new EmailSentToCustomerOnAdded
                {
                    HostContextInfo=serviceBusContext.HostContextInfo,

                    //Add your properties here
                };
                    
            await serviceBusContext.Publish(@event);
        }

Last updated