Skip to content

Commit e547e95

Browse files
committed
Full port of Tuple namespace.
SVN: trunk@3155
1 parent 2132b20 commit e547e95

6 files changed

+147
-1
lines changed

src/NHibernate/Mapping/Component.cs

+36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class Component : SimpleValue
1818
private bool dynamic;
1919
private bool isKey;
2020
private string roleName;
21+
private Dictionary<EntityMode, string> tuplizerImpls;
2122

2223
/// <summary></summary>
2324
public int PropertySpan
@@ -210,5 +211,40 @@ public Property GetProperty(string propertyName)
210211
}
211212
throw new MappingException("component property not found: " + propertyName);
212213
}
214+
215+
public virtual void AddTuplizer(EntityMode entityMode, string implClassName)
216+
{
217+
if (tuplizerImpls == null)
218+
tuplizerImpls = new Dictionary<EntityMode, string>();
219+
220+
tuplizerImpls[entityMode] = implClassName;
221+
}
222+
223+
public virtual string GetTuplizerImplClassName(EntityMode mode)
224+
{
225+
// todo : remove this once ComponentMetamodel is complete and merged
226+
if (tuplizerImpls == null)
227+
{
228+
return null;
229+
}
230+
return tuplizerImpls[mode];
231+
}
232+
233+
public virtual IDictionary<EntityMode, string> TuplizerMap
234+
{
235+
get
236+
{
237+
if (tuplizerImpls == null)
238+
return null;
239+
240+
return tuplizerImpls;
241+
}
242+
}
243+
244+
public bool HasPojoRepresentation
245+
{
246+
get { return componentClass != null; }
247+
}
248+
213249
}
214250
}

src/NHibernate/Mapping/PersistentClass.cs

+5
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,12 @@ public virtual System.Type GetTuplizerImplClassName(EntityMode mode)
972972

973973
return new Dictionary<EntityMode, System.Type>(tuplizerImpls);
974974
}
975+
}
975976

977+
public bool HasPojoRepresentation
978+
{
979+
get { return MappedClass != null; }
976980
}
981+
977982
}
978983
}

src/NHibernate/NHibernate-2.0.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@
963963
<Compile Include="Transform\PassThroughResultTransformer.cs" />
964964
<Compile Include="Transform\Transformers.cs" />
965965
<Compile Include="Tuple\Component\AbstractComponentTuplizer.cs" />
966+
<Compile Include="Tuple\Component\ComponentEntityModeToTuplizerMapping.cs" />
966967
<Compile Include="Tuple\Component\ComponentMetamodel.cs" />
967968
<Compile Include="Tuple\Component\DynamicMapComponentTuplizer.cs" />
968969
<Compile Include="Tuple\Component\IComponentTuplizer.cs" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}

src/NHibernate/Tuple/Component/ComponentMetamodel.cs

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public class ComponentMetamodel
1111
private readonly int propertySpan;
1212
private readonly StandardProperty[] properties;
1313
private readonly Dictionary<string, int> propertyIndexes;
14+
private readonly ComponentEntityModeToTuplizerMapping tuplizerMapping;
1415

1516
public ComponentMetamodel(Mapping.Component component)
1617
{
@@ -26,6 +27,7 @@ public ComponentMetamodel(Mapping.Component component)
2627
propertyIndexes[property.Name] = i;
2728
i++;
2829
}
30+
tuplizerMapping = new ComponentEntityModeToTuplizerMapping(component);
2931
}
3032

3133
public string Role
@@ -48,6 +50,11 @@ public StandardProperty[] Properties
4850
get { return properties; }
4951
}
5052

53+
public ComponentEntityModeToTuplizerMapping TuplizerMapping
54+
{
55+
get { return tuplizerMapping; }
56+
}
57+
5158
public StandardProperty GetProperty(int index)
5259
{
5360
if (index < 0 || index >= propertySpan)

src/NHibernate/Tuple/Entity/EntityEntityModeToTuplizerMapping.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public EntityEntityModeToTuplizerMapping(PersistentClass mappedEntity, EntityMet
4141
if (!userSuppliedTuplizerImpls.TryGetValue(EntityMode.Map, out tuplizerImpl))
4242
{
4343
dynamicMapTuplizer = new DynamicMapEntityTuplizer(em, mappedEntity);
44-
userSuppliedTuplizerImpls.Remove(EntityMode.Map);
4544
}
4645
else
4746
{
4847
dynamicMapTuplizer = BuildEntityTuplizer(tuplizerImpl, mappedEntity, em);
48+
userSuppliedTuplizerImpls.Remove(EntityMode.Map);
4949
}
5050

5151
// then the pojo tuplizer, using the dynamic-map tuplizer if no pojo representation is available

0 commit comments

Comments
 (0)