SkinnyControllersCommon by Ignat Andrei
Nuget / site data
Details
Info
Name: SkinnyControllersCommon
C# 9.0 ONLY
Author: Ignat Andrei
NuGet: https://www.nuget.org/packages/SkinnyControllersCommon
You can find more details at https://github.com/ignatandrei/SkinnyControllersGenerator/
Source : https://github.com/ignatandrei/SkinnyControllersGenerator/
Original Readme
SkinnyControllersGenerator
SkinnyControllers generates controller action for each field of your controller
How to install SkinnyControllers in a .NET Core 5 WebAPI / MVC application Step 1:
Install https://www.nuget.org/packages/SkinnyControllersGenerator/
Step 2:
Install https://www.nuget.org/packages/SkinnyControllersCommon/
Step 3:
Add a field to your action either via DI, either directly
[ApiController]
[Route("[controller]/[action]")]
public partial class WeatherForecastController : ControllerBase
{
private readonly RepositoryWF repository;
public WeatherForecastController(RepositoryWF repository)
{
this.repository = repository;
//or make
//this.repository=new RepositoryWF();
}
Step 4:
Add partial declaration and decorate your controller with
[AutoActions(template = TemplateIndicator.AllPost,FieldsName =new[] { "*" }, ExcludeFields =new[]{"_logger"})]
[ApiController]
[Route("[controller]/[action]")]
public partial class WeatherForecastController : ControllerBase
You can choose your template from
- All Post
- Get - if not arguments, POST else
- Rest action
You can add your template in 2 ways: //if custom template , hte name must end in controller.txt
[AutoActions(template = TemplateIndicator.CustomTemplateFile, FieldsName = new[] { "*" } ,CustomTemplateFileName = "Controllers\CustomTemplate1.controller.txt")]
For creating new generic templates, please PR to https://github.com/ignatandrei/SkinnyControllersGenerator
That's all!
Usual problems:
- error CS0260: Missing partial modifier on declaration of type
Answer:
Did you put partial on the controller declaration ?
public partial class
More Roslyn Source Code Generators
You can find more RSCG with examples at Roslyn Source Code Generators
About
Automatically add controllers actions for any class injected in constructor
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- WeatherForecastController.cs
- WeatherActions.cs
This is the CSharp Project that references SkinnyControllersCommon
<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)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of SkinnyControllersCommon in Program.cs
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();
This is the use of SkinnyControllersCommon in WeatherForecastController.cs
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;
}
}
This is the use of SkinnyControllersCommon in WeatherActions.cs
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;
}
}
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- WeatherForecastController.autogenerate.cs
//------------------------------------------------------------------------------
// <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);
}
}
}
Usefull
Download Example (.NET C# )
Share SkinnyControllersCommon
https://ignatandrei.github.io/RSCG_Examples/v2/docs/SkinnyControllersCommon