Skip to content

Commit ebd6b85

Browse files
committed
First release 1.0.0
1 parent 2caa836 commit ebd6b85

File tree

11 files changed

+103
-10
lines changed

11 files changed

+103
-10
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,40 @@
11
# Calabonga.AspNetCore.AppDefinitions
2-
Application Definitions base classes. The small but very helpful packege that can help you to organize your ASP.NET Core application.
2+
3+
Сборка позволяет навести порядок в вашем `Program.cs`. Можно всё разложить "по полочкам". Чтобы воспользоваться сборкой надо:
4+
5+
## Установка nuget-пакета
6+
7+
Можно воспользоваться инструментов Visual Studio:
8+
9+
![image1](./docs/1.jpg)
10+
11+
Или можно просто прописать в файле проекта, но тогда надо будет подставить правильную версию пакета. Посмотреть последнюю актуальную версию можно на [nuget.org](https://www.nuget.org/packages/Calabonga.AspNetCore.AppDefinitions/).
12+
13+
![image2](./docs/2.jpg)
14+
15+
### Создание AppDefinitions
16+
17+
Создайте папку `Definitions` в вашем проекте. В папке создайте `ContainerDefinition` и унаследуйте его от `AppDefinition`, как показано ниже на картинке. После этого сделайте переопределение метода `ConfigureServices` и/или других методов и свойств.
18+
19+
![image3](./docs/3.jpg)
20+
21+
На этой картинке переопределено два метода:
22+
23+
![image4](./docs/4.jpg)
24+
25+
Подключите ваши определения как показано на этой картинке:
26+
27+
![image6](./docs/6.jpg)
28+
29+
Таких определений (наследников от `AppDefinition`) может быть сколько угодно (конечно же в разумных пределах). После старта приложения вы увидите (если включен уровень логирования `Debug`) список всех подключенных определений (`AppDefinition`). Например, в моём случае их 18.
30+
31+
![image5](./docs/5.jpg)
32+
33+
### Фильтрация и порядок
34+
35+
У каждого из созданных вами наследников от `AppDefinition` есть свойство `Enabled` и `OrderIndex`. Угадайте, что можно с ними (с `AppDefinition`ами) сделать?
36+
37+
# An English
38+
Application Definitions base classes. The small but very helpful package that can help you to organize your ASP.NET Core application.
339

440
You can find more information in my blog [Nimble Framework](https://www.calabonga.net/blog/post/nimble-framework-v-6-1)

docs/1.jpg

419 KB
Loading

docs/2.jpg

451 KB
Loading

docs/3.jpg

121 KB
Loading

docs/4.jpg

320 KB
Loading

docs/5.jpg

448 KB
Loading

docs/6.jpg

350 KB
Loading

src/Calabonga.AspNetCore.AppDefinitions/AppDefinition.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;
55

6-
namespace Calabonga.MinimalApi.Definitions;
6+
namespace Calabonga.AspNetCore.AppDefinitions;
77

88
/// <summary>
99
/// Base implementation for <see cref="IAppDefinition"/>
@@ -23,4 +23,14 @@ public virtual void ConfigureServices(IServiceCollection services, IConfiguratio
2323
/// <param name="app"></param>
2424
/// <param name="env"></param>
2525
public virtual void ConfigureApplication(WebApplication app, IWebHostEnvironment env) { }
26+
27+
/// <summary>
28+
/// Order index for including into pipeline. Default value is 0 for, that's why order index can be undefined.
29+
/// </summary>
30+
public virtual int OrderIndex => 0;
31+
32+
/// <summary>
33+
/// Enable or disable to register into pipeline for the current application definition
34+
/// </summary>
35+
public virtual bool Enabled => true;
2636
}
Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,70 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.Extensions.DependencyInjection;
4+
using Microsoft.Extensions.Hosting;
5+
using Microsoft.Extensions.Logging;
46

5-
namespace Calabonga.MinimalApi.Definitions;
7+
namespace Calabonga.AspNetCore.AppDefinitions;
68

79
public static class AppDefinitionExtensions
810
{
11+
/// <summary>
12+
/// Finding all definitions in your project and include their into pipeline.<br/>
13+
/// Using <see cref="IServiceCollection"/> for registration.
14+
/// </summary>
15+
/// <remarks>
16+
/// When executing on development environment there are more diagnostic information available on console.
17+
/// </remarks>
18+
/// <param name="source"></param>
19+
/// <param name="builder"></param>
20+
/// <param name="entryPointsAssembly"></param>
921
public static void AddDefinitions(this IServiceCollection source, WebApplicationBuilder builder, params Type[] entryPointsAssembly)
1022
{
23+
var logger = source.BuildServiceProvider().GetRequiredService<ILogger<AppDefinition>>();
1124
var definitions = new List<IAppDefinition>();
12-
1325
foreach (var entryPoint in entryPointsAssembly)
1426
{
1527
var types = entryPoint.Assembly.ExportedTypes.Where(x => !x.IsAbstract && typeof(IAppDefinition).IsAssignableFrom(x));
16-
var instances = types.Select(Activator.CreateInstance).Cast<IAppDefinition>();
17-
definitions.AddRange(instances);
28+
var instances = types.Select(Activator.CreateInstance).Cast<IAppDefinition>().ToList();
29+
var instancesOrdered = instances.Where(x => x.Enabled).OrderBy(x => x.OrderIndex).ToList();
30+
if (builder.Environment.IsDevelopment())
31+
{
32+
logger.LogDebug("[AppDefinitions] Founded: {0}. Enabled: {1}", instances.Count, instancesOrdered.Count);
33+
logger.LogDebug("[AppDefinitions] Registered [{0}]", string.Join(", ", instancesOrdered.Select(x => x.GetType().Name).ToArray()));
34+
}
35+
36+
definitions.AddRange(instancesOrdered);
1837
}
1938

2039
definitions.ForEach(app => app.ConfigureServices(source, builder.Configuration));
2140
source.AddSingleton(definitions as IReadOnlyCollection<IAppDefinition>);
41+
2242
}
2343

44+
/// <summary>
45+
/// Finding all definitions in your project and include their into pipeline.<br/>
46+
/// Using <see cref="WebApplication"/> for registration.
47+
/// </summary>
48+
/// <remarks>
49+
/// When executing on development environment there are more diagnostic information available on console.
50+
/// </remarks>
51+
/// <param name="source"></param>
2452
public static void UseDefinitions(this WebApplication source)
2553
{
26-
var definitions = source.Services.GetRequiredService<IReadOnlyCollection<IAppDefinition>>();
54+
var logger = source.Services.GetRequiredService<ILogger<AppDefinition>>();
2755
var environment = source.Services.GetRequiredService<IWebHostEnvironment>();
28-
foreach (var endpoint in definitions)
56+
57+
var definitions = source.Services.GetRequiredService<IReadOnlyCollection<IAppDefinition>>();
58+
var instancesOrdered = definitions.Where(x => x.Enabled).OrderBy(x => x.OrderIndex).ToList();
59+
foreach (var endpoint in instancesOrdered)
2960
{
3061
endpoint.ConfigureApplication(source, environment);
3162
}
63+
64+
if (environment.IsDevelopment())
65+
{
66+
logger.LogDebug("Total application definitions configured {0}", instancesOrdered.Count());
67+
}
3268
}
69+
3370
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<Nullable>enable</Nullable>
77
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
88
<Title>Calabonga.AspNetCore.AppDefinitions</Title>
9-
<Version>1.0.0-beta1</Version>
9+
<Version>1.0.0</Version>
1010
<Authors>Calabonga</Authors>
1111
<Company>Calabonga Soft</Company>
1212
<Product>Calabonga.AspNetCore.AppDefinitions</Product>

0 commit comments

Comments
 (0)