Skip to content

Commit

Permalink
Use MEF Metadata for Readers and Writers
Browse files Browse the repository at this point in the history
  • Loading branch information
Boggin committed Aug 27, 2019
1 parent 994b889 commit d7ac412
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 34 deletions.
5 changes: 3 additions & 2 deletions src/FeatureFlagger.ConfigurationReaders/ConfigReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 7,11 @@
using System.Linq;
using FeatureFlagger.Domain;

[ExportMetadata("Name", "CONFIG")]
[Export(typeof(IConfigurationReader))]
[Export(typeof(IConfigurationReader)),
ExportMetadata(Constants.Reader, Constants.Config)]
public class ConfigReader : IConfigurationReader
{
// NOTE: this method is the same for StoreReader.
public Feature Read(string featureName, IEnumerable<Feature> features)
{
return
Expand Down
9 changes: 9 additions & 0 deletions src/FeatureFlagger.ConfigurationReaders/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,9 @@
namespace FeatureFlagger.ConfigurationReaders
{
public static class Constants
{
public const string Config = "CONFIG";
public const string Reader = "READER";
public const string Store = "STORE";
}
}
7 changes: 7 additions & 0 deletions src/FeatureFlagger.ConfigurationReaders/IReaderMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
namespace FeatureFlagger.ConfigurationReaders
{
public interface IReaderMetadata
{
string Reader { get; }
}
}
4 changes: 3 additions & 1 deletion src/FeatureFlagger.ConfigurationReaders/StoreReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 9,11 @@ namespace FeatureFlagger.ConfigurationReaders

using FeatureFlagger.Domain;

[Export(typeof(IConfigurationReader))]
[Export(typeof(IConfigurationReader)),
ExportMetadata(Constants.Reader, Constants.Store)]
public class StoreReader : IConfigurationReader
{
// NOTE: this method is the same for ConfigReader.
public Feature Read(string featureName, IEnumerable<Feature> features)
{
return
Expand Down
8 changes: 8 additions & 0 deletions src/FeatureFlagger.ConfigurationWriters/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,8 @@
namespace FeatureFlagger.ConfigurationWriters
{
public static class Constants
{
public const string Writer = "WRITER";
public const string Store = "STORE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 4,6 @@

public interface IConfigurationWriter
{
/// <summary>
/// The Name of the implementation.
/// </summary>
string Name { get; }

void Create(Feature feature);

void Update(Feature feature);
Expand Down
7 changes: 7 additions & 0 deletions src/FeatureFlagger.ConfigurationWriters/IWriterMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
namespace FeatureFlagger.ConfigurationReaders
{
public interface IWriterMetadata
{
string Writer { get; }
}
}
7 changes: 2 additions & 5 deletions src/FeatureFlagger.ConfigurationWriters/StoreWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 7,17 @@

using Feature = Domain.Feature;

[Export(typeof(IConfigurationWriter))]
[Export(typeof(IConfigurationWriter)),
ExportMetadata(Constants.Writer, Constants.Store)]
public class StoreWriter : IConfigurationWriter
{
private readonly string connectionString;

private StoreWriter()
{
this.Name = "STORE";

this.connectionString = ConfigurationManager.ConnectionStrings["FeatureFlagger_DB"].ConnectionString;
}

public string Name { get; }

public void Create(Feature feature)
{
this.CreateFeature(feature);
Expand Down
42 changes: 21 additions & 21 deletions src/FeatureFlagger/FeatureFlagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 30,11 @@ private FeatureFlagger()
[ImportMany]
public static IEnumerable<IBehaviour> Behaviours { get; private set; }

[ImportMany(typeof(IConfigurationReader))]
private static IEnumerable<IConfigurationReader> Readers { get; set; }
[ImportMany]
private static IEnumerable<Lazy<IConfigurationReader, IReaderMetadata>> Readers { get; set; }

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

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

Expand All @@ -47,34 47,36 @@ public static void SetFeatures()
Features = Reader.ReadAll();
}

private static void SetReader()
private static IConfigurationReader SetReader()
{
// set the configuation reader based on an AppSetting.
var source =
ConfigurationManager.AppSettings["FeatureFlaggerSource"]
?? Constants.Config;
Reader =
Readers.ToList()
.Find(
f =>
f.Name.Equals(
source,
StringComparison.OrdinalIgnoreCase));

return Readers.ToList()
.Find(
f =>
f.Metadata.Reader.Equals(
source,
StringComparison.OrdinalIgnoreCase))
.Value;
}

private static void SetWriter()
private static IConfigurationWriter SetWriter()
{
// set the configuation writer based on an AppSetting.
var source =
ConfigurationManager.AppSettings["FeatureFlaggerSource"]
?? Constants.Config;
Writer =
Writers.ToList()
.Find(
f =>
f.Name.Equals(
source,
StringComparison.OrdinalIgnoreCase));

return Writers.ToList()
.Find(
f =>
f.Metadata.Writer.Equals(
source,
StringComparison.OrdinalIgnoreCase))
.Value;
}

private void SetImports()
Expand All @@ -88,8 90,6 @@ private void SetImports()
var container = configuration.CreateContainer();
container.SatisfyImports(this);
Behaviours = container.GetExports<IBehaviour>();
Readers = container.GetExports<IConfigurationReader>();
Writers = container.GetExports<IConfigurationWriter>();
}
}
}

0 comments on commit d7ac412

Please sign in to comment.