Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

demonstration of deferred execution on refactor, noticed it was reusi…

…ng call to get property types so introduced a caching layer for that
  • Loading branch information...
commit 30826eb34b6af08a5d766ed6a0bbfcdaef698d4a 1 parent 7605f1b
@bobpace authored
View
1  src/EtlExample.Tests/EtlExample.Tests.csproj
@@ -91,6 +91,7 @@
<Compile Include="EtlTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Refactor\ExtensionsTests.cs" />
+ <Compile Include="Refactor\ProcessRefactorTests.cs" />
<Compile Include="Refactor\PropertyTypeRowBuilderTests.cs" />
<Compile Include="Verifies.cs" />
</ItemGroup>
View
65 src/EtlExample.Tests/Refactor/ProcessRefactorTests.cs
@@ -0,0 +1,65 @@
+using System.Collections.Generic;
+using EtlExample.Refactor;
+using Xunit;
+
+namespace EtlExample.Tests.Refactor
+{
+ public class ProcessRefactorTests : Verifies<ProcessRefactor>
+ {
+ [Fact]
+ public void deferred_execution_on_execute()
+ {
+ var data = new Dictionary<string, IDictionary<string, string>>
+ {
+ {
+ "red",
+ new Dictionary<string, string>
+ {
+ {"locationIdentifier", "1"},
+ {"serviceArea", "redv2"},
+ {"locationDescription", "redv3"},
+ {"locationUrl", "redv4"},
+ {"locationLogoName", "redv5"},
+ {"locationPhotoNames", "redv6"},
+ {"keywords", "redv7"},
+ }
+ },
+ {
+ "blue",
+ new Dictionary<string, string>
+ {
+ {"locationIdentifier", "2"},
+ {"serviceArea", "bluev2"},
+ {"locationDescription", "bluev3"},
+ {"locationUrl", "bluev4"},
+ {"locationLogoName", "bluev5"},
+ {"locationPhotoNames", "bluev6"},
+ {"keywords", "bluev7"},
+ }
+ },
+ {
+ "green",
+ new Dictionary<string, string>
+ {
+ {"locationIdentifier", "2"},
+ {"serviceArea", "greenv2"},
+ {"locationDescription", "greenv3"},
+ {"locationUrl", "greenv4"},
+ {"locationLogoName", "greenv5"},
+ {"locationPhotoNames", "greenv6"},
+ {"keywords", "greenv7"},
+ }
+ },
+ };
+ var process = new ProcessRefactor
+ {
+ CsvFileData = new CsvFileData(data)
+ };
+ var enumerable = process.Execute(null);
+ var enumerator = enumerable.GetEnumerator();
+ while (enumerator.MoveNext())
+ {
+ }
+ }
+ }
+}
View
9 src/EtlExample.Tests/Refactor/PropertyTypeRowBuilderTests.cs
@@ -4,6 +4,7 @@
using System.Linq;
using EtlExample.Refactor;
using Rhino.Etl.Core;
+using Rhino.Mocks;
using Xunit;
namespace EtlExample.Tests.Refactor
@@ -12,6 +13,7 @@ public class PropertyTypeRowBuilderTests : Verifies<IPropertyTypeRowBuilder, Pro
{
readonly int _id;
readonly IDictionary<string, string> _data;
+ IPropertyTypeValuesProvider propertyTypeValuesProvider;
public PropertyTypeRowBuilderTests()
{
@@ -23,9 +25,14 @@ public PropertyTypeRowBuilderTests()
{"sampleName", "value3"},
};
+ propertyTypeValuesProvider = MockFor<IPropertyTypeValuesProvider>();
+ propertyTypeValuesProvider
+ .Expect(x => x.GetPropertyTypes<SampleAddressPropertyType>())
+ .Return(new DefaultPropertyTypeValuesProvider().GetPropertyTypes<SampleAddressPropertyType>());
+
Services.Container.Configure(
x => x.For<PropertyTypeRowBuilder>()
- .Use(() => new PropertyTypeRowBuilder(_id, _data)));
+ .Use(() => new PropertyTypeRowBuilder(propertyTypeValuesProvider, _id, _data)));
}
[Fact]
View
1  src/EtlExample/EtlExample.csproj
@@ -81,6 +81,7 @@
<Compile Include="Refactor\Address.cs" />
<Compile Include="Refactor\AddressPropertyType.cs" />
<Compile Include="Refactor\AddressPropertyTypeRowCreator.cs" />
+ <Compile Include="Refactor\CachingPropertyTypeValuesProvider.cs" />
<Compile Include="Refactor\CreatePropertyTypeRow.cs" />
<Compile Include="Refactor\CsvFileData.cs" />
<Compile Include="Refactor\DefaultPropertyTypeValuesProvider.cs" />
View
27 src/EtlExample/Refactor/CachingPropertyTypeValuesProvider.cs
@@ -0,0 +1,27 @@
+using System;
+using System.Collections.Generic;
+
+namespace EtlExample.Refactor
+{
+ public class CachingPropertyTypeValuesProvider : IPropertyTypeValuesProvider
+ {
+ readonly IPropertyTypeValuesProvider _valuesProvider;
+ readonly IDictionary<Type, IEnumerable<KeyValuePair<int, string>>> _valuesCache;
+
+ public CachingPropertyTypeValuesProvider(IPropertyTypeValuesProvider valuesProvider)
+ {
+ _valuesProvider = valuesProvider;
+ _valuesCache = new Dictionary<Type, IEnumerable<KeyValuePair<int, string>>>();
+ }
+
+ public IEnumerable<KeyValuePair<int, string>> GetPropertyTypes<T>() where T : struct, IConvertible
+ {
+ var type = typeof(T);
+ if (!_valuesCache.ContainsKey(type))
+ {
+ _valuesCache[type] = _valuesProvider.GetPropertyTypes<T>();
+ }
+ return _valuesCache[type];
+ }
+ }
+}
View
12 src/EtlExample/Refactor/CsvFileData.cs
@@ -6,7 +6,17 @@ namespace EtlExample.Refactor
{
public class CsvFileData
{
- IDictionary<string, IDictionary<string, string>> _data;
+ readonly IDictionary<string, IDictionary<string, string>> _data;
+
+ public CsvFileData()
+ : this(new Dictionary<string, IDictionary<string, string>>())
+ {
+ }
+
+ public CsvFileData(IDictionary<string, IDictionary<string,string>> data)
+ {
+ _data = data;
+ }
public IEnumerable<T> SelectRowsAs<T>(Func<IDictionary<string,string>, T> selectAction)
{
View
2  src/EtlExample/Refactor/DefaultPropertyTypeValuesProvider.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
namespace EtlExample.Refactor
@@ -14,6 +15,7 @@ public class DefaultPropertyTypeValuesProvider : IPropertyTypeValuesProvider
throw new ArgumentException("T must be an enum");
}
+ Debug.WriteLine("returning property types");
return Enum.GetValues(enumType)
.Cast<int>()
.Zip(Enum.GetNames(enumType), (key, value) => new KeyValuePair<int, string>(key, value));
View
2  src/EtlExample/Refactor/Extensions.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
namespace EtlExample.Refactor
{
@@ -15,6 +16,7 @@ public static string ChangeFirstLetterToLower(this string input)
public static KeyValuePair<TKey, TValue> ChangeValue<TKey, TValue>(this KeyValuePair<TKey, TValue> item, Func<TValue, TValue> provideNewValue)
{
+ Debug.WriteLine("changing value");
var oldValue = item.Value;
var newValue = provideNewValue(oldValue);
return new KeyValuePair<TKey, TValue>(item.Key, newValue);
View
7 src/EtlExample/Refactor/ProcessRefactor.cs
@@ -1,4 +1,5 @@
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using Rhino.Etl.Core;
@@ -10,12 +11,14 @@ public class ProcessRefactor
public IEnumerable<Row> Execute(IEnumerable<Row> rows)
{
+ var propertyTypeValueProvider =
+ new CachingPropertyTypeValuesProvider(new DefaultPropertyTypeValuesProvider());
return CsvFileData
.SelectRowsAs(x =>
{
+ Debug.WriteLine("selecting as property type row builder");
int id = Address.Load(x["locationIdentifier"]).AddressId;
- //return new PropertyTypeRowBuilder(id, x);
- return new PropertyTypeRowBuilder(id, x);
+ return new PropertyTypeRowBuilder(propertyTypeValueProvider, id, x);
})
.SelectMany(x => x.GetPropertyTypeRowsFor<AddressPropertyType>());
}
View
8 src/EtlExample/Refactor/PropertyTypeRowBuilder.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Linq;
using Rhino.Etl.Core;
@@ -11,11 +12,6 @@ public class PropertyTypeRowBuilder : IPropertyTypeRowBuilder
readonly int _id;
readonly IDictionary<string, string> _data;
- public PropertyTypeRowBuilder(int id, IDictionary<string, string> data)
- : this(new DefaultPropertyTypeValuesProvider(), id, data)
- {
- }
-
public PropertyTypeRowBuilder(IPropertyTypeValuesProvider propertyTypeValuesProvider,
int id,
IDictionary<string, string> data)
@@ -27,6 +23,7 @@ public PropertyTypeRowBuilder(int id, IDictionary<string, string> data)
public IEnumerable<Row> GetPropertyTypeRowsFor<T>(CreatePropertyTypeRow<T> rowCreator) where T : struct, IConvertible
{
+ Debug.WriteLine("getting property type rows for");
return _propertyTypeValuesProvider
.GetPropertyTypes<T>()
.Select(kvp => kvp.ChangeValue(x => x.ChangeFirstLetterToLower()))
@@ -42,6 +39,7 @@ public PropertyTypeRowBuilder(int id, IDictionary<string, string> data)
public static Row DefaultRow(int id, int propertyTypeId, string propertyValue, Type propertyTypeEnum)
{
+ Debug.WriteLine("creating row");
var typeName = propertyTypeEnum.Name;
var idColumnName = string.Format("{0}ID", typeName.Replace("PropertyType", ""));
var propertyTypeIdColumnName = string.Format("{0}ID", typeName);
Please sign in to comment.
Something went wrong with that request. Please try again.