HandlerΒΆ

To handle requests create a class that extends from Handler. Then create a method on that class with the [Mediates] attribute that takes the request object as the first parameter and returns the response object.

For example, here in TeamHandler.cs we have a method called CreateTeam that takes in a CreateTeam object and returns a TeamResult.

namespace Example.Handler.WithHandlerBaseClass
{
    using League.Api.Team;
    using Miruken.Callback;
    using Miruken.Mediate;

    public class TeamHandler : Handler
    {
        [Mediates]
        public TeamResult CreateTeam(CreateTeam request)
        {
            return new TeamResult
            {
                Team = request.Team
            };
        }

        [Mediates]
        public void RemoveTeam(RemoveTeam request)
        {
            //remove team
        }
    }
}

It is optimal that your handler classes inherit from Handler but it is not required. If they do not inherit from Handler or implement IHandler, Miruken will wrap them in a handler inside the framwork.

namespace Example.Handler.WithoutInheritance
{
    using League.Api.Team;
    using Miruken.Mediate;

    public class TeamHandler
    {
        [Mediates]
        public TeamResult CreateTeam(CreateTeam request)
        {
            return new TeamResult
            {
                Team = request.Team
            };
        }
    }
}

Optionally, the [Mediates] method can take an IHandler as the second parameter. We call this the composer. The composer is for code composition. Use it within your method to send other messages or publish notifications.

namespace Example.Handler.WithComposer
{
    using League.Api.Team;
    using Miruken.Callback;
    using Miruken.Mediate;

    public class TeamHandler
    {
        [Mediates]
        public TeamResult CreateTeam(CreateTeam request, IHandler composer)
        {
            var team = request.Team;

            composer.Publish(new TeamCreated());

            return new TeamResult
            {
                Team = team
            };
        }
    }
}