34 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			34 lines
		
	
	
		
			1.1 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Microsoft.AspNetCore.Mvc.ApplicationModels;
 | |
| using Microsoft.Extensions.DependencyInjection;
 | |
| using OpenHarbor.CQRS.Abstractions.Discovery;
 | |
| using System;
 | |
| 
 | |
| namespace OpenHarbor.CQRS.AspNetCore.Mvc;
 | |
| 
 | |
| public class CommandControllerConvention : IControllerModelConvention
 | |
| {
 | |
|     private readonly IServiceProvider _serviceProvider;
 | |
| 
 | |
|     public CommandControllerConvention(IServiceProvider serviceProvider)
 | |
|     {
 | |
|         this._serviceProvider = serviceProvider;
 | |
|     }
 | |
| 
 | |
|     public void Apply(ControllerModel controller)
 | |
|     {
 | |
|         if (!controller.ControllerType.IsGenericType)
 | |
|             return;
 | |
| 
 | |
|         if (!controller.ControllerType.Name.Contains("CommandController"))
 | |
|             return;
 | |
|         
 | |
|         if (controller.ControllerType.Assembly != typeof(CommandControllerConvention).Assembly)
 | |
|             return;
 | |
|             
 | |
|         var genericType = controller.ControllerType.GenericTypeArguments[0];
 | |
|         var commandDiscovery = this._serviceProvider.GetRequiredService<ICommandDiscovery>();
 | |
|         var command = commandDiscovery.FindCommand(genericType);
 | |
|         controller.ControllerName = command.LowerCamelCaseName;
 | |
|     }
 | |
| }
 |