comparison Source/Reflection/MetadataProvider/LinqMetadataProvider.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Data.Linq.Mapping;
3 using System.Linq;
4
5 namespace BLToolkit.Reflection.MetadataProvider
6 {
7 using DataAccess;
8 using Mapping;
9 using Extension;
10
11 public class LinqMetadataProvider : MetadataProviderBase
12 {
13 #region Helpers
14
15 private Type _type;
16 private bool? _isLinqObject;
17 readonly object _sync = new object();
18
19 void EnsureMapper(Type type)
20 {
21 if (_type != type)
22 {
23 _type = type;
24 _isLinqObject = null;
25 }
26 }
27
28 bool IsLinqObject(Type type)
29 {
30 lock (_sync)
31 {
32 EnsureMapper(type);
33
34 if (_isLinqObject == null)
35 {
36 var attrs = type.GetCustomAttributes(typeof(TableAttribute), true);
37 _isLinqObject = attrs.Length > 0;
38 }
39
40 return _isLinqObject.Value;
41 }
42 }
43
44 #endregion
45
46 #region GetFieldName
47
48 public override string GetFieldName(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
49 {
50 if (IsLinqObject(member.TypeAccessor.Type))
51 {
52 var a = member.GetAttribute<ColumnAttribute>();
53
54 if (a != null && !string.IsNullOrEmpty(a.Name))
55 {
56 isSet = true;
57 return a.Name;
58 }
59 }
60
61 return base.GetFieldName(typeExtension, member, out isSet);
62 }
63
64 #endregion
65
66 #region GetFieldStorage
67
68 public override string GetFieldStorage(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
69 {
70 if (IsLinqObject(member.TypeAccessor.Type))
71 {
72 var a = member.GetAttribute<ColumnAttribute>();
73
74 if (a != null && !string.IsNullOrEmpty(a.Name))
75 {
76 isSet = true;
77 return a.Storage;
78 }
79 }
80
81 return base.GetFieldStorage(typeExtension, member, out isSet);
82 }
83
84 #endregion
85
86 #region GetInheritanceDiscriminator
87
88 public override bool GetInheritanceDiscriminator(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
89 {
90 if (IsLinqObject(member.TypeAccessor.Type))
91 {
92 var a = member.GetAttribute<ColumnAttribute>();
93
94 if (a != null && !string.IsNullOrEmpty(a.Name))
95 {
96 isSet = true;
97 return a.IsDiscriminator;
98 }
99 }
100
101 return base.GetInheritanceDiscriminator(typeExtension, member, out isSet);
102 }
103
104 #endregion
105
106 #region GetMapIgnore
107
108 public override bool GetMapIgnore(TypeExtension typeExtension, MemberAccessor member, out bool isSet)
109 {
110 if (member.GetAttribute<AssociationAttribute>() != null)
111 {
112 isSet = true;
113 return true;
114 }
115
116 if (IsLinqObject(member.TypeAccessor.Type))
117 {
118 isSet = true;
119 return member.GetAttribute<ColumnAttribute>() == null;
120 }
121
122 return base.GetMapIgnore(typeExtension, member, out isSet);
123 }
124
125 #endregion
126
127 #region GetNullable
128
129 public override bool GetNullable(MappingSchema mappingSchema, TypeExtension typeExtension, MemberAccessor member, out bool isSet)
130 {
131 if (IsLinqObject(member.TypeAccessor.Type))
132 {
133 var attr = member.GetAttribute<ColumnAttribute>();
134
135 if (attr != null)
136 {
137 isSet = true;
138 return attr.CanBeNull;
139 }
140 }
141
142 return base.GetNullable(mappingSchema, typeExtension, member, out isSet);
143 }
144
145 #endregion
146
147 #region GetTableName
148
149 public override string GetTableName(Type type, ExtensionList extensions, out bool isSet)
150 {
151 if (IsLinqObject(type))
152 {
153 isSet = true;
154
155 var attrs = type.GetCustomAttributes(typeof(TableAttribute), true);
156
157 return ((TableAttribute)attrs[0]).Name;
158 }
159
160 return base.GetTableName(type, extensions, out isSet);
161 }
162
163 #endregion
164
165 #region GetPrimaryKeyOrder
166
167 public override int GetPrimaryKeyOrder(Type type, TypeExtension typeExt, MemberAccessor member, out bool isSet)
168 {
169 if (IsLinqObject(type))
170 {
171 ColumnAttribute a = member.GetAttribute<ColumnAttribute>();
172
173 if (a != null && a.IsPrimaryKey)
174 {
175 isSet = true;
176 return 0;
177 }
178 }
179
180 return base.GetPrimaryKeyOrder(type, typeExt, member, out isSet);
181 }
182
183 #endregion
184
185 #region GetNonUpdatableFlag
186
187 public override NonUpdatableAttribute GetNonUpdatableAttribute(Type type, TypeExtension typeExt, MemberAccessor member, out bool isSet)
188 {
189 if (IsLinqObject(member.TypeAccessor.Type))
190 {
191 var a = member.GetAttribute<ColumnAttribute>();
192
193 if (a != null)
194 {
195 isSet = true;
196 return a.IsDbGenerated ? new IdentityAttribute() : null;
197 }
198 }
199
200 return base.GetNonUpdatableAttribute(type, typeExt, member, out isSet);
201 }
202
203 #endregion
204
205 #region GetAssociation
206
207 public override Association GetAssociation(TypeExtension typeExtension, MemberAccessor member)
208 {
209 if (IsLinqObject(member.TypeAccessor.Type))
210 {
211 var a = member.GetAttribute<System.Data.Linq.Mapping.AssociationAttribute>();
212
213 if (a != null)
214 return new Association(member, Association.ParseKeys(a.ThisKey), Association.ParseKeys(a.OtherKey), a.Storage, true);
215 }
216
217 return base.GetAssociation(typeExtension, member);
218 }
219
220 #endregion
221
222 #region GetInheritanceMapping
223
224 public override InheritanceMappingAttribute[] GetInheritanceMapping(Type type,TypeExtension typeExtension)
225 {
226 if (IsLinqObject(type))
227 {
228 var attrs = type.GetCustomAttributes(typeof(InheritanceMappingAttribute), true);
229
230 if (attrs.Length > 0)
231 return attrs.Select(a => (InheritanceMappingAttribute)a).ToArray();
232 }
233
234 return base.GetInheritanceMapping(type, typeExtension);
235 }
236
237 #endregion
238 }
239 }