1- namespace DaJet . TypeSystem
1+ using System . Collections ;
2+ using System . Dynamic ;
3+
4+ namespace DaJet . TypeSystem
25{
3- /// <summary>
4- /// Универсальный объект представления данных, имеющий произвольное количество свойств, создаваемых или удаляемых программно.
5- /// Экземпляры данного класса предназначены для представления данных запросов, объектов конфигураций баз данных, а также
6- /// любых иных динамически изменяемых структур данных и переноса этих данных между программными компонентами.
7- /// </summary>
8- public sealed class DataObject : Dictionary < string , object >
6+ public sealed class DataObject : DynamicObject , IEnumerable < KeyValuePair < string , object > >
97 {
10- public DataObject ( ) : base ( ) { }
11- public DataObject ( int capacity ) : base ( capacity ) { }
12- /// <summary>
13- /// Replaces an existing property value or adds a new
14- /// property to the object using the provided value.
15- /// </summary>
8+ private readonly Dictionary < string , object > _data ;
9+ public DataObject ( )
10+ {
11+ _data = new Dictionary < string , object > ( ) ;
12+ }
13+ public DataObject ( int capacity )
14+ {
15+ _data = new Dictionary < string , object > ( capacity ) ;
16+ }
17+ public DataObject ( Dictionary < string , object > data )
18+ {
19+ _data = data is not null ? data : new Dictionary < string , object > ( ) ;
20+ }
21+ public bool IsEmpty { get { return _data . Count == 0 ; } }
1622 public void SetValue ( string name , object value )
1723 {
18- if ( ! TryAdd ( name , value ) )
24+ if ( ! _data . TryAdd ( name , value ) )
1925 {
20- this [ name ] = value ;
26+ _data [ name ] = value ;
2127 }
2228 }
29+ public bool TrySetValue ( string name , object value )
30+ {
31+ if ( ! _data . ContainsKey ( name ) )
32+ {
33+ return false ;
34+ }
35+
36+ _data [ name ] = value ;
37+
38+ return true ;
39+ }
40+ public object GetValue ( string name )
41+ {
42+ return _data [ name ] ;
43+ }
44+ public bool TryGetValue ( string name , out object value )
45+ {
46+ return _data . TryGetValue ( name , out value ) ;
47+ }
48+ public void Clear ( )
49+ {
50+ _data . Clear ( ) ;
51+ }
52+
53+ #region "IENUMERABLE IMPLEMENTATION"
54+ IEnumerator IEnumerable . GetEnumerator ( )
55+ {
56+ return GetEnumerator ( ) ;
57+ }
58+ public IEnumerator < KeyValuePair < string , object > > GetEnumerator ( )
59+ {
60+ return _data . GetEnumerator ( ) ;
61+ }
62+ #endregion
63+
64+ #region "DYNAMIC OBJECT IMPLEMENTATION"
65+ public override IEnumerable < string > GetDynamicMemberNames ( )
66+ {
67+ return _data . Keys ;
68+ }
69+ public override bool TrySetMember ( SetMemberBinder binder , object value )
70+ {
71+ return TrySetValue ( binder . Name , value ) ;
72+ }
73+ public override bool TryGetMember ( GetMemberBinder binder , out object value )
74+ {
75+ return _data . TryGetValue ( binder . Name , out value ) ;
76+ }
77+ #endregion
2378 }
2479}
0 commit comments