comparison Extensions/JointureAddOn/Mapping/CollectionFullObjectMapper.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 using BLToolkit.Data;
4 using BLToolkit.DataAccess;
5 using BLToolkit.Emit;
6
7 namespace BLToolkit.Mapping
8 {
9 public class CollectionFullObjectMapper : TableDescription, IObjectMapper
10 {
11 private readonly DbManager _db;
12 private readonly FactoryType _factoryType;
13
14 public CollectionFullObjectMapper(DbManager db, FactoryType factoryType)
15 {
16 _db = db;
17 _factoryType = factoryType;
18
19 PropertiesMapping = new List<IMapper>();
20 PrimaryKeyValueGetters = new List<GetHandler>();
21 PrimaryKeyNames = new List<string>();
22 }
23
24 public Type PropertyCollectionType { get; set; }
25
26 #region IMapper Members
27
28 public int DataReaderIndex { get; set; }
29 public SetHandler Setter { get; set; }
30 public Type PropertyType { get; set; }
31 public string PropertyName { get; set; }
32
33 #endregion
34
35 #region IObjectMapper
36
37 public bool IsLazy { get; set; }
38 public bool ContainsLazyChild { get; set; }
39 public GetHandler Getter { get; set; }
40
41 public List<GetHandler> PrimaryKeyValueGetters { get; set; }
42 public Association Association { get; set; }
43
44 public List<string> PrimaryKeyNames { get; set; }
45
46 #endregion
47
48 #region ILazyMapper
49
50 public GetHandler ParentKeyGetter { get; set; }
51
52 #endregion
53
54 public object CreateInstance()
55 {
56 object result = ContainsLazyChild
57 ? (_factoryType == FactoryType.LazyLoading
58 ? TypeFactory.LazyLoading.Create(PropertyType, this, LoadLazy)
59 : TypeFactory.LazyLoadingWithDataBinding.Create(PropertyType, this, LoadLazy))
60 : FunctionFactory.Remote.CreateInstance(PropertyType);
61
62 return result;
63 }
64
65 private object LoadLazy(IMapper mapper, object proxy, Type parentType)
66 {
67 var lazyMapper = (ILazyMapper) mapper;
68 object key = lazyMapper.ParentKeyGetter(proxy);
69
70 var fullSqlQuery = new FullSqlQuery(_db, true);
71 object parentLoadFull = fullSqlQuery.SelectByKey(parentType, key);
72 if (parentLoadFull == null)
73 {
74 object value = Activator.CreateInstance(mapper is CollectionFullObjectMapper
75 ? (mapper as CollectionFullObjectMapper).PropertyCollectionType
76 : mapper.PropertyType);
77 return value;
78 }
79
80 var objectMapper = (IObjectMapper) mapper;
81 return objectMapper.Getter(parentLoadFull);
82 }
83
84 #region IPropertiesMapping Members
85
86 public List<IMapper> PropertiesMapping { get; private set; }
87 public IPropertiesMapping ParentMapping { get; set; }
88
89 #endregion
90 }
91 }