Skip to content

Commit

Permalink
Add options to generate repositories as lazy or not at all (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
Beffyman committed Jan 25, 2020
1 parent 5094839 commit c5a86de
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 90 deletions.
1 change: 1 addition & 0 deletions src/Beffyman.AspNetCore.Client.Generator/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class Constants
public const string HttpOverrideGetMethod = nameof(IHttpOverride.GetResponseAsync);
public const string HttpOverrideOnNonOverridedResponse = nameof(IHttpOverride.OnNonOverridedResponseAsync);

public const string RepositoryLazyProviderName = "provider";

public const string Serializer = nameof(IHttpSerializer);
public const string SerializerField = "Serializer";
Expand Down
19 changes: 19 additions & 0 deletions src/Beffyman.AspNetCore.Client.Generator/GeneratorTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,25 @@ public class GeneratorTask :
#endif
{
public string CurrentDirectory { get; set; }

public string RouteToServiceProjectFolder { get; set; }
public string ClientInterfaceName { get; set; }
public string RegisterName { get; set; }

public string UseValueTask { get; set; }
public string UseInternalClients { get; set; }
public string ClientRouteConstraints { get; set; }
public string ErrorOnUnhandledCallback { get; set; }
public string MultipleFiles { get; set; }
public string GenerateStaticRoutes { get; set; }

public string GenerateClientRepository { get; set; }
public string GenerateLazyClientRepository { get; set; }

public string RoutesNamespace { get; set; }
public string ClientNamespace { get; set; }
public string HubNamespace { get; set; }

public string AllowedNamespaces { get; set; }
public string ExcludedNamespaces { get; set; }

Expand All @@ -41,15 +48,21 @@ public void Fill(IDictionary<string, string> properties)
RouteToServiceProjectFolder = properties.GetValueOrDefault(nameof(RouteToServiceProjectFolder));
ClientInterfaceName = properties.GetValueOrDefault(nameof(ClientInterfaceName));
RegisterName = properties.GetValueOrDefault(nameof(RegisterName));

UseValueTask = properties.GetValueOrDefault(nameof(UseValueTask));
UseInternalClients = properties.GetValueOrDefault(nameof(UseInternalClients));
ClientRouteConstraints = properties.GetValueOrDefault(nameof(ClientRouteConstraints));
ErrorOnUnhandledCallback = properties.GetValueOrDefault(nameof(ErrorOnUnhandledCallback));
MultipleFiles = properties.GetValueOrDefault(nameof(MultipleFiles));
GenerateStaticRoutes = properties.GetValueOrDefault(nameof(GenerateStaticRoutes));

GenerateClientRepository = properties.GetValueOrDefault(nameof(GenerateClientRepository));
GenerateLazyClientRepository = properties.GetValueOrDefault(nameof(GenerateLazyClientRepository));

RoutesNamespace = properties.GetValueOrDefault(nameof(RoutesNamespace));
ClientNamespace = properties.GetValueOrDefault(nameof(ClientNamespace));
HubNamespace = properties.GetValueOrDefault(nameof(HubNamespace));

AllowedNamespaces = properties.GetValueOrDefault(nameof(AllowedNamespaces));
ExcludedNamespaces = properties.GetValueOrDefault(nameof(ExcludedNamespaces));
}
Expand Down Expand Up @@ -84,15 +97,21 @@ protected override bool ExecuteIsolated()
Settings.RouteToServiceProjectFolder = RouteToServiceProjectFolder;
Settings.ClientInterfaceName = ClientInterfaceName;
Settings.RegisterName = RegisterName;

Settings.UseValueTask = bool.Parse(UseValueTask ?? "false");
Settings.UseInternalClients = bool.Parse(UseInternalClients ?? "false");
Settings.ClientRouteConstraints = bool.Parse(ClientRouteConstraints ?? "false");
Settings.ErrorOnUnhandledCallback = bool.Parse(ErrorOnUnhandledCallback ?? "false");
Settings.MultipleFiles = bool.Parse(MultipleFiles ?? "false");
Settings.GenerateStaticRoutes = bool.Parse(GenerateStaticRoutes ?? "false");

Settings.GenerateClientRepository = bool.Parse(GenerateClientRepository ?? "true");
Settings.GenerateLazyClientRepository = bool.Parse(GenerateLazyClientRepository ?? "false");

Settings.RoutesNamespace = RoutesNamespace;
Settings.ClientNamespace = ClientNamespace;
Settings.HubNamespace = HubNamespace;

Settings.AllowedNamespaces = AllowedNamespaces?.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
Settings.ExcludedNamespaces = ExcludedNamespaces?.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

Expand Down
82 changes: 74 additions & 8 deletions src/Beffyman.AspNetCore.Client.Generator/Output/ClassWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ public static void WriteClientsFile(GenerationContext context)

var usings = new List<string>
{
@"using Beffyman.AspNetCore.Client;",
@"//Requires nuget Beffyman.AspNetCore.Client",
"using Beffyman.AspNetCore.Client;",
"using Beffyman.AspNetCore.Client.Authorization;",
"using Beffyman.AspNetCore.Client.Exceptions;",
"using Beffyman.AspNetCore.Client.Http;",
Expand Down Expand Up @@ -405,6 +406,11 @@ public static string GetRelativePath(string file)

public static string WriteRepositoryRegistration(string version)
{
if (!Settings.GenerateClientRepository)
{
return string.Empty;
}

return $@" services.AddScoped<I{Settings.ClientInterfaceName}{version}Repository,{Settings.ClientInterfaceName}{version}Repository>();";
}

Expand Down Expand Up @@ -477,6 +483,11 @@ private static string WriteRouteRepositoryEndpoint(AspNetCoreHttpController cont

public static string WriteRepositories(GenerationContext context)
{
if (!Settings.GenerateClientRepository)
{
return string.Empty;
}

var versions = context.HttpClients.Where(x => x.Generated)
.GroupBy(x => x.NamespaceVersion)
.OrderBy(x => x.Key)
Expand All @@ -493,8 +504,6 @@ public static string WriteRepositories(GenerationContext context)

public static string WriteRepository(IGrouping<string, AspNetCoreHttpController> version)
{


return
$@"
public interface I{Settings.ClientInterfaceName}{version.Key}Repository
Expand All @@ -504,39 +513,96 @@ public interface I{Settings.ClientInterfaceName}{version.Key}Repository
{(Settings.UseInternalClients ? "internal" : "public")} class {Settings.ClientInterfaceName}{version.Key}Repository : I{Settings.ClientInterfaceName}{version.Key}Repository
{{
{WriteRepositoryLazyProvider()}
{string.Join($@"{Environment.NewLine}", version.OrderBy(x => x.Name).Select(x => WriteRepositoryProperty(version.Key, x)))}
public {Settings.ClientInterfaceName}{version.Key}Repository
(
{string.Join($@",{Environment.NewLine}", version.OrderBy(x => x.Name).Select(x => WriteRepositoryParameter(version.Key, x)))}
{WriteRepositoryLazyProviderParameter(version)}
{string.Join($@",{Environment.NewLine}", version.OrderBy(x => x.Name).Select(x => WriteRepositoryParameter(version.Key, x)).NotNull())}
)
{{
{WriteRepositoryLazyProviderAssignment()}
{string.Join($@"{Environment.NewLine}", version.OrderBy(x => x.Name).Select(x => WriteRepositoryAssignment(version.Key, x)))}
}}
}}
";

}

private static string WriteRepositoryLazyProviderParameter(IGrouping<string, AspNetCoreHttpController> version)
{
if (!Settings.GenerateLazyClientRepository)
{
return string.Empty;
}

return $"IServiceProvider {Constants.RepositoryLazyProviderName}";
}

private static string WriteRepositoryLazyProviderAssignment()
{
if (!Settings.GenerateLazyClientRepository)
{
return string.Empty;
}

return $"this._{Constants.RepositoryLazyProviderName} = {Constants.RepositoryLazyProviderName};";
}

private static string WriteRepositoryLazyProvider()
{
if (!Settings.GenerateLazyClientRepository)
{
return string.Empty;
}

return $"protected readonly IServiceProvider _{Constants.RepositoryLazyProviderName};";
}

public static string WriteRepositoryInterfaceProperty(string key, AspNetCoreHttpController controller)
private static string WriteRepositoryInterfaceProperty(string key, AspNetCoreHttpController controller)
{
return $@"{key}{(key != null ? "." : "")}{(controller.NamespaceSuffix != null ? $"{controller.NamespaceSuffix}." : string.Empty)}I{controller.ClientName} {controller.Name} {{ get; }}";
}

public static string WriteRepositoryProperty(string key, AspNetCoreHttpController controller)
{
return $@"public {key}{(key != null ? "." : "")}{(controller.NamespaceSuffix != null ? $"{controller.NamespaceSuffix}." : string.Empty)}I{controller.ClientName} {controller.Name} {{ get; }}";
string type = $"{key}{(key != null ? "." : "")}{(controller.NamespaceSuffix != null ? $"{controller.NamespaceSuffix}." : string.Empty)}I{controller.ClientName}";
string name = controller.Name;
if (!Settings.GenerateLazyClientRepository)
{
return $@"public {type} {name} {{ get; }}";
}

return
$@"private readonly Lazy<{type}> lazy_{name};
public {type} {name} => lazy_{name}.Value;";

}

public static string WriteRepositoryParameter(string key, AspNetCoreHttpController controller)
{
if (Settings.GenerateLazyClientRepository)
{
return null;
}

return $@"{key}{(key != null ? "." : "")}{(controller.NamespaceSuffix != null ? $"{controller.NamespaceSuffix}." : string.Empty)}I{controller.ClientName} param_{controller.Name.ToLower()}";
}

public static string WriteRepositoryAssignment(string key, AspNetCoreHttpController controller)
{
return $@"this.{controller.Name} = param_{controller.Name.ToLower()};";
if (!Settings.GenerateLazyClientRepository)
{
return $@"this.{controller.Name} = param_{controller.Name.ToLower()};";
}
else
{
string type = $"{key}{(key != null ? "." : "")}{(controller.NamespaceSuffix != null ? $"{controller.NamespaceSuffix}." : string.Empty)}I{controller.ClientName}";

return
$@"this.lazy_{controller.Name} = new Lazy<{type}>(() => _{Constants.RepositoryLazyProviderName}.GetService<{type}>());";
}
}


Expand Down
7 changes: 7 additions & 0 deletions src/Beffyman.AspNetCore.Client.Generator/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,23 @@ internal static class Settings
public static string RouteToServiceProjectFolder { get; set; }
public static string ClientInterfaceName { get; set; }
public static string RegisterName { get; set; }

public static bool UseValueTask { get; set; }
public static bool UseInternalClients { get; set; }
public static bool ClientRouteConstraints { get; set; }
public static bool ErrorOnUnhandledCallback { get; set; }
public static bool MultipleFiles { get; set; }
public static bool GenerateStaticRoutes { get; set; }

public static bool GenerateClientRepository { get; set; }
public static bool GenerateLazyClientRepository { get; set; }

public static string RoutesNamespace { get; set; }
public static string ClientNamespace { get; set; }
public static string HubNamespace { get; set; }



public static string[] AllowedNamespaces { get; set; }

public static string[] ExcludedNamespaces { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
<RouteToServiceProjectFolder></RouteToServiceProjectFolder>
<ClientInterfaceName>MyServiceClient</ClientInterfaceName>
<RegisterName>MyService</RegisterName>

<UseValueTask>true</UseValueTask>
<UseInternalClients>true</UseInternalClients>
<ClientRouteConstraints>true</ClientRouteConstraints>
<ErrorOnUnhandledCallback>true</ErrorOnUnhandledCallback>
<MultipleFiles>false</MultipleFiles>
<GenerateStaticRoutes>true</GenerateStaticRoutes>

<GenerateClientRepository>true</GenerateClientRepository>
<GenerateLazyClientRepository>true</GenerateLazyClientRepository>

<RoutesNamespace>Routes</RoutesNamespace>
<ClientNamespace>MyService.Clients</ClientNamespace>
<HubNamespace>MyService.Hubs</HubNamespace>

<AllowedNamespaces>System*;</AllowedNamespaces>
<ExcludedNamespaces></ExcludedNamespaces>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
RouteToServiceProjectFolder="$(RouteToServiceProjectFolder)"
ClientInterfaceName="$(ClientInterfaceName)"
RegisterName="$(RegisterName)"

UseValueTask="$(UseValueTask)"
UseInternalClients="$(UseInternalClients)"
ClientRouteConstraints="$(ClientRouteConstraints)"
ErrorOnUnhandledCallback="$(ErrorOnUnhandledCallback)"
MultipleFiles="$(MultipleFiles)"
GenerateStaticRoutes ="$(GenerateStaticRoutes)"

GenerateClientRepository ="$(GenerateClientRepository)"
GenerateLazyClientRepository ="$(GenerateLazyClientRepository)"

RoutesNamespace ="$(RoutesNamespace)"
ClientNamespace="$(ClientNamespace)"
HubNamespace="$(HubNamespace)"

AllowedNamespaces="$(AllowedNamespaces)"
ExcludedNamespaces="$(ExcludedNamespaces)"/>

Expand Down
1 change: 1 addition & 0 deletions tests/FunctionApp2.Clients/Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//Requires nuget Beffyman.AspNetCore.Client
using Beffyman.AspNetCore.Client.Authorization;
using Beffyman.AspNetCore.Client.Exceptions;
using Beffyman.AspNetCore.Client.GeneratorExtensions;
Expand Down
1 change: 1 addition & 0 deletions tests/TestAzureFunction.Clients/Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//Requires nuget Beffyman.AspNetCore.Client
using Beffyman.AspNetCore.Client.Authorization;
using Beffyman.AspNetCore.Client.Exceptions;
using Beffyman.AspNetCore.Client.GeneratorExtensions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<ErrorOnUnhandledCallback>true</ErrorOnUnhandledCallback>
<MultipleFiles>false</MultipleFiles>
<GenerateStaticRoutes>true</GenerateStaticRoutes>
<GenerateClientRepository>false</GenerateClientRepository>
<GenerateLazyClientRepository>false</GenerateLazyClientRepository>
<RoutesNamespace>Routes</RoutesNamespace>
<ClientNamespace>TestAzureFunction.Clients</ClientNamespace>
<AllowedNamespaces>$(AllowedNamespaces);TestAzureFunction.*;</AllowedNamespaces>
Expand Down
1 change: 1 addition & 0 deletions tests/TestBlazorApp.Clients/Clients.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//Requires nuget Beffyman.AspNetCore.Client
using Beffyman.AspNetCore.Client.Authorization;
using Beffyman.AspNetCore.Client.Exceptions;
using Beffyman.AspNetCore.Client.GeneratorExtensions;
Expand Down
23 changes: 1 addition & 22 deletions tests/TestBlazorApp.Clients/Installer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//Requires nuget Beffyman.AspNetCore.Client
using Beffyman.AspNetCore.Client.Authorization;
using Beffyman.AspNetCore.Client.Exceptions;
using Beffyman.AspNetCore.Client.GeneratorExtensions;
Expand Down Expand Up @@ -41,7 +42,6 @@ public static IServiceCollection AddTestBlazorClients(this IServiceCollection se
configuration.RegisterClientWrapperCreator<ITestBlazorAppClient>(TestBlazorAppClientWrapper.Create);
configuration.UseClientWrapper<ITestBlazorAppClientWrapper, TestBlazorAppClientWrapper>((provider) => new TestBlazorAppClientWrapper(provider.GetService<Func<ITestBlazorAppClient, IFlurlClient>>(), configuration.GetSettings(), provider));
configure?.Invoke(configuration);
services.AddScoped<ITestBlazorAppClientRepository, TestBlazorAppClientRepository>();
services.AddScoped<IWeatherForecastClient, WeatherForecastClient>();
return configuration.ApplyConfiguration<ITestBlazorAppClient>(services);
}
Expand Down Expand Up @@ -85,25 +85,4 @@ public static ITestBlazorAppClientWrapper Create(Func<ITestBlazorAppClient, IFlu
public interface ITestBlazorAppClient : IClient
{
}

public interface ITestBlazorAppClientRepository
{
IWeatherForecastClient WeatherForecast
{
get;
}
}

internal class TestBlazorAppClientRepository : ITestBlazorAppClientRepository
{
public IWeatherForecastClient WeatherForecast
{
get;
}

public TestBlazorAppClientRepository(IWeatherForecastClient param_weatherforecast)
{
this.WeatherForecast = param_weatherforecast;
}
}
}
1 change: 1 addition & 0 deletions tests/TestBlazorApp.Clients/Routes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//Requires nuget Beffyman.AspNetCore.Client
using Beffyman.AspNetCore.Client.Authorization;
using Beffyman.AspNetCore.Client.Exceptions;
using Beffyman.AspNetCore.Client.GeneratorExtensions;
Expand Down
2 changes: 2 additions & 0 deletions tests/TestBlazorApp.Clients/TestBlazorApp.Clients.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<ErrorOnUnhandledCallback>false</ErrorOnUnhandledCallback>
<MultipleFiles>true</MultipleFiles>
<GenerateStaticRoutes>true</GenerateStaticRoutes>
<GenerateClientRepository>false</GenerateClientRepository>
<GenerateLazyClientRepository>true</GenerateLazyClientRepository>
<RoutesNamespace>Routes</RoutesNamespace>
<ClientNamespace>TestBlazorApp.Clients</ClientNamespace>
<HubNamespace>TestBlazorApp.Hubs</HubNamespace>
Expand Down
Loading

0 comments on commit c5a86de

Please sign in to comment.