-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtensions.cs
More file actions
executable file
·189 lines (171 loc) · 6.87 KB
/
Extensions.cs
File metadata and controls
executable file
·189 lines (171 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.Linq;
using Microsoft.WindowsAzure.StorageClient;
using JoshCodes.Core.Urns.Extensions;
using JoshCodes.Web.Attributes.Extensions;
using System.Linq.Expressions;
namespace JoshCodes.Persistence.Azure.Storage.Extensions
{
public static class Extensions
{
public static bool TryParseRowKey(this Uri uri, CloudTableClient tableClient, out string rowKey, out string partitionKey)
{
string ns;
string[] nss;
if (!uri.TryParseUrnNamespaceString(out nss, out ns) || nss.Length < 3)
{
rowKey = null;
partitionKey = null;
return false;
}
rowKey = nss[2];
partitionKey = nss[1];
return true;
}
public static string ParseRowKey(this Uri uri, CloudTableClient tableClient, out string partitionKey)
{
string ns;
string [] nss = uri.ParseUrnNamespaceString(out ns);
var rowKey = nss[2];
partitionKey = nss[1];
return rowKey;
}
internal static Uri BuildUrn<T>(this AzureObjectWrapper<T> defines, string rowKey, string partitionKey, CloudTableClient tableClient)
where T : Entity
{
var nsId = defines.GetType().GetUrnNamespaceIdentifier(true);
var urn = nsId.ToUrn(tableClient.BaseUri.Host, partitionKey, rowKey);
return urn;
}
/// <summary>
/// Get the status code of the response
/// </summary>
/// <param name="exception"></param>
/// <returns>Status code for exception or -1 if response if unavailable</returns>
public static int GetStatusCode(this System.Data.Services.Client.DataServiceRequestException exception)
{
System.Data.Services.Client.DataServiceResponse resp = exception.Response;
int statusCode = -1;
if (resp.IsBatchResponse)
{
statusCode = resp.BatchStatusCode;
}
else if (resp.Any())
{
statusCode = resp.First().StatusCode;
}
return statusCode;
}
// TODO: DRY this out
public static bool IsProblemPreconditionFailed(this Exception exception)
{
var preconditionFailedCode = (int)System.Net.HttpStatusCode.PreconditionFailed;
if(exception is System.Data.Services.Client.DataServiceRequestException)
{
var dsre = (System.Data.Services.Client.DataServiceRequestException)exception;
int statusCode = dsre.GetStatusCode();
return (statusCode == preconditionFailedCode);
}
if(exception is System.Data.Services.Client.DataServiceClientException)
{
var dsce = (System.Data.Services.Client.DataServiceClientException)exception;
return (dsce.StatusCode == preconditionFailedCode);
}
if(exception is StorageClientException)
{
var sce = (StorageClientException)exception;
return (sce.StatusCode == System.Net.HttpStatusCode.PreconditionFailed);
}
return false;
}
public static bool IsProblemResourceAlreadyExists(this Exception exception)
{
if (exception is JoshCodes.Persistence.Azure.Storage.DuplicateResourceException)
{
return true;
}
var conflictFailedCode = (int)System.Net.HttpStatusCode.Conflict;
if (exception is System.Data.Services.Client.DataServiceRequestException)
{
var dsre = (System.Data.Services.Client.DataServiceRequestException)exception;
int statusCode = dsre.GetStatusCode();
return (statusCode == conflictFailedCode);
}
if (exception is System.Data.Services.Client.DataServiceClientException)
{
var dsce = (System.Data.Services.Client.DataServiceClientException)exception;
return (dsce.StatusCode == conflictFailedCode);
}
if (exception is StorageClientException)
{
var sce = (StorageClientException)exception;
return (sce.StatusCode == System.Net.HttpStatusCode.Conflict);
}
return false;
}
public static bool AtomicModification<TEntity, TValue>(
this TEntity entity,
TValue requiredValue, TValue newValue, out TValue currentValue,
TableServiceContext tableServiceContext,
Expression<Func<TEntity, TValue>> propertySelector)
where TValue : IComparable<TValue>
where TEntity : TableServiceEntity
{
// check the the object is in the requested state
currentValue = propertySelector.Compile().Invoke(entity);
if (requiredValue.CompareTo(currentValue) != 0)
{
return false;
}
var bodyType = propertySelector.Body.GetType();
var propertyExpr = (System.Linq.Expressions.MemberExpression)propertySelector.Body;
var memberInfo = (System.Reflection.PropertyInfo)propertyExpr.Member;
memberInfo.SetValue(entity, newValue);
try
{
tableServiceContext.UpdateObject(entity);
tableServiceContext.SaveChanges();
currentValue = newValue;
return true;
}
catch (Exception ex)
{
if (ex.IsProblemPreconditionFailed())
{
return false;
}
throw;
}
}
public static bool AtomicModification<TEntity>(
this TEntity entity,
Func<TEntity, bool> conditionForExecution,
Action<TEntity> updateAction,
Action<TEntity> onSuccess,
TableServiceContext tableServiceContext)
where TEntity : TableServiceEntity
{
// check the the object is in the requested state
if (!conditionForExecution(entity))
{
return false;
}
updateAction(entity);
try
{
tableServiceContext.UpdateObject(entity);
tableServiceContext.SaveChanges();
onSuccess(entity);
return true;
}
catch (Exception ex)
{
if (ex.IsProblemPreconditionFailed())
{
return false;
}
throw;
}
}
}
}