This repository was archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIdUtils.cs
More file actions
167 lines (137 loc) · 3.78 KB
/
IdUtils.cs
File metadata and controls
167 lines (137 loc) · 3.78 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
using System;
using SimpleStack.Extensions;
using SimpleStack.Interfaces;
using System.Linq;
namespace SimpleStack
{
public static class IdUtils<T>
{
internal static Func<T, object> CanGetId;
static IdUtils()
{
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
var hasIdInterfaces = typeof(T).FindInterfaces(
(t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
if (hasIdInterfaces.Length > 0)
{
CanGetId = HasId<T>.GetId;
return;
}
#endif
if (typeof(T).IsClass())
{
if (typeof(T).GetPropertyInfo(IdUtils.IdField) != null
&& typeof(T).GetPropertyInfo(IdUtils.IdField).GetMethodInfo() != null)
{
CanGetId = HasPropertyId<T>.GetId;
return;
}
foreach (var pi in typeof(T).GetPublicProperties()
.Where(pi => pi.CustomAttributes()
.Cast<Attribute>()
.Any(attr => attr.GetType().Name == "PrimaryKeyAttribute")))
{
CanGetId = StaticAccessors<T>.ValueUnTypedGetPropertyTypeFn(pi);
return;
}
}
CanGetId = x => x.GetHashCode();
}
public static object GetId(T entity)
{
return CanGetId(entity);
}
}
internal static class HasPropertyId<TEntity>
{
private static readonly Func<TEntity, object> GetIdFn;
static HasPropertyId()
{
var pi = typeof(TEntity).GetPropertyInfo(IdUtils.IdField);
GetIdFn = StaticAccessors<TEntity>.ValueUnTypedGetPropertyTypeFn(pi);
}
public static object GetId(TEntity entity)
{
return GetIdFn(entity);
}
}
internal static class HasId<TEntity>
{
private static readonly Func<TEntity, object> GetIdFn;
static HasId()
{
#if MONOTOUCH || SILVERLIGHT
GetIdFn = HasPropertyId<TEntity>.GetId;
#else
var hasIdInterfaces = typeof(TEntity).FindInterfaces(
(t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
var genericArg = hasIdInterfaces[0].GetGenericArguments()[0];
var genericType = typeof(HasIdGetter<,>).MakeGenericType(typeof(TEntity), genericArg);
var oInstanceParam = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "oInstanceParam");
var exprCallStaticMethod = System.Linq.Expressions.Expression.Call
(
genericType,
"GetId",
new Type[0],
oInstanceParam
);
GetIdFn = System.Linq.Expressions.Expression.Lambda<Func<TEntity, object>>
(
exprCallStaticMethod,
oInstanceParam
).Compile();
#endif
}
public static object GetId(TEntity entity)
{
return GetIdFn(entity);
}
}
internal class HasIdGetter<TEntity, TId>
where TEntity : IHasId<TId>
{
public static object GetId(TEntity entity)
{
return entity.Id;
}
}
public static class IdUtils
{
public const string IdField = "Id";
public static object GetObjectId(this object entity)
{
return entity.GetType().GetPropertyInfo(IdField).GetMethodInfo().Invoke(entity, new object[0]);
}
public static object GetId<T>(this T entity)
{
return IdUtils<T>.GetId(entity);
}
public static string CreateUrn<T>(object id)
{
return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
}
public static string CreateUrn(Type type, object id)
{
return string.Format("urn:{0}:{1}", type.Name.ToLower(), id);
}
public static string CreateUrn<T>(this T entity)
{
var id = GetId(entity);
return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
}
public static string CreateCacheKeyPath<T>(string idValue)
{
if (idValue.Length < 4)
{
idValue = idValue.PadLeft(4, '0');
}
idValue = idValue.Replace(" ", "-");
var rootDir = typeof(T).Name;
var dir1 = idValue.Substring(0, 2);
var dir2 = idValue.Substring(2, 2);
var path = string.Format("{1}{0}{2}{0}{3}{0}{4}", StringExtensions.DirSeparatorChar,
rootDir, dir1, dir2, idValue);
return path;
}
}
}