Skip to content

Commit

Permalink
Attempt with custom MetadataAttribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Boggin committed Aug 29, 2019
1 parent f527f5e commit a1b726d
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/FeatureFlagger.ConfigurationReaders/ConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,13 1,11 @@
namespace FeatureFlagger.ConfigurationReaders
{
using System.Collections.Generic;
using System.Composition;
using System.Configuration;

using FeatureFlagger.Domain;

[Export(typeof(IConfigurationReader)),
ExportMetadata(Constants.Reader, Constants.Config)]
[ExportReader(Reader = Constants.Config)]
public class ConfigReader : IConfigurationReader
{
public IEnumerable<Feature> ReadAll()
Expand Down
1 change: 0 additions & 1 deletion src/FeatureFlagger.ConfigurationReaders/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,6 @@ namespace FeatureFlagger.ConfigurationReaders
public static class Constants
{
public const string Config = "CONFIG";
public const string Reader = "Reader";
public const string Store = "STORE";
}
}
12 changes: 12 additions & 0 deletions src/FeatureFlagger.ConfigurationReaders/ExportReaderAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
namespace FeatureFlagger.ConfigurationReaders
{
using System;
using System.Composition;

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportReaderAttribute : ExportAttribute
{
public string Reader { get; set; }
}
}
7 changes: 0 additions & 7 deletions src/FeatureFlagger.ConfigurationReaders/ReaderMetadata.cs

This file was deleted.

4 changes: 1 addition & 3 deletions src/FeatureFlagger.ConfigurationReaders/StoreReader.cs
Original file line number Diff line number Diff line change
@@ -1,15 1,13 @@
namespace FeatureFlagger.ConfigurationReaders
{
using System.Collections.Generic;
using System.Composition;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;

using FeatureFlagger.Domain;

[Export(typeof(IConfigurationReader)),
ExportMetadata(Constants.Reader, Constants.Store)]
[ExportReader(Reader = Constants.Store)]
public class StoreReader : IConfigurationReader
{
public IEnumerable<Feature> ReadAll()
Expand Down
1 change: 0 additions & 1 deletion src/FeatureFlagger.ConfigurationWriters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,6 @@ namespace FeatureFlagger.ConfigurationWriters
{
public static class Constants
{
public const string Writer = "Writer";
public const string Store = "STORE";
}
}
12 changes: 12 additions & 0 deletions src/FeatureFlagger.ConfigurationWriters/ExportWriterAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,12 @@
namespace FeatureFlagger.ConfigurationWriters
{
using System;
using System.Composition;

[MetadataAttribute]
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public class ExportWriterAttribute : ExportAttribute
{
public string Writer { get; set; }
}
}
3 changes: 1 addition & 2 deletions src/FeatureFlagger.ConfigurationWriters/StoreWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 7,7 @@

using Feature = Domain.Feature;

[Export(typeof(IConfigurationWriter)),
ExportMetadata(Constants.Writer, Constants.Store)]
[ExportWriter(Writer = Constants.Store)]
public class StoreWriter : IConfigurationWriter
{
private readonly string connectionString;
Expand Down
7 changes: 0 additions & 7 deletions src/FeatureFlagger.ConfigurationWriters/WriterMetadata.cs

This file was deleted.

14 changes: 1 addition & 13 deletions src/FeatureFlagger/FeatureFlagExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
namespace FeatureFlagger
{
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -31,7 30,7 @@ private static IEnumerable<Flag> GetFlags(IFeatureFlagger featureFlagger)
featureFlagger.GetType().Name
.Replace("FeatureFlagger", string.Empty);

var feature = Read(featureName);
var feature = Helpers.Read(featureName);

// add the feature name to each flag as a property
// (as long as it's not been added already).
Expand All @@ -46,16 45,5 @@ private static IEnumerable<Flag> GetFlags(IFeatureFlagger featureFlagger)

return feature.Flags;
}

private static Feature Read(string featureName)
{
return
FeatureFlagger.Features.ToList()
.Find(
f =>
f.Name.Equals(
featureName,
StringComparison.OrdinalIgnoreCase));
}
}
}
9 changes: 7 additions & 2 deletions src/FeatureFlagger/FeatureFlagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 31,10 @@ private FeatureFlagger()
public static IEnumerable<IBehaviour> Behaviours { get; private set; }

[ImportMany]
private static IEnumerable<Lazy<IConfigurationReader, ReaderMetadata>> Readers { get; set; }
private static IEnumerable<ExportFactory<IConfigurationReader, ExportReaderAttribute>> Readers { get; set; }

[ImportMany]
private static IEnumerable<Lazy<IConfigurationWriter, WriterMetadata>> Writers { get; set; }
private static IEnumerable<ExportFactory<IConfigurationWriter, ExportWriterAttribute>> Writers { get; set; }

public static IEnumerable<Feature> Features { get; private set; }

Expand All @@ -49,13 49,16 @@ private static IConfigurationReader SetReader()
ConfigurationManager.AppSettings["FeatureFlaggerSource"]
?? Constants.Config;

var readers = Readers.ToList();

var reader =
Readers.ToList()
.Find(
f =>
f.Metadata.Reader.Equals(
source,
StringComparison.OrdinalIgnoreCase))
.CreateExport()
.Value;

return reader;
Expand All @@ -75,6 78,7 @@ private static IConfigurationWriter SetWriter()
f.Metadata.Writer.Equals(
source,
StringComparison.OrdinalIgnoreCase))
.CreateExport()
.Value;

return writer;
Expand All @@ -91,6 95,7 @@ private void SetImports()
var container = configuration.CreateContainer();
container.SatisfyImports(this);
Behaviours = container.GetExports<IBehaviour>();
Readers = container.GetExports<ExportFactory<IConfigurationReader, ExportReaderAttribute>>();
}
}
}
19 changes: 19 additions & 0 deletions src/FeatureFlagger/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,19 @@
namespace FeatureFlagger
{
using System;
using System.Linq;

public class Helpers
{
public static Domain.Feature Read(string featureName)
{
return
FeatureFlagger.Features.ToList()
.Find(
f =>
f.Name.Equals(
featureName,
StringComparison.OrdinalIgnoreCase));
}
}
}

0 comments on commit a1b726d

Please sign in to comment.