1313using System . Data . Common ;
1414using NHibernate . Engine ;
1515using NHibernate . SqlTypes ;
16- using NHibernate . Util ;
1716
1817namespace NHibernate . Type
1918{
2019 using System . Threading . Tasks ;
2120 using System . Threading ;
22- public partial class MetaType : AbstractType , IMetaType
21+ public partial class MetaType : AbstractType
2322 {
2423
2524 public override async Task < object > NullSafeGetAsync ( DbDataReader rs , string [ ] names , ISessionImplementor session , object owner , CancellationToken cancellationToken )
2625 {
2726 cancellationToken . ThrowIfCancellationRequested ( ) ;
28- object key = await ( baseType . NullSafeGetAsync ( rs , names , session , owner , cancellationToken ) ) . ConfigureAwait ( false ) ;
29- return key == null ? null : values [ key ] ;
27+ return GetValueForKey ( await ( _baseType . NullSafeGetAsync ( rs , names , session , owner , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
3028 }
3129
32- public override async Task < object > NullSafeGetAsync ( DbDataReader rs , string name , ISessionImplementor session , object owner , CancellationToken cancellationToken )
30+ public override async Task < object > NullSafeGetAsync ( DbDataReader rs , string name , ISessionImplementor session , object owner , CancellationToken cancellationToken )
3331 {
3432 cancellationToken . ThrowIfCancellationRequested ( ) ;
35- object key = await ( baseType . NullSafeGetAsync ( rs , name , session , owner , cancellationToken ) ) . ConfigureAwait ( false ) ;
36- return key == null ? null : values [ key ] ;
33+ return GetValueForKey ( await ( _baseType . NullSafeGetAsync ( rs , name , session , owner , cancellationToken ) ) . ConfigureAwait ( false ) ) ;
3734 }
3835
3936 public override Task NullSafeSetAsync ( DbCommand st , object value , int index , bool [ ] settable , ISessionImplementor session , CancellationToken cancellationToken )
@@ -53,13 +50,25 @@ public override Task NullSafeSetAsync(DbCommand st, object value, int index, boo
5350 }
5451 }
5552
56- public override Task NullSafeSetAsync ( DbCommand st , object value , int index , ISessionImplementor session , CancellationToken cancellationToken )
53+ public override Task NullSafeSetAsync ( DbCommand st , object value , int index , ISessionImplementor session , CancellationToken cancellationToken )
5754 {
5855 if ( cancellationToken . IsCancellationRequested )
5956 {
6057 return Task . FromCanceled < object > ( cancellationToken ) ;
6158 }
62- return baseType . NullSafeSetAsync ( st , value == null ? null : keys [ ( string ) value ] , index , session , cancellationToken ) ;
59+ try
60+ {
61+ // "_keys?[(string) value]" is valid code provided "_keys[(string) value]" can never yield null. It is the
62+ // case because we use a dictionary interface which throws in case of missing key, and because it is not
63+ // possible to have a value associated to a null key since generic dictionaries do not support null keys.
64+ var key = value == null ? null : _keys ? [ ( string ) value ] ?? value ;
65+
66+ return _baseType . NullSafeSetAsync ( st , key , index , session , cancellationToken ) ;
67+ }
68+ catch ( Exception ex )
69+ {
70+ return Task . FromException < object > ( ex ) ;
71+ }
6372 }
6473
6574 public override async Task < bool > IsDirtyAsync ( object old , object current , bool [ ] checkable , ISessionImplementor session , CancellationToken cancellationToken )
0 commit comments