|
4 | 4 | // Created by: Alexey Gamzov
|
5 | 5 | // Created: 2008.05.26
|
6 | 6 |
|
7 |
| -using System; |
8 |
| - |
| 7 | +using Xtensive.Core; |
| 8 | +using Xtensive.Orm.Model; |
9 | 9 | using Xtensive.Tuples;
|
10 | 10 |
|
11 | 11 | namespace Xtensive.Orm.Internals.FieldAccessors
|
12 | 12 | {
|
13 | 13 | internal class EntityFieldAccessor<T> : FieldAccessor<T>
|
14 | 14 | {
|
15 |
| - /// <inheritdoc/> |
16 |
| - public override bool AreSameValues(object oldValue, object newValue) |
| 15 | + private FieldInfo field; |
| 16 | + |
| 17 | + public override void SetFieldInfo(FieldInfo value) |
17 | 18 | {
|
18 |
| - return ReferenceEquals(oldValue, newValue); |
| 19 | + field = field is null ? value : throw Exceptions.AlreadyInitialized("Field"); |
| 20 | + base.SetFieldInfo(value); |
19 | 21 | }
|
| 22 | + |
| 23 | + /// <inheritdoc/> |
| 24 | + public override bool AreSameValues(object oldValue, object newValue) => ReferenceEquals(oldValue, newValue); |
20 | 25 | /// <inheritdoc/>
|
21 | 26 | /// <exception cref="InvalidOperationException">Invalid arguments.</exception>
|
22 | 27 | public override void SetValue(Persistent obj, T value)
|
23 | 28 | {
|
24 |
| - var entity = value as Entity; |
25 |
| - var field = Field; |
| 29 | + var tuple = obj.Tuple; |
| 30 | + if (value is Entity entity) { |
| 31 | + if (entity.Session != obj.Session) |
| 32 | + throw new InvalidOperationException(string.Format(Strings.ExEntityXIsBoundToAnotherSession, entity.Key)); |
26 | 33 |
|
27 |
| - if (!ReferenceEquals(value, null) && entity==null) |
28 |
| - throw new InvalidOperationException(string.Format( |
29 |
| - Strings.ExValueShouldBeXDescendant, WellKnownOrmTypes.Entity)); |
30 |
| - |
31 |
| - if (entity!=null && entity.Session!=obj.Session) |
32 |
| - throw new InvalidOperationException(string.Format( |
33 |
| - Strings.ExEntityXIsBoundToAnotherSession, entity.Key)); |
34 |
| - |
35 |
| - var mappingInfo = field.MappingInfo; |
36 |
| - int fieldIndex = mappingInfo.Offset; |
37 |
| - if (entity==null) { |
38 |
| - int nextFieldIndex = fieldIndex + mappingInfo.Length; |
39 |
| - for (int i = fieldIndex; i < nextFieldIndex; i++) |
40 |
| - obj.Tuple.SetValue(i, null); |
| 34 | + entity.Key.Value.CopyTo(tuple, 0, FieldIndex, field.MappingInfo.Length); |
41 | 35 | }
|
42 | 36 | else {
|
43 |
| - entity.Key.Value.CopyTo(obj.Tuple, 0, fieldIndex, mappingInfo.Length); |
| 37 | + if (!ReferenceEquals(value, null)) |
| 38 | + throw new InvalidOperationException(string.Format(Strings.ExValueShouldBeXDescendant, WellKnownOrmTypes.Entity)); |
| 39 | + |
| 40 | + for (int i = FieldIndex, nextFieldIndex = FieldIndex + field.MappingInfo.Length; i < nextFieldIndex; i++) |
| 41 | + tuple.SetValue(i, null); |
44 | 42 | }
|
45 | 43 | }
|
46 | 44 |
|
47 | 45 | /// <inheritdoc/>
|
48 |
| - public override T GetValue(Persistent obj) |
49 |
| - { |
50 |
| - var field = Field; |
51 |
| - Key key = obj.GetReferenceKey(field); |
52 |
| - if (key is null) |
53 |
| - return default(T); |
54 |
| - return (T) (object) obj.Session.Query.SingleOrDefault(key); |
55 |
| - } |
| 46 | + public override T GetValue(Persistent obj) => |
| 47 | + obj.GetReferenceKey(field) is { } key |
| 48 | + ? (T) (object) obj.Session.Query.SingleOrDefault(key) |
| 49 | + : default; |
56 | 50 | }
|
57 | 51 | }
|
0 commit comments