Skip to content

Commit dcae84a

Browse files
committed
AppDefinitions new registration information on debug model was implemented
1 parent b37be50 commit dcae84a

File tree

5 files changed

+78
-17
lines changed

5 files changed

+78
-17
lines changed

src/Calabonga.AspNetCore.AppDefinitions/AppDefinition.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public virtual void ConfigureApplication(WebApplication app) { }
2727
public virtual int OrderIndex => 0;
2828

2929
/// <summary>
30-
/// Enable or disable to register into pipeline for the current application definition
30+
/// Enable or disable to register into pipeline for the current application Definition
3131
/// </summary>
3232
public virtual bool Enabled { get; protected set; } = true;
3333
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace Calabonga.AspNetCore.AppDefinitions;
2+
3+
/// <summary>
4+
/// Information about collection of <see cref="IAppDefinition"/>
5+
/// </summary>
6+
internal sealed class AppDefinitionCollection
7+
{
8+
internal IList<AppDefinitionItem> Items { get; } = new List<AppDefinitionItem>();
9+
internal IList<string> EntryPoints { get; } = new List<string>();
10+
11+
internal void AddInfo(AppDefinitionItem definition)
12+
{
13+
var exists = Items.FirstOrDefault(x => x == definition);
14+
if (exists is null)
15+
{
16+
Items.Add(definition);
17+
}
18+
}
19+
20+
/// <summary>
21+
/// Adding founded item to collection
22+
/// </summary>
23+
/// <param name="entryPointName"></param>
24+
public void AddEntryPoint(string entryPointName) => EntryPoints.Add(entryPointName);
25+
}
26+
27+
/// <summary>
28+
/// Information about <see cref="IAppDefinition"/>
29+
/// </summary>
30+
/// <param name="Definition"></param>
31+
public sealed record AppDefinitionItem(IAppDefinition Definition);

src/Calabonga.AspNetCore.AppDefinitions/AppDefinitionExtensions.cs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,46 @@ public static void AddDefinitions(this IServiceCollection source, WebApplication
2020
{
2121
var logger = source.BuildServiceProvider().GetRequiredService<ILogger<AppDefinition>>();
2222
var definitions = new List<IAppDefinition>();
23+
var appDefinitionInfo = source.BuildServiceProvider().GetService<AppDefinitionCollection>();
24+
var info = appDefinitionInfo ?? new AppDefinitionCollection();
25+
2326
foreach (var entryPoint in entryPointsAssembly)
2427
{
28+
info.AddEntryPoint(entryPoint.Name);
29+
30+
2531
var types = entryPoint.Assembly.ExportedTypes.Where(x => !x.IsAbstract && typeof(IAppDefinition).IsAssignableFrom(x));
2632
var instances = types.Select(Activator.CreateInstance).Cast<IAppDefinition>().ToList();
27-
var instancesOrdered = instances.Where(x => x.Enabled).OrderBy(x => x.OrderIndex).ToList();
28-
if (logger.IsEnabled(LogLevel.Debug))
33+
//if (logger.IsEnabled(LogLevel.Debug))
34+
//{
35+
// logger.LogDebug("AppDefinitions Founded: {@AppDefinitionsCountTotal}.", instances.Count);
36+
//}
37+
38+
foreach (var definition in instances)
2939
{
30-
logger.LogDebug(@"[AppDefinitions] Founded: {AppDefinitionsCountTotal}. Enabled: {AppDefinitionsCountEnabled}", instances.Count, instancesOrdered.Count);
31-
logger.LogDebug(@"[AppDefinitions] Registered [{Total}]", string.Join(", ", instancesOrdered.Select(x => x.GetType().Name).ToArray()));
40+
info.AddInfo(new AppDefinitionItem(definition));
3241
}
3342

43+
var instancesOrdered = instances.Where(x => x.Enabled).OrderBy(x => x.OrderIndex).ToList();
3444
definitions.AddRange(instancesOrdered);
3545
}
3646

37-
definitions.ForEach(app => app.ConfigureServices(source, builder));
38-
source.AddSingleton(definitions as IReadOnlyCollection<IAppDefinition>);
47+
foreach (var definition in definitions)
48+
{
49+
definition.ConfigureServices(source, builder);
50+
}
51+
if (logger.IsEnabled(LogLevel.Debug))
52+
{
53+
logger.LogDebug("[AppDefinitions]: From {@items}", string.Join(", ", info.EntryPoints));
54+
55+
56+
foreach (var item in info.Items.OrderBy(x => x.Definition.GetType().Name))
57+
{
58+
logger.LogDebug("[AppDefinitions]: {@AppDefinitionName} (Enabled: {@Enabled})", item.Definition.GetType().Name, item.Definition.Enabled ? "Yes" : "No");
59+
}
60+
}
61+
62+
source.AddSingleton(info);
3963
}
4064

4165
/// <summary>
@@ -49,14 +73,20 @@ public static void AddDefinitions(this IServiceCollection source, WebApplication
4973
public static void UseDefinitions(this WebApplication source)
5074
{
5175
var logger = source.Services.GetRequiredService<ILogger<AppDefinition>>();
52-
var definitions = source.Services.GetRequiredService<IReadOnlyCollection<IAppDefinition>>();
53-
var instancesOrdered = definitions.Where(x => x.Enabled).OrderBy(x => x.OrderIndex).ToList();
76+
var definitions = source.Services.GetRequiredService<AppDefinitionCollection>();
77+
78+
if (logger.IsEnabled(LogLevel.Debug))
79+
{
80+
logger.LogDebug("From {Modules} assemblies totally AppDefinitions found: {Count} ", string.Join(", ", definitions.EntryPoints), definitions.Items.Count);
81+
}
82+
83+
var instancesOrdered = definitions.Items.Where(x => x.Definition.Enabled).OrderBy(x => x.Definition.OrderIndex).ToList();
5484

55-
instancesOrdered.ForEach(x => x.ConfigureApplication(source));
85+
instancesOrdered.ForEach(x => x.Definition.ConfigureApplication(source));
5686

5787
if (logger.IsEnabled(LogLevel.Debug))
5888
{
59-
logger.LogDebug("Total application definitions configured {Count}", instancesOrdered.Count);
89+
logger.LogDebug("Total AppDefinitions applied: {Count}", instancesOrdered.Count);
6090
}
6191
}
6292

src/Calabonga.AspNetCore.AppDefinitions/Calabonga.AspNetCore.AppDefinitions.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
88
<Title>Calabonga.AspNetCore.AppDefinitions</Title>
9-
<Version>1.2.2</Version>
9+
<Version>1.3.0</Version>
1010
<Authors>Calabonga</Authors>
1111
<Company>Calabonga Soft</Company>
1212
<Product>Calabonga.AspNetCore.AppDefinitions</Product>
1313
<Description>Application Definitions base classes. The small but very helpful packege that can help you to organize your ASP.NET Core application.</Description>
14-
<Copyright>Calabonga SOFT 2001-2022</Copyright>
14+
<Copyright>Calabonga SOFT © 2001-2023</Copyright>
1515
<PackageProjectUrl>https://github.com/Calabonga/Calabonga.AspNetCore.AppDefinitions</PackageProjectUrl>
1616
<PackageIcon>logo.png</PackageIcon>
1717
<PackageReadmeFile>README.md</PackageReadmeFile>
1818
<RepositoryUrl>https://github.com/Calabonga/Calabonga.AspNetCore.AppDefinitions</RepositoryUrl>
1919
<RepositoryType>git</RepositoryType>
2020
<PackageTags>calabonga;architecture;definitions;minimal-api;nimble-framework</PackageTags>
21-
<PackageReleaseNotes>AppDefinition LogLevel updated</PackageReleaseNotes>
21+
<PackageReleaseNotes>AppDefinitions new registration information on debug model was implemented</PackageReleaseNotes>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2323
</PropertyGroup>
2424

src/Calabonga.AspNetCore.AppDefinitions/IAppDefinition.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace Calabonga.AspNetCore.AppDefinitions;
55

66
/// <summary>
7-
/// Application definition interface abstraction
7+
/// Application Definition interface abstraction
88
/// </summary>
9-
internal interface IAppDefinition
9+
public interface IAppDefinition
1010
{
1111
/// <summary>
1212
/// Configure services for current application
@@ -27,7 +27,7 @@ internal interface IAppDefinition
2727
int OrderIndex { get; }
2828

2929
/// <summary>
30-
/// Enable or disable to register into pipeline for the current application definition
30+
/// Enable or disable to register into pipeline for the current application Definition
3131
/// </summary>
3232
bool Enabled { get; }
3333
}

0 commit comments

Comments
 (0)