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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Tango.BL
{
public abstract class ObservableEntityDTO<DTO, T> : IObservableEntityDTO, IEquatable<T> where T : IObservableEntity where DTO : ObservableEntityDTO<DTO, T>
{
public int ID { get; set; }
public String Guid { get; set; }
public DateTime LastUpdated { get; set; }
public static DTO FromObservable(T observable)
{
if (observable == null) return null;
var dto = Activator.CreateInstance<DTO>();
foreach (var prop in typeof(DTO).GetProperties())
{
var observableProp = typeof(T).GetProperty(prop.Name);
if (observableProp != null)
{
if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String))
{
prop.SetValue(dto, observableProp.GetValue(observable));
}
else if (!prop.PropertyType.IsGenericType)
{
prop.SetValue(dto, prop.PropertyType.GetMethod(nameof(FromObservable), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { observableProp.GetValue(observable) }));
}
else
{
IList collection = Activator.CreateInstance(prop.PropertyType) as IList;
IList source = observableProp.GetValue(observable) as IList;
foreach (var item in source)
{
collection.Add(prop.PropertyType.GenericTypeArguments[0].GetMethod(nameof(FromObservable), BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Invoke(null, new object[] { item }));
}
prop.SetValue(dto, collection);
}
}
}
return dto;
}
public T ToObservable()
{
T observable = Activator.CreateInstance<T>();
MapToObservable(observable);
return observable;
}
public void MapToObservable(T observable)
{
foreach (var prop in typeof(DTO).GetProperties())
{
var observableProp = typeof(T).GetProperty(prop.Name);
if (observableProp != null)
{
if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String))
{
observableProp.SetValue(observable, prop.GetValue(this));
}
else if (!prop.PropertyType.IsGenericType)
{
var propInstance = prop.GetValue(this);
if (propInstance != null)
{
var observableInstance = observableProp.GetValue(observable);
if (observableInstance == null)
{
observableInstance = Activator.CreateInstance(observableProp.PropertyType);
observableProp.SetValue(observable, observableInstance);
}
var method = prop.PropertyType.GetRuntimeMethod(nameof(MapToObservable), new Type[] { observableProp.PropertyType });
method.Invoke(propInstance, new object[] { observableInstance });
}
}
else
{
IList collection = prop.GetValue(this) as IList;
IList observableCollection = observableProp.GetValue(observable) as IList;
if (collection != null)
{
foreach (var item in collection)
{
bool found = false;
foreach (var o in observableCollection)
{
var dtoItem = item as IObservableEntityDTO;
var observableItem = o as IObservableEntity;
if (dtoItem.Guid.Equals(observableItem.Guid))
{
var method = dtoItem.GetType().GetRuntimeMethod(nameof(MapToObservable), new Type[] { observableItem.GetType() });
method.Invoke(dtoItem, new object[] { observableItem });
found = true;
break;
}
}
if (!found)
{
var observableItem = Activator.CreateInstance(observableProp.PropertyType.GenericTypeArguments[0]);
var method = item.GetType().GetRuntimeMethod(nameof(MapToObservable), new Type[] { observableItem.GetType() });
method.Invoke(item, new object[] { observableItem });
observableCollection.Add(observableItem);
}
}
}
}
}
}
}
public bool EqualsToObservable(T observable)
{
if (observable == null) return false;
foreach (var prop in typeof(DTO).GetProperties())
{
var observableProp = typeof(T).GetProperty(prop.Name);
if (observableProp != null)
{
if (prop.PropertyType.IsPrimitive || prop.PropertyType.IsValueType || prop.PropertyType == typeof(String))
{
var observableValue = observableProp.GetValue(observable);
var dtoValue = prop.GetValue(this);
if (dtoValue == null && observableValue != null) return false;
if (dtoValue != null && observableValue == null) return false;
if (dtoValue != null && observableValue != null)
{
if (!dtoValue.Equals(observableValue))
{
return false;
}
}
}
else if (!prop.PropertyType.IsGenericType)
{
var propInstance = prop.GetValue(this);
var observableInstance = observableProp.GetValue(observable);
if (propInstance == null && observableInstance != null) return false;
if (propInstance != null)
{
var method = prop.PropertyType.GetRuntimeMethod(nameof(EqualsToObservable), new Type[] { observableInstance.GetType() });
if (!((bool)method.Invoke(propInstance, new object[] { observableInstance })))
{
return false;
}
}
}
else
{
IList source = observableProp.GetValue(observable) as IList;
IList collection = prop.GetValue(this) as IList;
if (collection == null && source != null) return false;
if (source == null && collection != null) return false;
if (source != null && collection != null)
{
if (source.Count != collection.Count) return false;
for (int i = 0; i < source.Count; i++)
{
var item = collection[i];
var itemSource = source[i];
var method = item.GetType().GetRuntimeMethod(nameof(EqualsToObservable), new Type[] { itemSource.GetType() });
if (item == null && itemSource != null) return false;
if (!((bool)method.Invoke(item, new object[] { itemSource })))
{
return false;
}
}
}
}
}
}
return true;
}
public bool Equals(T observable)
{
return EqualsToObservable(observable);
}
}
}
|