/
PropertyInspector.cs
150 lines (124 loc) · 4.18 KB
/
PropertyInspector.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.MappingModel;
namespace FluentNHibernate.Conventions.DslImplementation
{
public class PropertyInspector : IPropertyInspector
{
private readonly InspectorModelMapper<IPropertyInspector, PropertyMapping> propertyMappings = new InspectorModelMapper<IPropertyInspector, PropertyMapping>();
private readonly PropertyMapping mapping;
public PropertyInspector(PropertyMapping mapping)
{
this.mapping = mapping;
propertyMappings.Map(x => x.Insert, x => x.Insert);
propertyMappings.Map(x => x.Update, x => x.Update);
propertyMappings.Map(x => x.Type, x => x.Type);
propertyMappings.Map(x => x.Access, x => x.Access);
propertyMappings.Map(x => x.EntityType, x => x.ContainingEntityType);
propertyMappings.Map(x => x.Formula, x => x.Formula);
propertyMappings.Map(x => x.Name, x => x.Name);
propertyMappings.Map(x => x.OptimisticLock, x => x.OptimisticLock);
propertyMappings.Map(x => x.Generated, x => x.Generated);
}
public bool Insert
{
get { return mapping.Insert; }
}
public bool Update
{
get { return mapping.Update; }
}
public int Length
{
get { return GetValueFromColumns<int>(x => x.Length); }
}
public bool Nullable
{
get { return !GetValueFromColumns<bool>(x => x.NotNull); }
}
public string Formula
{
get { return mapping.Formula; }
}
public TypeReference Type
{
get { return mapping.Type; }
}
public string SqlType
{
get { return GetValueFromColumns<string>(x => x.SqlType); }
}
public bool Unique
{
get { return GetValueFromColumns<bool>(x => x.Unique); }
}
public string UniqueKey
{
get { return GetValueFromColumns<string>(x => x.UniqueKey); }
}
public string Name
{
get { return mapping.Name; }
}
public bool OptimisticLock
{
get { return mapping.OptimisticLock; }
}
public string Generated
{
get { return mapping.Generated; }
}
public IEnumerable<IColumnInspector> Columns
{
get
{
foreach (var column in mapping.Columns)
yield return new ColumnInspector(mapping.ContainingEntityType, column);
}
}
public Access Access
{
get
{
if (mapping.Attributes.IsSpecified(x => x.Access))
return Access.FromString(mapping.Access);
return Access.Unset;
}
}
public Type EntityType
{
get { return mapping.ContainingEntityType; }
}
public string StringIdentifierForModel
{
get { return mapping.Name; }
}
public bool ReadOnly
{
get { return mapping.Insert && mapping.Update; }
}
public PropertyInfo Property
{
get { return mapping.PropertyInfo; }
}
public bool IsSet(PropertyInfo property)
{
return mapping.Attributes.IsSpecified(propertyMappings.Get(property));
}
/// <summary>
/// Gets the requested value off the first column, as all columns are (currently) created equal
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private T GetValueFromColumns<T>(Func<ColumnMapping, object> property)
{
var column = mapping.Columns.FirstOrDefault();
if (column != null)
return (T)property(column);
return default(T);
}
}
}