3
using ServiceStack.Common.Reflection;
4
using ServiceStack.DesignPatterns.Model;
6
namespace ServiceStack.Common.Utils
8
public static class IdUtils<T>
10
internal static Func<T, object> CanGetId;
15
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
16
var hasIdInterfaces = typeof(T).FindInterfaces(
17
(t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
19
if (hasIdInterfaces.Length > 0)
21
CanGetId = HasId<T>.GetId;
27
&& typeof(T).GetProperty(IdUtils.IdField) != null
28
&& typeof(T).GetProperty(IdUtils.IdField).GetGetMethod() != null)
30
CanGetId = HasPropertyId<T>.GetId;
34
CanGetId = x => x.GetHashCode();
38
public static object GetId(T entity)
40
return CanGetId(entity);
44
internal static class HasPropertyId<TEntity>
46
private static readonly Func<TEntity, object> GetIdFn;
48
static HasPropertyId()
50
var pi = typeof(TEntity).GetProperty(IdUtils.IdField);
51
GetIdFn = StaticAccessors<TEntity>.ValueUnTypedGetPropertyTypeFn(pi);
54
public static object GetId(TEntity entity)
56
return GetIdFn(entity);
60
internal static class HasId<TEntity>
62
private static readonly Func<TEntity, object> GetIdFn;
67
#if MONOTOUCH || SILVERLIGHT
68
GetIdFn = HasPropertyId<TEntity>.GetId;
70
var hasIdInterfaces = typeof(TEntity).FindInterfaces(
71
(t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
73
var genericArg = hasIdInterfaces[0].GetGenericArguments()[0];
74
var genericType = typeof(HasIdGetter<,>).MakeGenericType(typeof(TEntity), genericArg);
76
var oInstanceParam = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "oInstanceParam");
77
var exprCallStaticMethod = System.Linq.Expressions.Expression.Call
84
GetIdFn = System.Linq.Expressions.Expression.Lambda<Func<TEntity, object>>
92
public static object GetId(TEntity entity)
94
return GetIdFn(entity);
98
internal class HasIdGetter<TEntity, TId>
99
where TEntity : IHasId<TId>
101
public static object GetId(TEntity entity)
107
public static class IdUtils
109
public const string IdField = "Id";
111
public static object GetObjectId(this object entity)
113
return entity.GetType().GetProperty(IdField).GetGetMethod().Invoke(entity, new object[0]);
116
public static object GetId<T>(this T entity)
118
return IdUtils<T>.GetId(entity);
121
public static string CreateUrn<T>(object id)
123
return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
126
public static string CreateUrn(Type type, object id)
128
return string.Format("urn:{0}:{1}", type.Name.ToLower(), id);
131
public static string CreateUrn<T>(this T entity)
133
var id = GetId(entity);
134
return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
137
public static string CreateCacheKeyPath<T>(string idValue)
139
if (idValue.Length < 4)
141
idValue = idValue.PadLeft(4, '0');
143
idValue = idValue.Replace(" ", "-");
145
var rootDir = typeof(T).Name;
146
var dir1 = idValue.Substring(0, 2);
147
var dir2 = idValue.Substring(2, 2);
149
var path = string.Format("{1}{0}{2}{0}{3}{0}{4}", Path.DirectorySeparatorChar,
150
rootDir, dir1, dir2, idValue);
b'\\ No newline at end of file'