Skip to content

Commit 0e286a9

Browse files
author
Noah Potash
committed
Changed interface to attribute, version to 2.0.0
1 parent 086fa5f commit 0e286a9

File tree

7 files changed

+30
-34
lines changed

7 files changed

+30
-34
lines changed

README.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
[![NuGet Package](https://img.shields.io/nuget/vpre/SapientGuardian.ConfigureAll.svg)](https://www.nuget.org/packages/SapientGuardian.ConfigureAll/)
66

77
## Description
8-
ConfigureAll is a library to make using the Options configuration pattern easier. Just implement IConfigurationObject and ConfigureAll!
8+
ConfigureAll is a library to make using the Options configuration pattern easier. Just add the ConfigurationObject attribute and ConfigureAll!
99

1010
## How to use it
1111

12-
1. Implement the IConfigurationObject interface on your configuration objects by adding a ConfigurationKey property indicating the name of the key in your configuration that maps to this object
12+
1. Add the ConfigurationObject attribute on your configuration objects indicating the name of the key in your configuration that maps to the object
1313
```C#
1414
using SapientGuardian.ConfigureAll;
1515

1616

1717
namespace ConfigureAllTests
1818
{
19+
[ConfigurationObject("TestKey")]
1920
public class TestConfigurationObject : IConfigurationObject
20-
{
21-
public string ConfigurationKey => "TestKey";
22-
21+
{
2322
public string TestValue { get; set; }
2423
}
2524
}

SapientGuardian.ConfigureAll.sln

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{ED4B5916-7C5B-47ED-AADB-D5B42F5BFDF1}"
99
ProjectSection(SolutionItems) = preProject
1010
global.json = global.json
11+
README.md = README.md
1112
EndProjectSection
1213
EndProject
1314
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SapientGuardian.ConfigureAll", "src\SapientGuardian.ConfigureAll\SapientGuardian.ConfigureAll.xproj", "{EE56ACA6-6750-4DE9-8E8E-468076B9EB6A}"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
6+
namespace SapientGuardian.ConfigureAll
7+
{
8+
[AttributeUsage(System.AttributeTargets.Class)]
9+
public class ConfigurationObject : Attribute
10+
{
11+
public string ConfigurationKey => configurationKey;
12+
private string configurationKey;
13+
public ConfigurationObject(string configurationKey)
14+
{
15+
this.configurationKey = configurationKey;
16+
}
17+
}
18+
}

src/SapientGuardian.ConfigureAll/IConfigurationObject.cs

-21
This file was deleted.

src/SapientGuardian.ConfigureAll/ServiceCollectionExtensions.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static IServiceCollection ConfigureAll(this IServiceCollection services,
3737

3838
var configurationObjectTypes = from type in assembly.GetTypes()
3939
let typeInfo = type.GetTypeInfo()
40-
where typeof(IConfigurationObject).GetTypeInfo().IsAssignableFrom(typeInfo)
40+
where typeInfo.GetCustomAttribute<ConfigurationObject>() != null
4141
&& typeInfo.IsClass
4242
select type;
4343

@@ -47,7 +47,7 @@ where typeof(IConfigurationObject).GetTypeInfo().IsAssignableFrom(typeInfo)
4747
var configurationOptionsInterfaceType = typeof(IConfigureOptions<>).MakeGenericType(configurationObjectTypeParameter);
4848
var configureFromConfigurationOptionsType = typeof(ConfigureFromConfigurationOptions<>).MakeGenericType(configurationObjectTypeParameter);
4949

50-
var configurationOptionsObject = (IConfigurationObject)Activator.CreateInstance(configurationObjectType);
50+
var configurationOptionsObject = configurationObjectType.GetTypeInfo().GetCustomAttribute<ConfigurationObject>();
5151

5252
var configureFromConfigurationOptions = Activator.CreateInstance(configureFromConfigurationOptionsType, configuration.GetSection(configurationOptionsObject.ConfigurationKey));
5353

src/SapientGuardian.ConfigureAll/project.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"version": "1.0.0",
3-
"description": "Library to make using the Options configuration pattern easier. Just implement IConfigurationObject and ConfigureAll!",
2+
"version": "2.0.0",
3+
"description": "Library to make using the Options configuration pattern easier. Just add the ConfigurationObject attribute and ConfigureAll!",
44
"authors": [ "SapientGuardian" ],
55
"buildOptions": {
66
"emitEntryPoint": false

test/ConfigureAllTests/TestConfigurationObject.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
namespace ConfigureAllTests
88
{
9-
public class TestConfigurationObject : IConfigurationObject
10-
{
11-
public string ConfigurationKey => "TestKey";
12-
9+
[ConfigurationObject("TestKey")]
10+
public class TestConfigurationObject
11+
{
1312
public string TestValue { get; set; }
1413
}
1514
}

0 commit comments

Comments
 (0)