forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleRecord.cs
More file actions
150 lines (129 loc) · 4.97 KB
/
SimpleRecord.cs
File metadata and controls
150 lines (129 loc) · 4.97 KB
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.Dynamic;
using System.Linq;
using System.Text;
using System.Threading;
using Simple.Data.Extensions;
namespace Simple.Data
{
public partial class SimpleRecord : DynamicObject, ICloneable
{
private static readonly DictionaryCloner Cloner = new DictionaryCloner();
private readonly ConcreteObject _concreteObject = new ConcreteObject();
private readonly IDictionary<string, object> _data;
private readonly DataStrategy _database;
private readonly string _tableName;
public SimpleRecord()
{
_data = new Dictionary<string, object>(HomogenizedEqualityComparer.DefaultInstance);
}
public SimpleRecord(DataStrategy database)
{
_data = new Dictionary<string, object>(HomogenizedEqualityComparer.DefaultInstance);
_database = database;
}
public SimpleRecord(IDictionary<string, object> data)
: this(data, null)
{
}
public SimpleRecord(IDictionary<string, object> data, string tableName)
: this(data, tableName, null)
{
}
public SimpleRecord(IDictionary<string, object> data, string tableName, DataStrategy dataStrategy)
{
_tableName = tableName;
_database = dataStrategy;
_data = data ?? new Dictionary<string, object>();
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (_data.ContainsKey(binder.Name))
{
result = _data[binder.Name];
var converted = ConvertResult(result);
if (!ReferenceEquals(result, converted))
_data[binder.Name] = result = converted;
return true;
}
if (_tableName == null)
{
result = null;
return false;
}
if (_database != null)
{
var relatedAdapter = _database.GetAdapter() as IAdapterWithRelation;
if (relatedAdapter != null && relatedAdapter.IsValidRelation(_tableName, binder.Name))
{
result = GetRelatedData(binder, relatedAdapter);
return true;
}
}
return base.TryGetMember(binder, out result);
}
private object GetRelatedData(GetMemberBinder binder, IAdapterWithRelation relatedAdapter)
{
object result;
var related = relatedAdapter.FindRelated(_tableName, _data, binder.Name);
var query = related as SimpleQuery;
if (query != null)
{
query.SetDataStrategy(_database);
result = query;
}
else
{
result = related is IDictionary<string, object>
? (object) new SimpleRecord(related as IDictionary<string, object>, binder.Name, _database)
: new SimpleResultSet(
((IEnumerable<IDictionary<string, object>>) related).Select(
dict => new SimpleRecord(dict, binder.Name, _database)));
}
return result;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
_data[binder.Name] = value;
return true;
}
public override bool TryConvert(ConvertBinder binder, out object result)
{
result = _concreteObject.Get(binder.Type, _data);
return result != null;
}
public override IEnumerable<string> GetDynamicMemberNames()
{
return _data.Keys.AsEnumerable();
}
private object ConvertResult(object result)
{
if (result is SimpleList || result is SimpleRecord) return result;
var subRecord = result as IDictionary<string, object>;
if (subRecord != null)
return new SimpleRecord(subRecord);
var list = result as IEnumerable<object>;
if (list != null)
{
return new SimpleList(list.Select(ConvertResult));
}
var func = result as Func<IDictionary<string, object>, object>;
if (func != null)
{
result = func(_data);
}
return result;
}
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public object Clone()
{
return new SimpleRecord(Cloner.CloneDictionary(_data), _tableName, _database);
}
}
}