comparison Source/Mapping/DictionaryIndexListMapperT.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3
4 using BLToolkit.Common;
5 using BLToolkit.Reflection;
6
7 namespace BLToolkit.Mapping
8 {
9 public class DictionaryIndexListMapper<T> : IMapDataDestinationList
10 {
11 public DictionaryIndexListMapper(
12 IDictionary<CompoundValue,T> dic,
13 MapIndex index,
14 ObjectMapper objectMapper)
15 {
16 _dic = dic;
17 _mapper = objectMapper;
18
19 _fields = new NameOrIndexParameter[index.Fields.Length];
20 _fromSource = new bool[index.Fields.Length];
21
22 for (int i = 0; i < _fields.Length; i++)
23 {
24 bool fromSource = index.Fields[i].ByName && index.Fields[i].Name[0] == '@';
25
26 _fields[i] = fromSource ? index.Fields[i].Name.Substring(1) : index.Fields[i];
27 _fromSource[i] = fromSource;
28 _isFromSource = _isFromSource || fromSource;
29 _isFromDest = _isFromDest || !fromSource;
30 }
31 }
32
33 private readonly NameOrIndexParameter[] _fields;
34 private readonly IDictionary<CompoundValue,T> _dic;
35 private readonly bool[] _fromSource;
36 private readonly bool _isFromSource;
37 private readonly bool _isFromDest;
38 private ObjectMapper _mapper;
39 private object _newObject;
40 private object[] _indexValue;
41
42 #region IMapDataDestinationList Members
43
44 private void AddObject()
45 {
46 if (_newObject != null)
47 {
48 if (_isFromDest)
49 for (int i = 0; i < _fields.Length; i++)
50 if (!_fromSource[i])
51 _indexValue[i] = _mapper.TypeAccessor[_fields[i]].GetValue(_newObject);
52
53 _dic[new CompoundValue(_indexValue)] = (T)_newObject;
54 }
55 }
56
57 public virtual void InitMapping(InitContext initContext)
58 {
59 ISupportMapping sm = _dic as ISupportMapping;
60
61 if (sm != null)
62 {
63 sm.BeginMapping(initContext);
64
65 if (_mapper != initContext.ObjectMapper)
66 _mapper = initContext.ObjectMapper;
67 }
68 }
69
70 [CLSCompliant(false)]
71 public virtual IMapDataDestination GetDataDestination(InitContext initContext)
72 {
73 return _mapper;
74 }
75
76 public virtual object GetNextObject(InitContext initContext)
77 {
78 AddObject();
79
80 _indexValue = new object[_fields.Length];
81
82 if (_isFromSource)
83 for (int i = 0; i < _fields.Length; i++)
84 if (_fromSource[i])
85 _indexValue[i] = _fields[i].ByName ?
86 initContext.DataSource.GetValue(initContext.SourceObject, _fields[i].Name) :
87 initContext.DataSource.GetValue(initContext.SourceObject, _fields[i].Index);
88
89 return _newObject = _mapper.CreateInstance(initContext);
90 }
91
92 public virtual void EndMapping(InitContext initContext)
93 {
94 AddObject();
95
96 ISupportMapping sm = _dic as ISupportMapping;
97
98 if (sm != null)
99 sm.EndMapping(initContext);
100 }
101
102 #endregion
103 }
104 }