|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using NHibernate.Mapping; |
| 4 | +using NHibernate.Util; |
| 5 | + |
| 6 | +namespace NHibernate.Tuple.Component |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Handles mapping <see cref="EntityMode"/>s to <see cref="IComponentTuplizer">ComponentTuplizers</see>. |
| 10 | + /// <p/> |
| 11 | + /// Most of the handling is really in the super class; here we just create |
| 12 | + /// the tuplizers and add them to the superclass |
| 13 | + /// </summary> |
| 14 | + [Serializable] |
| 15 | + public class ComponentEntityModeToTuplizerMapping : EntityModeToTuplizerMapping |
| 16 | + { |
| 17 | + private static readonly System.Type[] componentTuplizerCTORSignature = new System.Type[] { typeof(Mapping.Component) }; |
| 18 | + |
| 19 | + public ComponentEntityModeToTuplizerMapping(Mapping.Component component) |
| 20 | + { |
| 21 | + PersistentClass owner = component.Owner; |
| 22 | + |
| 23 | + // create our own copy of the user-supplied tuplizer impl map |
| 24 | + Dictionary<EntityMode, string> userSuppliedTuplizerImpls; |
| 25 | + if (component.TuplizerMap != null) |
| 26 | + userSuppliedTuplizerImpls = new Dictionary<EntityMode, string>(component.TuplizerMap); |
| 27 | + else |
| 28 | + userSuppliedTuplizerImpls = new Dictionary<EntityMode, string>(); |
| 29 | + |
| 30 | + |
| 31 | + // Build the dynamic-map tuplizer... |
| 32 | + ITuplizer dynamicMapTuplizer; |
| 33 | + string tuplizerImpl; |
| 34 | + if (!userSuppliedTuplizerImpls.TryGetValue(EntityMode.Map,out tuplizerImpl)) |
| 35 | + { |
| 36 | + dynamicMapTuplizer = new DynamicMapComponentTuplizer(component); |
| 37 | + } |
| 38 | + else |
| 39 | + { |
| 40 | + dynamicMapTuplizer = BuildComponentTuplizer(tuplizerImpl, component); |
| 41 | + userSuppliedTuplizerImpls.Remove(EntityMode.Map); |
| 42 | + } |
| 43 | + |
| 44 | + // then the pojo tuplizer, using the dynamic-map tuplizer if no pojo representation is available |
| 45 | + ITuplizer pojoTuplizer; |
| 46 | + string tempObject2; |
| 47 | + userSuppliedTuplizerImpls.TryGetValue(EntityMode.Poco, out tempObject2); |
| 48 | + userSuppliedTuplizerImpls.Remove(EntityMode.Poco); |
| 49 | + tuplizerImpl = tempObject2; |
| 50 | + if (owner.HasPojoRepresentation && component.HasPojoRepresentation) |
| 51 | + { |
| 52 | + if (tuplizerImpl == null) |
| 53 | + { |
| 54 | + pojoTuplizer = new PojoComponentTuplizer(component); |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + pojoTuplizer = BuildComponentTuplizer(tuplizerImpl, component); |
| 59 | + } |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + pojoTuplizer = dynamicMapTuplizer; |
| 64 | + } |
| 65 | + |
| 66 | + // put the "standard" tuplizers into the tuplizer map first |
| 67 | + if (pojoTuplizer != null) |
| 68 | + { |
| 69 | + AddTuplizer(EntityMode.Poco, pojoTuplizer); |
| 70 | + } |
| 71 | + if (dynamicMapTuplizer != null) |
| 72 | + { |
| 73 | + AddTuplizer(EntityMode.Map, dynamicMapTuplizer); |
| 74 | + } |
| 75 | + |
| 76 | + // then handle any user-defined entity modes.. |
| 77 | + foreach (KeyValuePair<EntityMode, string> entry in userSuppliedTuplizerImpls) |
| 78 | + { |
| 79 | + IComponentTuplizer tuplizer = BuildComponentTuplizer(entry.Value, component); |
| 80 | + AddTuplizer(entry.Key, tuplizer); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private IComponentTuplizer BuildComponentTuplizer(string tuplizerImpl, Mapping.Component component) |
| 85 | + { |
| 86 | + try |
| 87 | + { |
| 88 | + System.Type implClass = ReflectHelper.ClassForName(tuplizerImpl); |
| 89 | + return (IComponentTuplizer)implClass.GetConstructor(componentTuplizerCTORSignature).Invoke(new object[] { component }); |
| 90 | + } |
| 91 | + catch (Exception t) |
| 92 | + { |
| 93 | + throw new HibernateException("Could not build tuplizer [" + tuplizerImpl + "]", t); |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
0 commit comments