comparison Source/Mapping/DictionaryListMapper.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;
3
4 using BLToolkit.Common;
5 using BLToolkit.Reflection;
6
7 namespace BLToolkit.Mapping
8 {
9 public class DictionaryListMapper : IMapDataDestinationList
10 {
11 public DictionaryListMapper(
12 IDictionary dic,
13 NameOrIndexParameter keyField,
14 ObjectMapper objectMapper)
15 {
16 _dic = dic;
17 _mapper = objectMapper;
18 _fromSource = keyField.ByName && keyField.Name[0] == '@';
19 _keyField = _fromSource? keyField.Name.Substring(1): keyField;
20 }
21
22 private readonly IDictionary _dic;
23 private readonly bool _fromSource;
24 private NameOrIndexParameter _keyField;
25 private ObjectMapper _mapper;
26 private object _newObject;
27 private object _keyValue;
28
29 #region IMapDataDestinationList Members
30
31 private void AddObject()
32 {
33 if (_newObject != null)
34 {
35 if (!_fromSource)
36 _keyValue = _mapper.TypeAccessor[_keyField].GetValue(_newObject);
37
38 _dic[_keyValue] = _newObject;
39 }
40 }
41
42 public virtual void InitMapping(InitContext initContext)
43 {
44 ISupportMapping sm = _dic as ISupportMapping;
45
46 if (sm != null)
47 {
48 sm.BeginMapping(initContext);
49
50 if (_mapper != initContext.ObjectMapper)
51 _mapper = initContext.ObjectMapper;
52 }
53 }
54
55 [CLSCompliant(false)]
56 public virtual IMapDataDestination GetDataDestination(InitContext initContext)
57 {
58 return _mapper;
59 }
60
61 static readonly char[] _trim = { ' ' };
62
63 public virtual object GetNextObject(InitContext initContext)
64 {
65 AddObject();
66
67 if (_fromSource)
68 {
69 _keyValue = _keyField.ByName ?
70 initContext.DataSource.GetValue(initContext.SourceObject, _keyField.Name) :
71 initContext.DataSource.GetValue(initContext.SourceObject, _keyField.Index);
72
73 if (Common.Configuration.TrimDictionaryKey && _keyValue is string)
74 _keyValue = _keyValue.ToString().TrimEnd(_trim);
75 }
76
77 return _newObject = _mapper.CreateInstance(initContext);
78 }
79
80 public virtual void EndMapping(InitContext initContext)
81 {
82 AddObject();
83
84 ISupportMapping sm = _dic as ISupportMapping;
85
86 if (sm != null)
87 sm.EndMapping(initContext);
88 }
89
90 #endregion
91 }
92 }