~ubuntu-branches/ubuntu/trusty/smuxi/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/ServiceStack/src/ServiceStack.Common/Utils/IdUtils.cs

  • Committer: Package Import Robot
  • Author(s): Mirco Bauer
  • Date: 2013-05-25 22:11:31 UTC
  • mfrom: (1.2.12)
  • Revision ID: package-import@ubuntu.com-20130525221131-nd2mc0kzubuwyx20
Tags: 0.8.11-1
* [22d13d5] Imported Upstream version 0.8.11
* [6d2b95a] Refreshed patches
* [89eb66e] Added ServiceStack libraries to smuxi-engine package
* [848ab10] Enable Campfire engine
* [c6dbdc7] Always build db4o for predictable build result
* [13ec489] Exclude OS X specific libraries from dh_clideps

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System;
 
2
using System.IO;
 
3
using ServiceStack.Common.Reflection;
 
4
using ServiceStack.DesignPatterns.Model;
 
5
 
 
6
namespace ServiceStack.Common.Utils
 
7
{
 
8
    public static class IdUtils<T>
 
9
    {
 
10
        internal static Func<T, object> CanGetId;
 
11
 
 
12
        static IdUtils()
 
13
        {
 
14
 
 
15
#if !SILVERLIGHT && !MONOTOUCH && !XBOX
 
16
            var hasIdInterfaces = typeof(T).FindInterfaces(
 
17
                (t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
 
18
 
 
19
            if (hasIdInterfaces.Length > 0)
 
20
            {
 
21
                CanGetId = HasId<T>.GetId;
 
22
                return;
 
23
            }
 
24
#endif
 
25
 
 
26
            if (typeof(T).IsClass
 
27
                && typeof(T).GetProperty(IdUtils.IdField) != null
 
28
                && typeof(T).GetProperty(IdUtils.IdField).GetGetMethod() != null)
 
29
            {
 
30
                CanGetId = HasPropertyId<T>.GetId;
 
31
            }
 
32
            else
 
33
            {
 
34
                CanGetId = x => x.GetHashCode();
 
35
            }
 
36
        }
 
37
 
 
38
        public static object GetId(T entity)
 
39
        {
 
40
            return CanGetId(entity);
 
41
        }
 
42
    }
 
43
 
 
44
    internal static class HasPropertyId<TEntity>
 
45
    {
 
46
        private static readonly Func<TEntity, object> GetIdFn;
 
47
 
 
48
        static HasPropertyId()
 
49
        {
 
50
            var pi = typeof(TEntity).GetProperty(IdUtils.IdField);
 
51
            GetIdFn = StaticAccessors<TEntity>.ValueUnTypedGetPropertyTypeFn(pi);
 
52
        }
 
53
 
 
54
        public static object GetId(TEntity entity)
 
55
        {
 
56
            return GetIdFn(entity);
 
57
        }
 
58
    }
 
59
 
 
60
    internal static class HasId<TEntity>
 
61
    {
 
62
        private static readonly Func<TEntity, object> GetIdFn;
 
63
 
 
64
        static HasId()
 
65
        {
 
66
 
 
67
#if MONOTOUCH || SILVERLIGHT
 
68
            GetIdFn = HasPropertyId<TEntity>.GetId;
 
69
#else
 
70
            var hasIdInterfaces = typeof(TEntity).FindInterfaces(
 
71
                (t, critera) => t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IHasId<>), null);
 
72
 
 
73
            var genericArg = hasIdInterfaces[0].GetGenericArguments()[0];
 
74
            var genericType = typeof(HasIdGetter<,>).MakeGenericType(typeof(TEntity), genericArg);
 
75
 
 
76
            var oInstanceParam = System.Linq.Expressions.Expression.Parameter(typeof(TEntity), "oInstanceParam");
 
77
            var exprCallStaticMethod = System.Linq.Expressions.Expression.Call
 
78
                (
 
79
                    genericType,
 
80
                    "GetId",
 
81
                    new Type[0],
 
82
                    oInstanceParam
 
83
                );
 
84
            GetIdFn = System.Linq.Expressions.Expression.Lambda<Func<TEntity, object>>
 
85
                (
 
86
                    exprCallStaticMethod,
 
87
                    oInstanceParam
 
88
                ).Compile();
 
89
#endif
 
90
        }
 
91
 
 
92
        public static object GetId(TEntity entity)
 
93
        {
 
94
            return GetIdFn(entity);
 
95
        }
 
96
    }
 
97
 
 
98
    internal class HasIdGetter<TEntity, TId>
 
99
        where TEntity : IHasId<TId>
 
100
    {
 
101
        public static object GetId(TEntity entity)
 
102
        {
 
103
            return entity.Id;
 
104
        }
 
105
    }
 
106
 
 
107
    public static class IdUtils
 
108
    {
 
109
        public const string IdField = "Id";
 
110
 
 
111
        public static object GetObjectId(this object entity)
 
112
        {
 
113
            return entity.GetType().GetProperty(IdField).GetGetMethod().Invoke(entity, new object[0]);
 
114
        }
 
115
 
 
116
        public static object GetId<T>(this T entity)
 
117
        {
 
118
            return IdUtils<T>.GetId(entity);
 
119
        }
 
120
 
 
121
        public static string CreateUrn<T>(object id)
 
122
        {
 
123
            return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
 
124
        }
 
125
 
 
126
        public static string CreateUrn(Type type, object id)
 
127
        {
 
128
            return string.Format("urn:{0}:{1}", type.Name.ToLower(), id);
 
129
        }
 
130
 
 
131
        public static string CreateUrn<T>(this T entity)
 
132
        {
 
133
            var id = GetId(entity);
 
134
            return string.Format("urn:{0}:{1}", typeof(T).Name.ToLower(), id);
 
135
        }
 
136
 
 
137
        public static string CreateCacheKeyPath<T>(string idValue)
 
138
        {
 
139
            if (idValue.Length < 4)
 
140
            {
 
141
                idValue = idValue.PadLeft(4, '0');
 
142
            }
 
143
            idValue = idValue.Replace(" ", "-");
 
144
 
 
145
            var rootDir = typeof(T).Name;
 
146
            var dir1 = idValue.Substring(0, 2);
 
147
            var dir2 = idValue.Substring(2, 2);
 
148
 
 
149
            var path = string.Format("{1}{0}{2}{0}{3}{0}{4}", Path.DirectorySeparatorChar,
 
150
                rootDir, dir1, dir2, idValue);
 
151
 
 
152
            return path;
 
153
        }
 
154
 
 
155
    }
 
156
 
 
157
}
 
 
b'\\ No newline at end of file'