Skip to content

Commit 178eb27

Browse files
authored
Feature/auto configuration (#134)
* Add IServiceResolveCallback Api * Update version of Microsoft.Extensions.Hosting * Refactor services.BuildxxxProvider * Fix csproj * performance optimization * Add AspectCore.Extensions.Configuration project * Refactor * Provider configuration bind * Fix scope resolve
1 parent 76e0ec8 commit 178eb27

32 files changed

+505
-77
lines changed

build/version.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project>
22
<PropertyGroup>
3-
<VersionMajor>0</VersionMajor>
4-
<VersionMinor>7</VersionMinor>
3+
<VersionMajor>1</VersionMajor>
4+
<VersionMinor>0</VersionMinor>
55
<VersionPatch>0</VersionPatch>
66
<VersionQuality></VersionQuality>
77
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectCore.Extensions.Configuration", "src\AspectCore.Extensions.Configuration\AspectCore.Extensions.Configuration.csproj", "{F031B41A-C7D1-475C-AF51-3C163A91DF8B}"
4+
EndProject
5+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "references", "references", "{17B63D57-9385-4790-BC48-8682125AD342}"
6+
EndProject
7+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{BD682B96-A49E-496C-B093-85904FA3474A}"
8+
EndProject
9+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectCore.Extensions.Reflection", "..\reflection\src\AspectCore.Extensions.Reflection\AspectCore.Extensions.Reflection.csproj", "{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2}"
10+
EndProject
11+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectCore.Abstractions", "..\core\src\AspectCore.Abstractions\AspectCore.Abstractions.csproj", "{D2E82FEC-7753-4BC2-BF7F-777C53D009F1}"
12+
EndProject
13+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectCore.Core", "..\core\src\AspectCore.Core\AspectCore.Core.csproj", "{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5}"
14+
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{EE749FBD-FA9E-4391-8CCA-FA7E9DE183BD}"
16+
EndProject
17+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspectCore.Extensions.Configuration.Tests", "test\AspectCore.Extensions.Configuration.Tests\AspectCore.Extensions.Configuration.Tests.csproj", "{7E643D68-6361-4611-BF65-23FF230591A5}"
18+
EndProject
19+
Global
20+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
21+
Debug|Any CPU = Debug|Any CPU
22+
Release|Any CPU = Release|Any CPU
23+
EndGlobalSection
24+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
25+
{F031B41A-C7D1-475C-AF51-3C163A91DF8B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
26+
{F031B41A-C7D1-475C-AF51-3C163A91DF8B}.Debug|Any CPU.Build.0 = Debug|Any CPU
27+
{F031B41A-C7D1-475C-AF51-3C163A91DF8B}.Release|Any CPU.ActiveCfg = Release|Any CPU
28+
{F031B41A-C7D1-475C-AF51-3C163A91DF8B}.Release|Any CPU.Build.0 = Release|Any CPU
29+
{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2}.Release|Any CPU.ActiveCfg = Release|Any CPU
32+
{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2}.Release|Any CPU.Build.0 = Release|Any CPU
33+
{D2E82FEC-7753-4BC2-BF7F-777C53D009F1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
34+
{D2E82FEC-7753-4BC2-BF7F-777C53D009F1}.Debug|Any CPU.Build.0 = Debug|Any CPU
35+
{D2E82FEC-7753-4BC2-BF7F-777C53D009F1}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{D2E82FEC-7753-4BC2-BF7F-777C53D009F1}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
38+
{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
39+
{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
40+
{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5}.Release|Any CPU.Build.0 = Release|Any CPU
41+
{7E643D68-6361-4611-BF65-23FF230591A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
42+
{7E643D68-6361-4611-BF65-23FF230591A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
43+
{7E643D68-6361-4611-BF65-23FF230591A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
44+
{7E643D68-6361-4611-BF65-23FF230591A5}.Release|Any CPU.Build.0 = Release|Any CPU
45+
EndGlobalSection
46+
GlobalSection(NestedProjects) = preSolution
47+
{F031B41A-C7D1-475C-AF51-3C163A91DF8B} = {BD682B96-A49E-496C-B093-85904FA3474A}
48+
{BDB5D3B0-4CC1-4BC4-951A-AACF2C5BB0E2} = {17B63D57-9385-4790-BC48-8682125AD342}
49+
{D2E82FEC-7753-4BC2-BF7F-777C53D009F1} = {17B63D57-9385-4790-BC48-8682125AD342}
50+
{1E167DFD-C7D6-4EB0-9B87-F65AF97764C5} = {17B63D57-9385-4790-BC48-8682125AD342}
51+
{7E643D68-6361-4611-BF65-23FF230591A5} = {EE749FBD-FA9E-4391-8CCA-FA7E9DE183BD}
52+
EndGlobalSection
53+
EndGlobal
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<Import Project="..\..\..\build\common.props" />
3+
<PropertyGroup>
4+
<Description>Configuration extension system for ASP.NET Core via AspectCore-Framework.</Description>
5+
<AssemblyTitle>AspectCore.Extensions.Configuration</AssemblyTitle>
6+
<AllowUnsafeBlocks>False</AllowUnsafeBlocks>
7+
<AssemblyName>AspectCore.Extensions.Configuration</AssemblyName>
8+
<PackageId>AspectCore.Extensions.Configuration</PackageId>
9+
<PackageTags>Reflection;Aop;DynamicProxy;Configuration</PackageTags>
10+
<PackageReleaseNotes>Configuration extension system for ASP.NET Core via AspectCore-Framework.</PackageReleaseNotes>
11+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
12+
</PropertyGroup>
13+
<ItemGroup>
14+
<ProjectReference Include="..\..\..\core\src\AspectCore.Abstractions\AspectCore.Abstractions.csproj" />
15+
<ProjectReference Include="..\..\..\reflection\src\AspectCore.Extensions.Reflection\AspectCore.Extensions.Reflection.csproj" />
16+
</ItemGroup>
17+
<ItemGroup>
18+
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.1.0" />
19+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.1.0" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace AspectCore.Extensions.Configuration
4+
{
5+
public class ConfigurationBindingAttribute : ConfigurationMetadataAttribute
6+
{
7+
public override string[] Sections { get; }
8+
9+
public override string Key { get; } = null;
10+
11+
public override ConfigurationBindType Type { get; } = ConfigurationBindType.Class;
12+
13+
public ConfigurationBindingAttribute(params string[] sections)
14+
{
15+
Sections = sections;
16+
}
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Linq;
3+
4+
namespace AspectCore.Extensions.Configuration
5+
{
6+
public abstract class ConfigurationMetadataAttribute : Attribute, IConfigurationMetadataProvider
7+
{
8+
public abstract string[] Sections { get; }
9+
public abstract string Key { get; }
10+
public abstract ConfigurationBindType Type { get; }
11+
12+
public string GetSection()
13+
{
14+
if (Sections == null || Sections.Length == 0)
15+
{
16+
return null;
17+
}
18+
19+
if (Sections.Length ==1)
20+
{
21+
return Sections[0];
22+
}
23+
24+
return Sections.Aggregate((x, y) => x + ":" + y);
25+
}
26+
}
27+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
3+
namespace AspectCore.Extensions.Configuration
4+
{
5+
public class ConfigurationValueAttribute : ConfigurationMetadataAttribute
6+
{
7+
public override string Key { get; }
8+
9+
public override ConfigurationBindType Type { get; } = ConfigurationBindType.Value;
10+
11+
public override string[] Sections { get; }
12+
13+
public ConfigurationValueAttribute(string key, params string[] sections)
14+
{
15+
Key = key;
16+
Sections = sections;
17+
}
18+
}
19+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System.Reflection;
2+
using AspectCore.Injector;
3+
using AspectCore.Extensions.Reflection;
4+
using Microsoft.Extensions.Configuration;
5+
6+
namespace AspectCore.Extensions.Configuration
7+
{
8+
public sealed class ConfigurationBindResolveCallback : IServiceResolveCallback
9+
{
10+
private const BindingFlags _flags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic;
11+
12+
public object Invoke(IServiceResolver resolver, object instance, ServiceDefinition service)
13+
{
14+
if (instance == null || instance is IConfiguration)
15+
{
16+
return instance;
17+
}
18+
19+
var instanceType = instance.GetType();
20+
var configuration = resolver.ResolveRequired<IConfiguration>();
21+
foreach (var field in instanceType.GetFields(_flags))
22+
{
23+
var reflector = field.GetReflector();
24+
var configurationMetadata = reflector.GetCustomAttribute<ConfigurationMetadataAttribute>();
25+
if (configurationMetadata == null)
26+
{
27+
continue;
28+
}
29+
30+
var section = configurationMetadata.GetSection();
31+
if (configurationMetadata.Type == ConfigurationBindType.Value)
32+
{
33+
var key = section == null ? configurationMetadata.Key : section + ":" + configurationMetadata.Key;
34+
reflector.SetValue(instance, configuration.GetValue(field.FieldType, key));
35+
}
36+
else
37+
{
38+
var configurationSection = section == null ? configuration : configuration.GetSection(section);
39+
reflector.SetValue(instance, configurationSection.Get(field.FieldType));
40+
}
41+
}
42+
43+
return instance;
44+
}
45+
}
46+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace AspectCore.Extensions.Configuration
2+
{
3+
public enum ConfigurationBindType
4+
{
5+
Value,
6+
Class
7+
}
8+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace AspectCore.Extensions.Configuration
2+
{
3+
public interface IConfigurationMetadataProvider
4+
{
5+
string[] Sections { get; }
6+
7+
string Key { get; }
8+
9+
ConfigurationBindType Type { get; }
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System;
2+
using AspectCore.Injector;
3+
4+
namespace AspectCore.Extensions.Configuration
5+
{
6+
public static class ServiceContainerExtensions
7+
{
8+
public static IServiceContainer AddConfigurationInject(this IServiceContainer container)
9+
{
10+
if (container == null)
11+
{
12+
throw new ArgumentNullException(nameof(container));
13+
}
14+
15+
container.AddType<IServiceResolveCallback, ConfigurationBindResolveCallback>(Lifetime.Singleton);
16+
return container;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)