Albon.ControllerGenerator 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Albon.ControllerGenerator --version 1.0.1                
NuGet\Install-Package Albon.ControllerGenerator -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Albon.ControllerGenerator" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Albon.ControllerGenerator --version 1.0.1                
#r "nuget: Albon.ControllerGenerator, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Albon.ControllerGenerator as a Cake Addin
#addin nuget:?package=Albon.ControllerGenerator&version=1.0.1

// Install Albon.ControllerGenerator as a Cake Tool
#tool nuget:?package=Albon.ControllerGenerator&version=1.0.1                

ControllerGenerator

The ControllerGenerator is a useful tool for ASP.NET WebApps that enables dynamic creation of ControllerBase derived classes at runtime from business service classes.

Nuget Package

Get Started

  1. Flag the methods of the service you want to exposes an API Controller with HttpMethodAttribute class (HttpGet, HttpPost,...)

    [HttpGet]
    public IEnumerable<WeatherForecast> GetWeatherForecastGet1()
    
  2. Create the controller before using the ApplicationBuilder by calling the CreateController<TService>() of the ControllerGenerator class.

    public static Type CreateController<TService>();
    

    You can implement your own routing convention by implementing IRoutingConvention and call CreateController<TService>(IRoutingConvention routingConvention). It permits to choose how the route of your WebApp is defined. You can also implement your own naming convention by implementing INamingConvention and call CreateController<TService>(INamingConvention namingConvention). It permits to choose how the name of services of your WebApp is defined. You can also do both by calling CreateController<TService>(IRoutingConvention routingConvention, INamingConvention namingConvention). If you don't implement these interfaces, the ControllerGenerator will use default convention.

  3. Add the dynamic assembly to the WebApplicationBuilder

    var applicationBuilder = WebApplication.CreateBuilder(args);
    applicationBuilder.Services.AddMvc().AddApplicationPart(ControllerGenerator.ControllerGenerator.DynamicAssembly);
    
  4. Enjoy !

Why use it

The ControllerGenerator is a useful tool for ASP.NET WebApps that enables dynamic creation of ControllerBase derived classes at runtime. This approach offers several benefits and use cases:

  • Reduced Boilerplate Code: With ControllerGenerator, you can generate ControllerBase derived classes on-the-fly without the need to write extensive boilerplate code manually. This leads to cleaner and more concise codebases.

  • Dynamic API Endpoints: It allows you to create API endpoints dynamically, which can be particularly helpful when you have a large number of similar endpoints with minor variations.

  • Plugin Architecture: When building extensible applications, you can use ControllerGenerator to dynamically load and integrate external or third-party API controllers without having to recompile your main project.

  • Customization and Configuration: By generating ControllerBase derived classes at runtime, you can customize the behavior of API endpoints based on configuration files, database settings, or user input, providing greater flexibility to your application.

  • Versioning and Feature Toggling: ControllerGenerator can be leveraged to implement API versioning, allowing you to maintain backward compatibility while introducing new features to the API.

  • Testing and Mocking: In unit testing scenarios, ControllerGenerator can be beneficial for creating mock ControllerBase derived classes with predefined responses, making it easier to isolate and test different parts of your application.

  • Rapid Prototyping: During the early stages of development or when prototyping new features, using ControllerGenerator can accelerate the process of creating temporary or experimental API endpoints.

  • Microservices and Multi-Tenancy: In microservices architectures or multi-tenant applications, ControllerGenerator can dynamically generate controllers based on tenant-specific configurations or service requirements.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.5 196 7/27/2023
1.0.4 196 7/23/2023
1.0.3 190 7/22/2023
1.0.2 184 7/22/2023
1.0.1 178 7/22/2023
1.0.0 180 7/22/2023