Skip to content

Commit 53e5ee5

Browse files
authored
Update README.md
1 parent 1a59722 commit 53e5ee5

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,84 @@ The Azure Storage SDK requires that objects that it works with to implement the
88

99
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.
1010

11+
The library will convert simple properties to fields in Azure Table Storage. Complex types will serialize as json.
12+
1113
Examples
1214
========
1315
We'll use the following two classes for our examples
1416

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```).
1581

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+
```
1686

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)