Kit.Core/LibCommon/Kit.Core.Helpers/Routes/ControllerRouteFluentAutoge...

94 lines
3.2 KiB
Plaintext

<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
namespace Kit.Helpers.Routes
{
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
public partial class ControllerRouteFluent<TController> : IEndPointFluent<ControllerRouteFluent<TController>>
where TController : Controller
{
public delegate TResult GenericResult0Delegate<TResult>();
public delegate Task<TResult> GenericResult0DelegateAsync<TResult>();
<#
string _paramsT1 = "TResult";
string _paramsD1 = string.Empty;
for (int i = 1; i <= 5; i++)
{
string paramName = "TInput" + i.ToString();
_paramsT1 = _paramsT1 + ", " + paramName;
_paramsD1 = _paramsD1 + (_paramsD1 != string.Empty ? ", " : string.Empty) + paramName + " input" + i.ToString();
#>
public delegate TResult GenericResult<#= i #>Delegate<<#= _paramsT1 #>>(<#= _paramsD1 #>);
public delegate Task<TResult> GenericResult<#= i #>DelegateAsync<<#= _paramsT1 #>>(<#= _paramsD1 #>);
<#
}
#>
<#
var actionTypes = new List<Tuple<string, string, bool>>
{
new ("Generic", "TResult", true),
new ("Json", "JsonResult", false),
new ("Empty", "EmptyResult", false),
new ("Content", "ContentResult", false),
new ("View", "ViewResult", false),
new ("PartialView", "PartialViewResult", false),
new ("Action", "IActionResult", false),
};
foreach (var actionType in actionTypes)
{
bool withResultInGeneric = actionType.Item3;
var items = new List<Tuple<string, int, string, string>>();
items.Add(new Tuple<string, int, string, string>(actionType.Item1, 0, withResultInGeneric ? "<" + actionType.Item2 + ">" : string.Empty, actionType.Item2));
string _paramsT = withResultInGeneric ? actionType.Item2 : string.Empty;
string _paramsDelegate = actionType.Item2;
for (int i = 1; i <= 5; i++)
{
string genericString = "TInput" + i.ToString();
_paramsT += (_paramsT == string.Empty ? string.Empty : ", ") + genericString;
_paramsDelegate += ", " + genericString;
items.Add(new Tuple<string, int, string, string>(actionType.Item1, i, "<" + _paramsT + ">", _paramsDelegate));
}
#>
#region <#= actionType.Item2 #> Actions
<#
foreach (var item in items)
{
#>
public ControllerRouteFluent<TController> <#= item.Item1 #>Action<#= item.Item3 #>(string url, Expression<Func<TController, GenericResult<#= item.Item2 #>Delegate<<#= item.Item4 #>>>> actionSelector)
=> this.AddRouteLambda(url, actionSelector);
public ControllerRouteFluent<TController> <#= item.Item1 #>ActionAsync<#= item.Item3 #>(string url, Expression<Func<TController, GenericResult<#= item.Item2 #>DelegateAsync<<#= item.Item4 #>>>> actionSelector)
=> this.AddRouteLambda(url, actionSelector);
<#
}
#>
#endregion
<#
}
#>
}
}