Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

Packet Handler Decorators #37

Open
snarlynarwhal opened this issue Mar 18, 2019 · 3 comments
Open

Packet Handler Decorators #37

snarlynarwhal opened this issue Mar 18, 2019 · 3 comments
Assignees
Milestone

Comments

@snarlynarwhal
Copy link
Contributor

snarlynarwhal commented Mar 18, 2019

What do you think about adding built-in support for Packet Handler Decorators?

We could use an Attribute-based means of assigning Decorators to Packet Handlers and wire them up at startup.

This blog talks about the Command Handler Pattern and has a section about enhancing the pattern with Decorators:

https://blogs.cuttingedge.it/steven/posts/2011/meanwhile-on-the-command-side-of-my-architecture/

The article provides these use case examples:

  • Checking the authorization of the current user before commands get executed
  • Validating commands before commands get executed
  • Measuring the duration of executing commands
  • Logging and audit trailing
  • Executing commands in the background
  • Queuing commands to be processed in a different process

Here's an example of how you might define custom Decorators for a Handler:

[Decorate(
typeof(LoggingHandlerDecorator),
typeof(AuthorizationHandlerDecorator),
typeof(BackgroundJobHandlerDecorator)]
public class SomePacketHandler : PacketHandlerBase<SomePacket>
{
    ....
}

Having a way to define global or module Decorators could prevent having to add the Decorate attribute to so many Handler classes.

Here's an example of the sort of benefits that Decorators can provide, especially when used with helper Attributes:

[Authorization(30)] // User must have authorization level of 30 for this to succeed
public override Task Process(SomePacket packet, IPacketContext context)
{
    ...
}

For this example, the AuthorizationHandlerDecorator.Process method might look something like this:

public override async Task Process(SomePacket packet, IPacketContext context)
{
    int userAccessLevel = 0; // Get user access level from service
    // Gets from the method's Authorization Attribute
    int requiredAccessLevel = GetRequiredAccessLevel(packet);
    if (userAccessLevel >= requiredAccessLevel) 
    {
        await DecoratedHandler.Process(packet, context);
    }
}

Thoughts?

@markiodev markiodev self-assigned this Mar 18, 2019
@markiodev
Copy link
Owner

Already got some ideas for registering middleware, I'll have a go at this one and share around some docs.

@markiodev markiodev added this to the 3.1 milestone Mar 18, 2019
@markiodev
Copy link
Owner

Moving this to 3.1

@markiodev markiodev modified the milestones: 3.0, 3.1 Mar 22, 2019
@snarlynarwhal
Copy link
Contributor Author

I made PR #40 let me know what you think

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants