Skip to content

Commit 09ef4a4

Browse files
committed
some remaming + updated readme.
1 parent 40d1c9a commit 09ef4a4

File tree

8 files changed

+125
-109
lines changed

8 files changed

+125
-109
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,61 +75,77 @@ tableStore.Insert(employee);
7575
In this example we ignored the ```Department``` property.
7676

7777
### Calculated Keys
78-
There may be situations where you want the partition key or row key to be calculated from information outside of your object (such as date, which can be a useful partition key) or where you want to use a fixed key (e.g. you don't need a row key).
78+
There may be situations where you want the partition key or row key to be calculated from information outside of your object (such as date, which can be a useful partition key), from multiple properties, or a fixed key (e.g. you don't need a row key).
7979

80-
Here's a contrived example of using date as a partition key:
80+
Here's an example of using the ```CompanyId``` and ```DepartmentId``` as partition keys.
8181

8282
```csharp
83-
var date = new DateTime(2017, 8, 31).ToString("yyyyMMdd");
8483

85-
var tableStore = new PocoTableStore<Employee, int, int>("TestEmployee", "UseDevelopmentStorage=true",
86-
partitionProperty: e => e.CompanyId,
87-
rowProperty: e => e.Id,
88-
calculatedPartitionKey: e => $"{date}_{e.CompanyId}",
89-
calculatedRowKey: e => e.Id.ToString(),
90-
calculatedPartitionKeyFromParameter: x => $"{date}_{x}",
91-
calculatedRowKeyFromParameter: x => x.ToString(),
92-
convertPartitionKey: pk => int.Parse(pk.Substring("yyyyMMdd_".Length)),
93-
convertRowKey: int.Parse);
94-
```
9584

96-
It's a little more complicated than I would have liked (I'm open to suggestions), but I'll try to explain the best I can:
85+
var partitionKeyMapper = new CalculatedKeyMapper<Employee, PartitionKey>(e => $"{e.CompanyId}.{e.Department.Id}", key =>
86+
{
87+
var parts = key.Split('.');
88+
var companyId = int.Parse(parts[0]);
89+
var departmentId = int.Parse(parts[1]);
90+
return new PartitionKey(companyId, departmentId);
91+
}, key=>$"{key.CompanyId}.{key.DepartmentId}");
9792

98-
```calculatedPartitionKey```: how to build the partition key from the object. In our case it's date + ```CompanyId```.
93+
var rowKeyMapper = new KeyMapper<Employee, int>(e => e.Id.ToString(), int.Parse, e => e.Id,
94+
id => id.ToString());
9995

100-
```calculatedRowKey```: how to build the row key from the object. In our case it's ```Id```.
96+
var keysConverter = new CalculatedKeysConverter<Employee, PartitionKey, int>(partitionKeyMapper, rowKeyMapper);
97+
98+
new PocoTableStore<Employee, PartitionKey, int>("TestEmployee", "UseDevelopmentStorage=true", keysConverter);
99+
```
101100

102-
```calculatedPartitionKeyFromParameter```: how to build the calculated partition key from the given partition key. In our case we would provide ```CompanyId``` and the output would be date + ```CompanyId```.
101+
If you used a previous version of this library, you may remember a more complicated, more limited constructor for ```PocoTableStore```. We've simplified things and added some flexibility by introducing ```IKeysConverter```, where implementations encapsulate the rules for converting to/from table storage keys.
103102

104-
```calculatedRowFromParameter```: how to build the calculated row key from the given row key. In our case we would provide ```Id``` and the output would be the stringified version.
103+
Notice that we introduced a new class called ```PartitionKey```. This class is a simple DTO to capture ```CompanyId``` and ```DepartmentId```. A nice side effect of having a class for this is that we gain type safety and intellisense.
104+
105+
```csharp
106+
public class PartitionKey
107+
{
108+
public PartitionKey(int companyId, int departmentId)
109+
{
110+
CompanyId = companyId;
111+
DepartmentId = departmentId;
112+
}
113+
public int CompanyId { get; }
114+
public int DepartmentId { get; }
115+
}
116+
```
117+
Inserting data is the same as always:
105118

106-
Whew.... that was a lot of work. Was it worth it? I sure think so, since it makes working with the records feel much more natural. To insert a record you continue to use syntax such as the following:
107119
```csharp
108120
var employee = new Employee
109121
{
110122
CompanyId = 1,
111-
Id = 142,
123+
Id = 1,
112124
Name = "Mr. Jim CEO",
113125
Department = new Department { Id = 22, Name = "Executive" }
114126
};
115127
tableStore.Insert(employee);
116128
```
129+
In table storage, the partition key for the above example would be "1.22" and it's row key would be "1".
117130

118-
Notice that you don't need to worry about constructing the partition key. Similarly, to query, you continue to query using just the simple identifiers like so:
119-
```charp
120-
var record = tableStore.GetRecord(1, 142);
131+
To retrieve the record, we can use ```PartitionKey``` to build the multi-part key.
132+
```csharp
133+
var record = tableStore.GetRecord(new PartitionKey(1, 22), 1);
121134
```
135+
136+
122137
#### Fixed Keys
123138
Fixed keys are really just a specialization of calculated keys. A scenario that you may run into sometimes is where you only need a single key, which is the case when you only query the data using point queries ("get by id"). In this scenario, you'll probably choose to supply a partition key and not a row key since in this case you'd get better throughput using partition keys in a high volume system (again, we are assuming a point-query-only scenario).
124139

140+
Note that in v1.3 of the library we've simplified fixed key scenarios by introducing a new ```FixedKey``` mapper, which will be consumed by the ```CalculatedKeysConverter```.
141+
125142
Again, we will use a contrived example. Here we have use ```Id``` as partition key , and we always use the word "user" for rowkey, since this will not be used.
126143

127144
```charp
128-
tableStore = new PocoTableStore<Employee, int, int>("TestEmployee", "UseDevelopmentStorage=true",
129-
partitionProperty: e => e.Id, rowProperty: null, calculatedPartitionKey: e => e.Id.ToString(), calculatedRowKey: e => "user",
130-
calculatedPartitionKeyFromParameter: x => x.ToString(),
131-
calculatedRowKeyFromParameter: x => "user",
132-
convertPartitionKey: int.Parse, convertRowKey: null);
145+
var partitionKeyMapper = new KeyMapper<Employee, int>(e =>e.CompanyId.ToString(), int.Parse, e => e.CompanyId, id =>id.ToString());
146+
var rowKeyMapper = new FixedKeyMapper<Employee, int>("user");
147+
148+
var keysConverter = new CalculatedKeysConverter<Employee, int, int>(partitionKeyMapper, rowKeyMapper);
133149
```
134150

135151
Inserting the data remains the same:

0 commit comments

Comments
 (0)