Skip to content

Commit f8c8901

Browse files
committed
v1.5
1 parent 1dbe529 commit f8c8901

File tree

3 files changed

+48
-12
lines changed

3 files changed

+48
-12
lines changed

README.md

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Examples
1919
========
2020
We'll use the following two classes for our examples
2121

22-
```csharp
22+
```c#
2323
public class Department
2424
{
2525
public int Id { get; set; }
@@ -43,7 +43,7 @@ We'll use the following two classes for our examples
4343
Converting to a table entity is easy. Use the ``.ToTableEntity()`` extension method and specify which properties represent the partition key and row key. If you need to customize how any of those two keys serialize there are overloads that accept string values.
4444

4545
Example:
46-
```csharp
46+
```c#
4747
var emp = new Employee()
4848
{
4949
Company = "Microsoft",
@@ -62,7 +62,7 @@ var emp = new Employee()
6262
```
6363

6464
Below is an example that uses string keys instead:
65-
```csharp
65+
```c#
6666
var emp = new Employee()
6767
{
6868
Name = "John Smith",
@@ -81,7 +81,7 @@ Below is an example that uses string keys instead:
8181
When converting your POCO to a table entity and ultimately serializing and persisting to Azure Table Storage, you may want to ignore some fields. To ignore properties, use the optional ```ignoredProperties``` parameter.
8282

8383
Example:
84-
```csharp
84+
```c#
8585
var tableEntity = emp.ToTableEntity(e=>e.Company, e=>e.Id, e=>e.ExternalId, e=>e.HireDate);
8686
```
8787
In the above example the partition key is ```Company```, the row key is ```Id``` and we ignored ```ExternalId``` and ```HireDate```.
@@ -90,12 +90,12 @@ In the above example the partition key is ```Company```, the row key is ```Id```
9090
Converting from a table entity is just as simple. If the both the partition keys can be converted to simple types, you can use the shorter overloaded extension method (```FromTableEntity```).
9191

9292
Here is a simple example where we specify the partition key (```Company```) and the row key (```Id```):
93-
```csharp
93+
```c#
9494
var employee = tableEntity.FromTableEntity<Employee, string, int>(e => e.Company, e => e.Id);
9595
```
9696

9797
Here is an example where a more complicated key was used, which is common in azure table storage because of the lack of indexes.
98-
```csharp
98+
```c#
9999
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)));
100100
```
101101
In this example the partitionkey had a prefix of "company_" and the row key had a prefix of "employee_".
@@ -104,12 +104,12 @@ In this example the partitionkey had a prefix of "company_" and the row key had
104104
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.
105105

106106
For your conveninece you can use the simplified `FromTableEntity` method. This is the equivilant of doing
107-
```csharp
107+
```c#
108108
var employee = tableEntity.FromTableEntity<Employee,object,object>(null, null, null, null);
109109
```
110110

111111
Example:
112-
```csharp
112+
```c#
113113
var employee = tableEntity.FromTableEntity<Employee>();
114114
```
115115

@@ -119,3 +119,36 @@ Beginning with v1.4, you can now control how the json for complex types are seri
119119
To set the settings globally, use `SetDefaultJsonSerializerSettings`. Use this option if you want to apply them to all table entity conversions.
120120

121121
The other option is pass in the settingds in the newly overloaded `ToTableEntity` and `FromTableEntity` methods.
122+
123+
## Custom Property Conversion For Non-Key Fields
124+
Starting in 1.5 you can specify custom property converters for properties that are not used as Partition or Row Key fields.
125+
126+
This is a niche use case, but useful if you need it, for example, if dates are stored as strings in Azure Table Storage.
127+
128+
Here is the object we'll be using in the example:
129+
```c#
130+
var car = new Car {
131+
Id = "abc",
132+
Make = "BMW",
133+
Model = "M5",
134+
Year = 2022,
135+
ReleaseDate = new DateTime(2022, 3, 1)
136+
};
137+
138+
```
139+
140+
First we need to specify property converters. `PropertyConverters` is a dictionary. The key is the
141+
property name and the value is a `PropertyConverter`, which specifies how to convert to and from `EntityProperty`.
142+
143+
Finally, pass the `PropertyConverters` object when converting to and from your table entities.
144+
145+
Note that in production use cases you don't have to always instantiate your property converters, you should have a single instance and re-use.
146+
147+
```c#
148+
var jsonSerializerSettings = new JsonSerializerSettings();
149+
150+
var carEntity =
151+
car.ToTableEntity(c => c.Year, c => car.Id, jsonSerializerSettings, propertyConverters);
152+
153+
var fromEntity = carEntity.FromTableEntity<Car,int,string>(c=>c.Year, c=>c.Id, jsonSerializerSettings, propertyConverters);
154+
```

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
2323
<AssemblyVersion>1.3.2.0</AssemblyVersion>
2424
<Nullable>disable</Nullable>
25-
<PackageVersion>1.5.0.0-beta</PackageVersion>
25+
<PackageVersion>1.5.0.0</PackageVersion>
2626
</PropertyGroup>
2727

2828
<ItemGroup>

src/TableStorage.Abstractions.UnitTests/EntityConvertTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,18 @@ public void convert_from_entity_table_custom_serialized_property()
556556

557557
var propertyConverters = new PropertyConverters<Car> {
558558
[nameof(car.ReleaseDate)] =
559-
new PropertyConverter<Car>(x =>
559+
new(_ =>
560560
new EntityProperty(car.ReleaseDate.ToString("yyyy-M-d")),
561561
(c,p) =>c.ReleaseDate = DateTime.Parse(p.StringValue)
562562
)
563563
};
564+
565+
var jsonSerializerSettings = new JsonSerializerSettings();
566+
564567
var carEntity =
565-
car.ToTableEntity(c => c.Year, c => car.Id, new JsonSerializerSettings(), propertyConverters);
568+
car.ToTableEntity(c => c.Year, c => car.Id, jsonSerializerSettings, propertyConverters);
566569

567-
var fromEntity = carEntity.FromTableEntity<Car,int,string>(c=>c.Year, c=>c.Id, new JsonSerializerSettings(), propertyConverters);
570+
var fromEntity = carEntity.FromTableEntity<Car,int,string>(c=>c.Year, c=>c.Id, jsonSerializerSettings, propertyConverters);
568571
Assert.Equal(new DateTime(2022, 3, 1), fromEntity.ReleaseDate);
569572
}
570573
}

0 commit comments

Comments
 (0)