dotnet-cqrs/PoweredSoft.CQRS.AspNetCore/Mvc/CommandControllerConvention.cs

29 lines
1.1 KiB
C#
Raw Normal View History

2021-02-02 12:19:59 -05:00
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.DependencyInjection;
using PoweredSoft.CQRS.Abstractions.Discovery;
using System;
namespace PoweredSoft.CQRS.AspNetCore.Mvc
{
2021-02-02 19:01:29 -05:00
public class CommandControllerConvention : IControllerModelConvention
2021-02-02 12:19:59 -05:00
{
private readonly IServiceProvider serviceProvider;
2021-02-02 19:01:29 -05:00
public CommandControllerConvention(IServiceProvider serviceProvider)
2021-02-02 12:19:59 -05:00
{
this.serviceProvider = serviceProvider;
}
public void Apply(ControllerModel controller)
{
2021-02-02 19:01:29 -05:00
if (controller.ControllerType.IsGenericType && controller.ControllerType.Name.Contains("CommandController") && controller.ControllerType.Assembly == typeof(CommandControllerConvention).Assembly)
2021-02-02 12:19:59 -05:00
{
var genericType = controller.ControllerType.GenericTypeArguments[0];
2021-02-02 19:01:29 -05:00
var commandDiscovery = this.serviceProvider.GetRequiredService<ICommandDiscovery>();
var command = commandDiscovery.FindCommand(genericType);
controller.ControllerName = command.Name;
2021-02-02 12:19:59 -05:00
}
}
}
}