RSCG – SkinnyControllersCommon
RSCG – SkinnyControllersCommon
name | SkinnyControllersCommon |
nuget | https://www.nuget.org/packages/SkinnyControllersCommon |
link | https://github.com/ignatandrei/SkinnyControllersGenerator/ |
author | Ignat Andrei |
Automatically add controllers actions for any class injected in constructor
This is how you can use SkinnyControllersCommon .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net6.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> </PropertyGroup> <ItemGroup> <PackageReference Include="SkinnyControllersCommon" Version="2023.5.14.2055" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> </ItemGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GeneratedX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
using SkinnyControllersDemo.Controllers; var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); builder.Services.AddTransient<WeatherActions>(); var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseHttpsRedirection(); app.UseAuthorization(); app.MapControllers(); app.Run();
using Microsoft.AspNetCore.Mvc; using SkinnyControllersCommon; namespace SkinnyControllersDemo.Controllers; [AutoActions(template = TemplateIndicator.NoArgs_Is_Get_Else_Post, FieldsName = new[] { "*" }, ExcludeFields = new[] { "_logger" })] [ApiController] [Route("[controller]/[action]")] public partial class WeatherForecastController : ControllerBase { private readonly ILogger<WeatherForecastController> _logger; private readonly WeatherActions weather; public WeatherForecastController(ILogger<WeatherForecastController> logger, WeatherActions weather) { _logger = logger; this.weather = weather; } }
namespace SkinnyControllersDemo.Controllers { public class WeatherActions { private static readonly string[] Summaries = new[] { "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" }; public IEnumerable<WeatherForecast> Get() { return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray(); } public async Task<int> MultiplyBy2(int nr) { await Task.Delay(nr); return nr*2; } } }
The code that is generated is
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // // SkinnyControllersGenerator: // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ using System.CodeDom.Compiler; using System.Runtime.CompilerServices; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; namespace SkinnyControllersDemo.Controllers { [GeneratedCode("SkinnyControllersGenerator", "")] [CompilerGenerated] partial class WeatherForecastController{ /*[HttpGet()] public int id(){ System.Diagnostics.Debugger.Break(); return 1; } */ [HttpGet] public System.Collections.Generic.IEnumerable<SkinnyControllersDemo.WeatherForecast> Get (){ //System.Diagnostics.Debugger.Break(); return weather.Get(); } [HttpPost] public System.Threading.Tasks.Task<int> MultiplyBy2 (int nr){ //System.Diagnostics.Debugger.Break(); return weather.MultiplyBy2(nr); } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/SkinnyControllersCommon
Leave a Reply