Skip to content

Commit

Permalink
Added better app config and moved sql to contrib
Browse files Browse the repository at this point in the history
  • Loading branch information
simonproctor committed Oct 25, 2019
1 parent 00faaf5 commit 9ff702b
Show file tree
Hide file tree
Showing 36 changed files with 1,528 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{0A03F567-003B-4192-BC83-CF12861DFAC9}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>FeatureToggles.Contrib.SqlProvider</RootNamespace>
<AssemblyName>FeatureToggles.Contrib.SqlProvider</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Providers\SqlDataProvider.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\FeatureToggles\FeatureToggles.csproj">
<Project>{ba473c25-d415-4323-8a7b-a4f34cf4fe43}</Project>
<Name>FeatureToggles</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
36 changes: 36 additions & 0 deletions src/FeatureToggles.Contrib.SqlProvider/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("FeatureToggles.Contrib.SqlProvider")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("FeatureToggles.Contrib.SqlProvider")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("0a03f567-003b-4192-bc83-cf12861dfac9")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
// </copyright>
//-----------------------------------------------------------------------

namespace FeatureToggles.Providers
namespace FeatureToggles.Contrib.SqlProvider.Providers
{
using System;
using System.Data;
using System.Data.SqlClient;
using FeatureToggles.Providers;
using Models;

public class SqlDataProvider : IToggleDataProvider
{
Expand Down Expand Up @@ -60,7 +62,60 @@ public Toggle GetFlag(string name)

}

return null;
return Toggle.Empty;
}

public Toggle GetFlag(string name, ToggleData userData)
{
Toggle flag;
try
{
using (SqlConnection conn = new SqlConnection("ToggleFlagConnection"))
{
conn.Open();

using (SqlCommand reader = new SqlCommand("GetFlagWithChecks", conn))
{
reader.CommandType = CommandType.StoredProcedure;
reader.Parameters.AddWithValue("@Name", name);

if (!string.IsNullOrWhiteSpace(userData.UserId))
{
reader.Parameters.AddWithValue("UserId", userData.UserId);
}

if (!string.IsNullOrWhiteSpace(userData.IpAddress))
{
reader.Parameters.AddWithValue("IpAddress", userData.IpAddress);
}

if (!string.IsNullOrWhiteSpace(userData.UserRoles))
{
reader.Parameters.AddWithValue("UserRoles", userData.UserRoles);
}

object result = reader.ExecuteScalar();

if (result == null)
{
flag = Toggle.Empty;
}
else
{
bool enabled = (bool)result;
flag = new Toggle(name, enabled);
}
}
}

return flag;
}
catch (Exception)
{

}

return Toggle.Empty;
}
}
}
6 changes: 6 additions & 0 deletions src/FeatureToggles.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeatureToggles", "FeatureTo
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ToggleTests", "ToggleTests\ToggleTests.csproj", "{C16E711D-78AB-4854-AAEC-51635B5C87A1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FeatureToggles.Contrib.SqlProvider", "FeatureToggles.Contrib.SqlProvider\FeatureToggles.Contrib.SqlProvider.csproj", "{0A03F567-003B-4192-BC83-CF12861DFAC9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{C16E711D-78AB-4854-AAEC-51635B5C87A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C16E711D-78AB-4854-AAEC-51635B5C87A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C16E711D-78AB-4854-AAEC-51635B5C87A1}.Release|Any CPU.Build.0 = Release|Any CPU
{0A03F567-003B-4192-BC83-CF12861DFAC9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0A03F567-003B-4192-BC83-CF12861DFAC9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A03F567-003B-4192-BC83-CF12861DFAC9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A03F567-003B-4192-BC83-CF12861DFAC9}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Core: Feature Toggles</title>
<authors>Codeminers Limited</authors>
<owners>Codeminers Limited</owners>
<version>1.0.0</version>
<version>1.1.0</version>
<projectUrl>https://www.codeminers.co.uk</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A utility library for using feature toggles</description>
Expand Down
19 changes: 18 additions & 1 deletion src/FeatureToggles/Configuration/AppConfigurationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@

//-----------------------------------------------------------------------
// <copyright file="AppConfigurationProvider.cs" company="Code Miners Limited">
// Copyright (c) 2019 Code Miners Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program.If not, see<https://www.gnu.org/licenses/>.
// </copyright>
//-----------------------------------------------------------------------

namespace FeatureToggles.Configuration
{
Expand Down
16 changes: 16 additions & 0 deletions src/FeatureToggles/Configuration/IpAddressesElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace FeatureToggles.Configuration
{
using System.Configuration;

/// <summary>
/// The url element represents the raw Xml config as an object
/// </summary>
public class IpAddressesElement : ConfigurationElement
{
/// <summary>
/// The url element value. This will be the fully qualified url for the endpoint
/// </summary>
[ConfigurationProperty("value", IsRequired = true)]
public string Value => this["value"] as string;
}
}
67 changes: 67 additions & 0 deletions src/FeatureToggles/Configuration/IpAddressesElementCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace FeatureToggles.Configuration
{
using System.Collections.Generic;
using System.Configuration;

[ConfigurationCollection(typeof(IpAddressesElement), AddItemName = "ipaddress", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class IpAddressesElementCollection : ConfigurationElementCollection, IEnumerable<IpAddressesElement>
{
/// <summary>
/// Gets or sets a url element from the collection by index
/// </summary>
/// <param name="index">The index</param>
/// <returns>The url element</returns>
public IpAddressesElement this[int index]
{
get => BaseGet(index) as IpAddressesElement;
set
{
if (Count <= 0)
{
BaseAdd(index, value);
return;
}

if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}

BaseAdd(index, value);
}
}

/// <summary>
/// Iterator for returning url elements from the collection
/// </summary>
/// <returns>A url element</returns>
public new IEnumerator<IpAddressesElement> GetEnumerator()
{
int count = Count;
for (int i = 0; i < count; i++)
{
yield return BaseGet(i) as IpAddressesElement;
}
}

/// <summary>
/// Required protected method for returning a configuration element compatible with this
/// collection
/// </summary>
/// <returns>The configuration element</returns>
protected override ConfigurationElement CreateNewElement()
{
return new IpAddressesElement();
}

/// <summary>
/// Gets the configuration element key from the provided element
/// </summary>
/// <param name="element">The configuration element base class</param>
/// <returns>The actual instance required</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((IpAddressesElement)element).Value;
}
}
}
16 changes: 16 additions & 0 deletions src/FeatureToggles/Configuration/RoleElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace FeatureToggles.Configuration
{
using System.Configuration;

/// <summary>
/// The url element represents the raw Xml config as an object
/// </summary>
public class RoleElement : ConfigurationElement
{
/// <summary>
/// The unique url element key
/// </summary>
[ConfigurationProperty("name", IsRequired = true)]
public string Name => this["name"] as string;
}
}
67 changes: 67 additions & 0 deletions src/FeatureToggles/Configuration/RolesElementCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
namespace FeatureToggles.Configuration
{
using System.Collections.Generic;
using System.Configuration;

[ConfigurationCollection(typeof(ToggleElement), AddItemName = "role", CollectionType = ConfigurationElementCollectionType.BasicMap)]
public class RolesElementCollection : ConfigurationElementCollection, IEnumerable<RoleElement>
{
/// <summary>
/// Gets or sets a url element from the collection by index
/// </summary>
/// <param name="index">The index</param>
/// <returns>The url element</returns>
public RoleElement this[int index]
{
get => BaseGet(index) as RoleElement;
set
{
if (Count <= 0)
{
BaseAdd(index, value);
return;
}

if (BaseGet(index) != null)
{
BaseRemoveAt(index);
}

BaseAdd(index, value);
}
}

/// <summary>
/// Iterator for returning url elements from the collection
/// </summary>
/// <returns>A url element</returns>
public new IEnumerator<RoleElement> GetEnumerator()
{
int count = Count;
for (int i = 0; i < count; i++)
{
yield return BaseGet(i) as RoleElement;
}
}

/// <summary>
/// Required protected method for returning a configuration element compatible with this
/// collection
/// </summary>
/// <returns>The configuration element</returns>
protected override ConfigurationElement CreateNewElement()
{
return new RoleElement();
}

/// <summary>
/// Gets the configuration element key from the provided element
/// </summary>
/// <param name="element">The configuration element base class</param>
/// <returns>The actual instance required</returns>
protected override object GetElementKey(ConfigurationElement element)
{
return ((RoleElement)element).Name;
}
}
}
Loading

0 comments on commit 9ff702b

Please sign in to comment.