Quick Start

Last updated: March 16th, 2019

Components

Reducer

Messages broadcast by a message broker often fall into separate classes while each class itself can be further broken down into smaller groups. With respect to the fact that each message class may not necessarily follow the same business for being deserialized and saved in a database, discrete components are required to render message-specific processes. A Reducer is responsible for breaking down received messages followed by distributing them across the corresponding message processors. In order to properly recognize the type of message, a Reducer can be recursively used until the final message type has been identified.

Example of IReducer

public interface IReducer
{
	Task Reduce(MessageContext messageContext);
}
										
Possible Implementation

public class TypicalMessage : IReducer
{
    public IReducer CurrencyReducer { get; }
    public IReducer StockTickerReducer { get; }
    public IProcessor CurrencyHeaderProcessor { get; }
    public IProcessor StockTickerHeaderProcessor { get; }
    public IProcessor TextMessageProcessor { get; }

    public TypicalMessage(IReducer currencyReducer, IReducer stockTickerReducer, IProcessor currencyHeaderProcessor, IProcessor stockTickerHeaderProcessor, IProcessor textMessageProcessor)
    {
        CurrencyReducer = currencyReducer;
        StockTickerReducer = stockTickerReducer;
        CurrencyHeaderProcessor = currencyHeaderProcessor;
        StockTickerHeaderProcessor = stockTickerHeaderProcessor;
        TextMessageProcessor = textMessageProcessor;
    }

    public async Task Reduce(MessageContext messageContext)
    {
        switch (messageContext.Message.Type)
        {
            case 1:
                {
                    await CurrencyHeaderProcessor.Process(messageContext.Message);
                    if (messageContext.CanEnumerate)
                    {
                        await CurrencyReducer.Reduce(messageContext.Enumerate());
                    }
                    break;
                }
            case 2:
                {
                    await StockTickerHeaderProcessor.Process(messageContext.Message);
                    if (messageContext.CanEnumerate)
                    {
                        await StockTickerReducer.Reduce(messageContext.Enumerate());
                    }
                    break;
                }
            case 3: {
                    await TextMessageProcessor.Process(messageContext.Message);
                    break;
                }
        }
    }
}
										

Processor

A Processor is a component which is responsible for deserializing raw messages and eventually concocting data in accordance with particular business requirements.

Example of IProcessor

public interface IProcessor
{
	Task Process(IMessage message);
}
									
Possible Implementation

public class HeaderProcessor : IProcessor
{
	public IAdapter<HeaderMessageModel> HeaderAdapter { get; }
	public async Task Process(IMessage message)
	{
		HeaderMessageModel headerMessageModel = JsonConvert.DeserializeObject(message.Message);
		await HeaderAdapter.Adapt(headerMessageModel);
	}
}
										

Adapter

The adapter is a component of RPA which is responsible for saving processed data in a database.

Example of IAdapter

public interface IAdapter<Entity> where Entity: class, new()
{
	Task Adapt(Entity obj);
}
									
Possible Implementation

public class HeaderAdapter : IAdapter<Header>
{
	public async Task Adapt(Header obj)
	{
		using(DbContext db = new Database()) {
			db.Add<Header>(obj);
			await db.SaveChangesAsync();
		}
	}
}
										

Are you an ambitious and entrepreneurial developer?

Instance Theme

www.pouyavedadiyan.com

Check out my website for more recepies, frameworks, libraries, and a tone of of other awesome things.

View My Website