Skip to content

Commit 6dd62ce

Browse files
committed
2 parents 3ec9449 + 53e5ee5 commit 6dd62ce

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,91 @@
11
# TableStorage.Abstractions.TableEntityConverters
22
Easily convert POCOs (Plain Old CLR Objects) to Azure Table Storage TableEntities and vice versa
3+
4+
The Azure Storage SDK requires that objects that it works with to implement the ITableEntity interface. This puts us into one of two places that are often not desirable:
5+
6+
1. You implement the ITableEntity interace, or inherit from TableEntity. This is easy, but now you've got a leaky abstraction, as well as properties that won't make much sense in your domain (e.g. instead of a UserId, you've now got a RowKey, of the wrong type), or you have fields that are out of place, like ETag and Timestamp.
7+
2. You create DTOs to save to ship data back and forth from the domain to Table Storage. This is a common style, but often is overkill, especially if we're just looking for a simple abstraction on top of Azure Table Storage.
8+
9+
This simple library seeks to take care of the mapping for us, so that you can continue to write your domain objects as POCOs, while still being able to leverage the Azure Storage SDK.
10+
11+
The library will convert simple properties to fields in Azure Table Storage. Complex types will serialize as json.
12+
13+
Examples
14+
========
15+
We'll use the following two classes for our examples
16+
17+
```csharp
18+
public class Department
19+
{
20+
public int Id { get; set; }
21+
public string Name { get; set; }
22+
public Guid? OptionalId { get; set; }
23+
24+
}
25+
public class Employee
26+
{
27+
public Employee()
28+
{
29+
30+
}
31+
public string Company { get; set; }
32+
public int Id { get; set; }
33+
public Guid ExternalId { get; set; }
34+
public string Name { get; set; }
35+
public DateTimeOffset HireDate { get; set; }
36+
public DateTimeOffset? TermDate { get; set; }
37+
public Department Department { get; set; }
38+
}
39+
```
40+
41+
## Convert to Table Entity
42+
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.
43+
44+
Example:
45+
```csharp
46+
var emp = new Employee()
47+
{
48+
Company = "Microsoft",
49+
Name = "John Smith",
50+
Department = new Department
51+
{
52+
Name = "QA",
53+
Id = 1,
54+
OptionalId = null
55+
},
56+
Id = 42,
57+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
58+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008")
59+
};
60+
var tableEntity = emp.ToTableEntity(e => e.Company, e => e.Id);
61+
```
62+
63+
Below is an example that uses string keys instead:
64+
```csharp
65+
var emp = new Employee()
66+
{
67+
Name = "John Smith",
68+
Department = new Department
69+
{
70+
Name = "QA",
71+
Id = 1,
72+
OptionalId = null
73+
},
74+
ExternalId = Guid.Parse("e3bf64f4-0537-495c-b3bf-148259d7ed36"),
75+
HireDate = DateTimeOffset.Parse("Thursday, January 31, 2008")
76+
};
77+
var tableEntity = emp.ToTableEntity("Google", "42");
78+
```
79+
## Convert from Table Entity
80+
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```).
81+
82+
Here is a simple example where we specify the partition key (```Company```) and the row key (```Id```):
83+
```csharp
84+
var employee = tableEntity.FromTableEntity<Employee, string, int>(e => e.Company, e => e.Id);
85+
```
86+
87+
Here is an example where a more complicated key was used, which is common in azure table storage because of the lack of indexes.
88+
```csharp
89+
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)));
90+
```
91+
In this example the partitionkey had a prefix of "company_" and the row key had a prefix of "employee_".

0 commit comments

Comments
 (0)