Flexbase
  • Flexbase
  • Introduction
  • #JoyofCoding with Flexbase
  • Core Concepts
    • Flexbase
    • Flexstudio
      • Commands & Events
  • Thinking in Flexbase
    • Features and Modules
    • Designing APIs using Flexbase Control Flow
  • Sample Application
  • Solution Structure
    • Getting Started
      • Application
        • Introduction to Solution
        • Demo - Solution Generation
        • Solution Structure Overview
        • Architecture Overview
        • Clean architecture layer
        • Common Configurations
      • Features
        • Understanding Feature
        • Use Case
        • First Feature - Add Customer
        • Second Feature - Update Customer
        • Third Feature - Delete Customer
        • Query Features
  • Input Output Models
  • Messages
  • Domain
  • Pre Bus
  • Post Bus
  • Subscribers
  • Bus Gamma
  • Persistence
  • Hosting
  • Startup Code - WebAPI
  • Startup Code - Background Job
  • Endpoint - AppSettings
  • Bridge
  • Basic CRUD - Insert & Update
  • Introduction
  • View Generated API Definition in Swagger
  • Walkthrough Generated Code Insert
  • Walkthrough Generated Code Query GetById
  • Domain Model
  • Migration
  • AppSettings
  • Bus : How Basic Routing Config Works
  • INSERT : Input API Model
  • INSERT : Attribute Validation
  • INSERT : Plugin Validation
  • INSERT : Mapper
  • INSERT : Controller
  • INSERT : Services
  • INSERT : PostBus Handler
  • INSERT : PostBus Subscriber
  • INSERT : Demo In Action
  • QUERY : Output API Model
  • QUERY : Build and Fetch Pattern
  • QUERY : Demo In Action
  • Basic CRUD 2
    • Introduction
    • View Generated API Definition In Swagger
  • Walkthrough Generated Code Update
  • Walkthrough Generated Code Delete
  • Walkthrough Generated Code GetList
  • Walkthrough Generated Code Get Paged List
  • Update In Action
  • Delete In Action
  • GetList In Action
  • GetPagedList In Action
  • Konarch
Powered by GitBook
On this page

INSERT : PostBus Subscriber

BasicCRUD Insert Query with Bridge

PreviousINSERT : PostBus HandlerNextINSERT : Demo In Action

Last updated 4 years ago

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