Skip to content

Commit

Permalink
MEF corrections
Browse files Browse the repository at this point in the history
Don't trust the examples from M$
  • Loading branch information
Boggin committed Aug 27, 2019
1 parent d7ac412 commit f527f5e
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 66 deletions.
15 changes: 1 addition & 14 deletions src/FeatureFlagger.ConfigurationReaders/ConfigReader.cs
Original file line number Diff line number Diff line change
@@ -1,28 1,15 @@
namespace FeatureFlagger.ConfigurationReaders
{
using System;
using System.Collections.Generic;
using System.Composition;
using System.Configuration;
using System.Linq;

using FeatureFlagger.Domain;

[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
features.ToList()
.Find(
f =>
f.Name.Equals(
featureName,
StringComparison.OrdinalIgnoreCase));
}

public IEnumerable<Feature> ReadAll()
{
return (List<Feature>)ConfigurationManager.GetSection("features");
Expand Down
2 changes: 1 addition & 1 deletion src/FeatureFlagger.ConfigurationReaders/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 3,7 @@ namespace FeatureFlagger.ConfigurationReaders
public static class Constants
{
public const string Config = "CONFIG";
public const string Reader = "READER";
public const string Reader = "Reader";
public const string Store = "STORE";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 6,6 @@

public interface IConfigurationReader
{
/// <summary>
/// Read a feature from the configuration.
/// </summary>
/// <param name="featureName">The name of the feature.</param>
/// <param name="features">The list of features.</param>
/// <returns>A Feature.</returns>
Feature Read(string featureName, IEnumerable<Feature> features);

/// <summary>
/// Read the configuration.
/// </summary>
Expand Down
7 changes: 0 additions & 7 deletions src/FeatureFlagger.ConfigurationReaders/IReaderMetadata.cs

This file was deleted.

7 changes: 7 additions & 0 deletions src/FeatureFlagger.ConfigurationReaders/ReaderMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
namespace FeatureFlagger.ConfigurationReaders
{
public class ReaderMetadata
{
public string Reader { get; }
}
}
13 changes: 0 additions & 13 deletions src/FeatureFlagger.ConfigurationReaders/StoreReader.cs
Original file line number Diff line number Diff line change
@@ -1,6 1,5 @@
namespace FeatureFlagger.ConfigurationReaders
{
using System;
using System.Collections.Generic;
using System.Composition;
using System.Configuration;
Expand All @@ -13,18 12,6 @@ namespace FeatureFlagger.ConfigurationReaders
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
features.ToList()
.Find(
f =>
f.Name.Equals(
featureName,
StringComparison.OrdinalIgnoreCase));
}

public IEnumerable<Feature> ReadAll()
{
var connectionString = ConfigurationManager.ConnectionStrings["FeatureFlagger_DB"].ConnectionString;
Expand Down
2 changes: 1 addition & 1 deletion src/FeatureFlagger.ConfigurationWriters/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 2,7 @@ namespace FeatureFlagger.ConfigurationWriters
{
public static class Constants
{
public const string Writer = "WRITER";
public const string Writer = "Writer";
public const string Store = "STORE";
}
}
7 changes: 0 additions & 7 deletions src/FeatureFlagger.ConfigurationWriters/IWriterMetadata.cs

This file was deleted.

3 changes: 2 additions & 1 deletion src/FeatureFlagger.ConfigurationWriters/StoreWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 13,8 @@ public class StoreWriter : IConfigurationWriter
{
private readonly string connectionString;

private StoreWriter()
[ImportingConstructor]
public StoreWriter()
{
this.connectionString = ConfigurationManager.ConnectionStrings["FeatureFlagger_DB"].ConnectionString;
}
Expand Down
7 changes: 7 additions & 0 deletions src/FeatureFlagger.ConfigurationWriters/WriterMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 1,7 @@
namespace FeatureFlagger.ConfigurationReaders
{
public class WriterMetadata
{
public string Writer { get; }
}
}
16 changes: 14 additions & 2 deletions src/FeatureFlagger/FeatureFlagExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 1,10 @@
namespace FeatureFlagger
{
using System;
using System.Collections.Generic;
using System.Linq;

using Flag = Domain.Flag;
using global::FeatureFlagger.Domain;

public static class FeatureFlaggerExtensions
{
Expand All @@ -30,7 31,7 @@ private static IEnumerable<Flag> GetFlags(IFeatureFlagger featureFlagger)
featureFlagger.GetType().Name
.Replace("FeatureFlagger", string.Empty);

var feature = FeatureFlagger.Reader.Read(featureName, FeatureFlagger.Features);
var feature = Read(featureName);

// add the feature name to each flag as a property
// (as long as it's not been added already).
Expand All @@ -45,5 46,16 @@ 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));
}
}
}
25 changes: 13 additions & 12 deletions src/FeatureFlagger/FeatureFlagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,45 22,43 @@ static FeatureFlagger()
private FeatureFlagger()
{
SetImports();
SetReader();
SetFeatures();
SetWriter();
Reader = SetReader();
Writer = SetWriter();
Features = Reader.ReadAll();
}

[ImportMany]
public static IEnumerable<IBehaviour> Behaviours { get; private set; }

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

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

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

public static IConfigurationReader Reader { get; private set; }

public static IConfigurationWriter Writer { get; private set; }

public static void SetFeatures()
{
Features = Reader.ReadAll();
}

private static IConfigurationReader SetReader()
{
// set the configuation reader based on an AppSetting.
var source =
ConfigurationManager.AppSettings["FeatureFlaggerSource"]
?? Constants.Config;

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

return reader;
}

private static IConfigurationWriter SetWriter()
Expand All @@ -70,13 68,16 @@ private static IConfigurationWriter SetWriter()
ConfigurationManager.AppSettings["FeatureFlaggerSource"]
?? Constants.Config;

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

return writer;
}

private void SetImports()
Expand Down

0 comments on commit f527f5e

Please sign in to comment.