/
StructuralType.cs
169 lines (137 loc) · 5.3 KB
/
StructuralType.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using Breeze.Core;
namespace Breeze.NetClient {
public class StructuralTypeCollection : MapCollection<String, StructuralType> {
protected override String GetKeyForItem(StructuralType item) {
return item.ShortName + ":#" + item.Namespace;
}
}
public abstract class StructuralType {
public StructuralType() {
Warnings = new List<string>();
MetadataStore = MetadataStore.Instance;
}
public static String ClrTypeNameToStructuralTypeName(String clrTypeName) {
if (String.IsNullOrEmpty(clrTypeName)) return null;
var entityTypeNameNoAssembly = clrTypeName.Split(',')[0];
var nameParts = entityTypeNameNoAssembly.Split('.');
String ns;
var shortName = nameParts[nameParts.Length - 1];
if (nameParts.Length > 1) {
ns = String.Join(".", nameParts.Take(nameParts.Length - 1));
} else {
ns = "";
}
var typeName = StructuralType.QualifyTypeName(shortName, ns);
return typeName;
}
public static string ClrTypeToStructuralTypeName(Type clrType) {
return QualifyTypeName(clrType.Name, clrType.Namespace);
}
public static String QualifyTypeName(String shortName, String ns) {
return shortName + ":#" + ns;
}
public static bool IsQualifiedTypeName(String entityTypeName) {
return entityTypeName.IndexOf(":#") >= 0;
}
public MetadataStore MetadataStore { get; internal set; }
public String Name {
get { return QualifyTypeName(ShortName, Namespace); }
}
public Type ClrType {
get {
if (_clrType == null) {
_clrType = MetadataStore.GetClrTypeFor(this);
if (_clrType == null) {
throw new Exception("Unable to locate a CLR type corresponding to: " + this.Name);
}
}
return _clrType;
}
internal set {
_clrType = value;
}
}
private Type _clrType;
public String ShortName { get; internal set; }
public String Namespace { get; internal set;}
public dynamic Custom { get; set; }
public bool IsAbstract { get; internal set; }
// TODO: determine if this is still needed;
public bool IsAnonymous { get; internal set; }
public List<String> Warnings { get; internal set; }
public abstract bool IsEntityType { get; }
public virtual IEnumerable<StructuralProperty> Properties {
get { return _dataProperties.Cast<StructuralProperty>(); }
}
public ICollection<DataProperty> DataProperties {
get { return _dataProperties.ReadOnlyValues; }
}
public DataProperty GetDataProperty(String dpName) {
return _dataProperties[dpName];
}
public virtual StructuralProperty GetProperty(String propName) {
return _dataProperties[propName];
}
internal virtual DataProperty AddDataProperty(DataProperty dp) {
dp.ParentType = this;
UpdateClientServerName(dp);
_dataProperties.Add(dp);
if (dp.IsComplexProperty) {
_complexProperties.Add(dp);
}
if (dp.IsUnmapped) {
_unmappedProperties.Add(dp);
}
return dp;
}
public ReadOnlyCollection<DataProperty> ComplexProperties {
get { return _complexProperties.ReadOnlyValues; }
}
public ReadOnlyCollection<DataProperty> UnmappedProperties {
get { return _unmappedProperties.ReadOnlyValues; }
}
public ICollection<Validator> Validators {
get { return _validators; }
}
internal void UpdateClientServerName(StructuralProperty property) {
var nc = MetadataStore.NamingConvention;
if (!String.IsNullOrEmpty(property.Name)) {
property.NameOnServer = nc.Test(property.Name, true);
} else {
property.Name = nc.Test(property.NameOnServer, false);
}
}
internal void UpdateClientServerFkNames(StructuralProperty property) {
// TODO: add check for name roundtriping ( to see if ok)
var nc = MetadataStore.NamingConvention;
var navProp = property as NavigationProperty;
if (navProp != null) {
if (navProp._foreignKeyNames.Count > 0) {
navProp._foreignKeyNamesOnServer = navProp._foreignKeyNames.Select(fkn => nc.Test(fkn, true)).ToSafeList();
} else {
navProp._foreignKeyNames = navProp._foreignKeyNamesOnServer.Select(fkn => nc.Test(fkn, false)).ToSafeList();
}
if (navProp._invForeignKeyNames.Count > 0) {
navProp._invForeignKeyNamesOnServer = navProp._invForeignKeyNames.Select(fkn => nc.Test(fkn, true)).ToSafeList();
} else {
navProp._invForeignKeyNames = navProp._invForeignKeyNamesOnServer.Select(fkn => nc.Test(fkn, false)).ToSafeList();
}
}
}
protected DataPropertyCollection _dataProperties = new DataPropertyCollection();
protected SafeList<DataProperty> _complexProperties = new SafeList<DataProperty>();
protected SafeList<DataProperty> _unmappedProperties = new SafeList<DataProperty>();
protected ValidatorCollection _validators = new ValidatorCollection();
}
internal class TypeNameInfo {
public String ShortTypeName { get; set; }
public String Namespace { get; set; }
public String TypeName { get; set; }
public Boolean IsAnonymous { get; set; }
}
}