Skip to content

Commit 71c601c

Browse files
committed
1. Added ability to ignore properties when converting to table entity.
2. Updated to .net core 2.0
1 parent 4b19307 commit 71c601c

File tree

5 files changed

+99
-8
lines changed

5 files changed

+99
-8
lines changed

src/TableStorage.Abstractions.TableEntityConverters.DotNet/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.1.0")]
36-
[assembly: AssemblyFileVersion("1.0.1.0")]
35+
[assembly: AssemblyVersion("1.1.0.0")]
36+
[assembly: AssemblyFileVersion("1.1.0.0")]

src/TableStorage.Abstractions.TableEntityConverters/EntityConvert.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ namespace TableStorage.Abstractions.TableEntityConverters
1010
{
1111
public static class EntityConvert
1212
{
13-
public static DynamicTableEntity ToTableEntity<T>(this T o, string partitionKey, string rowKey)
13+
public static DynamicTableEntity ToTableEntity<T>(this T o, string partitionKey, string rowKey, params Expression<Func<T, object>>[] ignoredProperties)
1414
{
1515
var type = typeof(T);
1616
var properties = GetProperties<T>(type);
17+
RemoveIgnoredProperties(properties, ignoredProperties);
1718
return CreateTableEntity(o, properties, partitionKey, rowKey);
1819
}
1920

2021

2122
public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, object>> partitionProperty,
22-
Expression<Func<T, object>> rowProperty)
23+
Expression<Func<T, object>> rowProperty, params Expression<Func<T, object>>[] ignoredProperties)
2324
{
2425
var type = typeof(T);
2526
var properties = GetProperties<T>(type);
@@ -34,14 +35,15 @@ public static DynamicTableEntity ToTableEntity<T>(this T o, Expression<Func<T, o
3435

3536
properties.Remove(partitionProp);
3637
properties.Remove(rowProp);
38+
RemoveIgnoredProperties(properties, ignoredProperties);
3739

3840
var partitionKey = partitionProp.GetValue(o).ToString();
3941
var rowKey = rowProp.GetValue(o).ToString();
4042

4143
return CreateTableEntity(o, properties, partitionKey, rowKey);
4244
}
4345

44-
46+
4547
public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
4648
Expression<Func<T, object>> partitionProperty,
4749
Expression<Func<T, object>> rowProperty) where T : new()
@@ -182,6 +184,18 @@ private static List<PropertyInfo> GetProperties<T>(Type type)
182184
return properties;
183185
}
184186

187+
private static void RemoveIgnoredProperties<T>(List<PropertyInfo> properties,
188+
Expression<Func<T, object>>[] ignoredProperties)
189+
{
190+
if (ignoredProperties != null)
191+
{
192+
for (int i = 0; i < ignoredProperties.Length; i++)
193+
{
194+
var ignoredProperty = properties.SingleOrDefault(p => p.Name == GetPropertyNameFromExpression(ignoredProperties[i]));
195+
properties.Remove(ignoredProperty);
196+
}
197+
}
198+
}
185199

186200
internal static string GetPropertyNameFromExpression<T>(Expression<Func<T, object>> exp)
187201
{

src/TableStorage.Abstractions.TableEntityConverters/TableStorage.Abstractions.TableEntityConverters.csproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
5-
<Version>1.0.1</Version>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
<Version>1.1.0</Version>
66
<Authors>Giovanni Galbo</Authors>
77
<Company />
88
<Description>Easily convert POCOs (Plain Old CLR Objects) to Azure Table Storage TableEntities and vice versa. This version is for .net core</Description>
99
<PackageProjectUrl>https://github.com/giometrix/TableStorage.Abstractions.TableEntityConverters</PackageProjectUrl>
1010
<PackageTags>table-storage azure-table-storage poco table-entities</PackageTags>
11+
<AssemblyVersion>1.1.0.0</AssemblyVersion>
12+
<FileVersion>1.1.0.0</FileVersion>
13+
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
17+
<DefineConstants>TRACE;RELEASE;NETCOREAPP2_0</DefineConstants>
1118
</PropertyGroup>
1219

1320
<ItemGroup>

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
using System;
3+
using System.Collections.Generic;
34
using TableStorage.Abstractions.TableEntityConverters;
45
using Xunit;
56

@@ -139,5 +140,74 @@ public void convert_from_entity_table_with_guid_keys()
139140
Assert.Equal(a, convertedObject.A);
140141

141142
}
143+
144+
145+
[Fact]
146+
public void convert_to_entity_table_ignore_simple_properties()
147+
{
148+
var emp = new Employee()
149+
{
150+
Company = "Google",
151+
Name = "John Smith",
152+
Department = new Department
153+
{
154+
Name = "QA",
155+
Id = 1,
156+
OptionalId = null
157+
},
158+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
159+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
160+
};
161+
var tableEntity = emp.ToTableEntity(e=>e.Company, e=>e.Id, e=>e.ExternalId, e=>e.HireDate);
162+
Assert.Equal("Google", tableEntity.PartitionKey);
163+
Assert.False(tableEntity.Properties.ContainsKey("ExternalId") );
164+
Assert.False(tableEntity.Properties.ContainsKey("HireDate"));
165+
}
166+
167+
[Fact]
168+
public void convert_to_entity_table_with_explicit_Keys_with_ignored_simple_properties()
169+
{
170+
var emp = new Employee()
171+
{
172+
Name = "John Smith",
173+
Department = new Department
174+
{
175+
Name = "QA",
176+
Id = 1,
177+
OptionalId = null
178+
},
179+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
180+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
181+
};
182+
var tableEntity = emp.ToTableEntity("Google", "42", e => e.Id, e => e.ExternalId, e => e.HireDate);
183+
Assert.Equal("Google", tableEntity.PartitionKey);
184+
Assert.False(tableEntity.Properties.ContainsKey("ExternalId"));
185+
Assert.False(tableEntity.Properties.ContainsKey("HireDate"));
186+
}
187+
188+
189+
[Fact]
190+
public void convert_to_entity_table_ignore_complex_properties()
191+
{
192+
var emp = new Employee()
193+
{
194+
Company = "Google",
195+
Name = "John Smith",
196+
Department = new Department
197+
{
198+
Name = "QA",
199+
Id = 1,
200+
OptionalId = null
201+
},
202+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
203+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
204+
};
205+
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id, e => e.Department);
206+
Assert.Equal("Google", tableEntity.PartitionKey);
207+
Assert.True(tableEntity.Properties.ContainsKey("ExternalId"));
208+
Assert.True(tableEntity.Properties.ContainsKey("HireDate"));
209+
Assert.False(tableEntity.Properties.ContainsKey("DepartmentJson"));
210+
}
211+
142212
}
143213
}

src/TableStorage.Abstractions.UnitTests/TableStorage.Abstractions.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>

0 commit comments

Comments
 (0)