Skip to content

Commit 3d703ba

Browse files
committed
Easier way to convert from table entity without any mapped keys
1 parent d4e8486 commit 3d703ba

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,16 @@ Here is an example where a more complicated key was used, which is common in azu
9797
var employee = tableEntity.FromTableEntity<Employee, string, int>(e=>e.Company, pk=>pk.Substring("company_".Length), e => e.Id, rk=>int.Parse(rk.Substring("employee_".Length)));
9898
```
9999
In this example the partitionkey had a prefix of "company_" and the row key had a prefix of "employee_".
100+
101+
### Converting From Table Entity While Ignoring Key Properties (Partition Key and Row Key)
102+
When converting from a table entity, you may not want to populate any fields derived from `PartitionKey` and `RowKey`. One reason for doing this might be that those keys are complex (derived from multiple properties for instance), and you already have those simple properties in your entity.
103+
104+
For your conveninece you can use the simplified `FromTableEntity` method. This is the equivilant of doing
105+
```csharp
106+
var employee = tableEntity.FromTableEntity<Employee,object,object>(null, null, null, null);
107+
```
108+
109+
Example:
110+
```csharp
111+
var employee = tableEntity.FromTableEntity<Employee>();
112+
```

src/TableStorage.Abstractions.TableEntityConverters/EntityConvert.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
8989
rowProp.SetValue(o, convertRowKey(entity.RowKey));
9090
properties.Remove(rowProp);
9191
}
92+
FillProperties(entity, o, properties);
93+
return o;
94+
}
95+
96+
public static T FromTableEntity<T>(this DynamicTableEntity entity) where T : new()
97+
{
98+
return entity.FromTableEntity<T, object, object>(null, null, null, null);
99+
}
100+
101+
private static void FillProperties<T>(DynamicTableEntity entity, T o, List<PropertyInfo> properties) where T : new()
102+
{
92103
foreach (var propertyInfo in properties)
93104
if (entity.Properties.ContainsKey(propertyInfo.Name))
94105
{
@@ -117,7 +128,6 @@ public static T FromTableEntity<T, TP, TR>(this DynamicTableEntity entity,
117128
propertyInfo.SetValue(o, propVal);
118129
}
119130
}
120-
return o;
121131
}
122132

123133
private static DynamicTableEntity CreateTableEntity(object o, List<PropertyInfo> properties,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks>netstandard2.0;netcoreapp2.0;net462</TargetFrameworks>
5-
<Version>1.1.4</Version>
5+
<Version>1.1.5</Version>
66
<Authors>Giovanni Galbo</Authors>
77
<Company>Giovanni Galbo</Company>
88
<Description>Easily convert POCOs (Plain Old CLR Objects) to Azure Table Storage TableEntities and vice versa.</Description>
@@ -11,7 +11,7 @@
1111
<PackageProjectUrl>https://github.com/giometrix/TableStorage.Abstractions.TableEntityConverters</PackageProjectUrl>
1212
<RepositoryUrl>https://github.com/giometrix/TableStorage.Abstractions.TableEntityConverters</RepositoryUrl>
1313
<PackageTags>table-storage azure-table-storage poco table-entities</PackageTags>
14-
<PackageReleaseNotes>Updating nuget packages</PackageReleaseNotes>
14+
<PackageReleaseNotes>Easier way to convert from table entity without any mapped keys</PackageReleaseNotes>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
1717
</PropertyGroup>

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ public void convert_from_entity_table_unmapped_partition_key()
215215
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
216216
}
217217

218+
[Fact]
219+
public void convert_from_entity_table_unmapped_partition_key_and_unmapped_row_key()
220+
{
221+
var emp = new Employee()
222+
{
223+
Company = "Microsoft",
224+
Name = "John Smith",
225+
Department = new Department
226+
{
227+
Name = "QA",
228+
Id = 1,
229+
OptionalId = Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0")
230+
},
231+
Id = 42,
232+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
233+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008 ")
234+
};
235+
var te = emp.ToTableEntity($"company_{emp.Company}", $"employee_{emp.Id}");
236+
var employee = te.FromTableEntity<Employee>();
237+
238+
Assert.Equal(Guid.Parse("12ae85a4-7131-4e8c-af63-074b066412e0"), employee.Department.OptionalId);
239+
}
240+
218241
public class GuidKeyTest
219242
{
220243
public Guid A { get; set; }

0 commit comments

Comments
 (0)