SendΒΆ
Send()
is an IHandler
extension method. It will traverse the context and stop after the first handler that successfully handles the message. If the message is not handled it will throw a “not handled” exception.
Here is an example of an Asp.net Web Api Controller. Context implements IHandler. We call Send on the context passing in the CreateTeam request. Mediator will look through the handlers in the context to find one that can handle the CreateTeam message and return a TeamResult.
namespace Example.Team
{
using System.Threading.Tasks;
using System.Web.Http;
using League.Api.Team;
using Miruken.Context;
using Miruken.Mediate;
public class TeamController : ApiController
{
public IContext Context { get; set; }
[HttpPost]
public async Task<Team> CreateTeam(CreateTeam request)
{
var result = await Context.Send(request);
return result.Team;
}
[HttpDelete]
public async Task RemoveTeam(RemoveTeam request)
{
await Context.Send(request);
}
}
}
namespace Example.League.Api.Team
{
using Miruken.Mediate.Api;
public class CreateTeam : IRequest<TeamResult>
{
public Team Team { get; set; }
}
}
namespace Example.League.Api.Team
{
public class RemoveTeam
{
public Team Team { get; set; }
}
}
namespace Example.League.Api.Team
{
public class TeamResult
{
public Team Team { get; set; }
}
}
Send does not require a response. Notice that the RemoveTeam request does not implement IRequest<TResponse>
and the handler does not return a response.
If the request does not implement IRequest<TResponse>
and the handler returns a response, you can pass in the expected return type when you call send as demostrated below.
namespace Example.WithPureClasses
{
using System.Threading.Tasks;
using System.Web.Http;
using League.Api.Team;
using Miruken.Context;
using Miruken.Mediate;
public class TeamController : ApiController
{
public Context Context { get; set; }
[HttpPost]
public async Task<Team> CreateTeam(CreateTeam request)
{
var result = await Context.Send<TeamResult>(request);
return result.Team;
}
}
}