forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntityModeToTuplizerMapping.cs
68 lines (63 loc) · 2.23 KB
/
EntityModeToTuplizerMapping.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections;
using NHibernate.Util;
namespace NHibernate.Tuple
{
/// <summary> Centralizes handling of <see cref="EntityMode"/> to <see cref="ITuplizer"/> mappings. </summary>
[Serializable]
public abstract class EntityModeToTuplizerMapping
{
// map of EntityMode -> Tuplizer
private readonly IDictionary tuplizers = new SequencedHashMap();
protected internal void AddTuplizer(EntityMode entityMode, ITuplizer tuplizer)
{
tuplizers[entityMode] = tuplizer;
}
/// <summary> Given a supposed instance of an entity/component, guess its entity mode. </summary>
/// <param name="obj">The supposed instance of the entity/component.</param>
/// <returns> The guessed entity mode. </returns>
public virtual EntityMode? GuessEntityMode(object obj)
{
foreach (DictionaryEntry entry in tuplizers)
{
ITuplizer tuplizer = (ITuplizer)entry.Value;
if (tuplizer.IsInstance(obj))
{
return (EntityMode)entry.Key;
}
}
return null;
}
/// <summary>
/// Locate the contained tuplizer responsible for the given entity-mode. If
/// no such tuplizer is defined on this mapping, then return null.
/// </summary>
/// <param name="entityMode">The entity-mode for which the caller wants a tuplizer.
/// </param>
/// <returns> The tuplizer, or null if not found.
/// </returns>
public virtual ITuplizer GetTuplizerOrNull(EntityMode entityMode)
{
return (ITuplizer)tuplizers[entityMode];
}
/// <summary> Locate the tuplizer contained within this mapping which is responsible
/// for the given entity-mode. If no such tuplizer is defined on this
/// mapping, then an exception is thrown.
///
/// </summary>
/// <param name="entityMode">The entity-mode for which the caller wants a tuplizer.
/// </param>
/// <returns> The tuplizer.
/// </returns>
/// <throws> HibernateException Unable to locate the requested tuplizer. </throws>
public virtual ITuplizer GetTuplizer(EntityMode entityMode)
{
ITuplizer tuplizer = GetTuplizerOrNull(entityMode);
if (tuplizer == null)
{
throw new HibernateException("No tuplizer found for entity-mode [" + entityMode + "]");
}
return tuplizer;
}
}
}