1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Text ;
4
+ using NHibernate . Impl ;
5
+ using NHibernate . Transform ;
6
+
7
+ namespace NHibernate . Expression
8
+ {
9
+ /// <summary>
10
+ /// Some applications need to create criteria queries in "detached
11
+ /// mode", where the Hibernate session is not available. This class
12
+ /// may be instantiated anywhere, and then a <c>ICriteria</c>
13
+ /// may be obtained by passing a session to
14
+ /// <c>GetExecutableCriteria()</c>. All methods have the
15
+ /// same semantics and behavior as the corresponding methods of the
16
+ /// <c>ICriteria</c> interface.
17
+ /// </summary>
18
+ public class DetachedCriteria
19
+ {
20
+ private CriteriaImpl impl ;
21
+ private ICriteria criteria ;
22
+
23
+ protected DetachedCriteria ( System . Type entityType )
24
+ {
25
+ impl = new CriteriaImpl ( entityType , null ) ;
26
+ criteria = impl ;
27
+ }
28
+
29
+ protected DetachedCriteria ( System . Type entityType , String alias )
30
+ {
31
+ impl = new CriteriaImpl ( entityType , alias , null ) ;
32
+ criteria = impl ;
33
+ }
34
+
35
+ protected DetachedCriteria ( CriteriaImpl impl , ICriteria criteria )
36
+ {
37
+ this . impl = impl ;
38
+ this . criteria = criteria ;
39
+ }
40
+
41
+ /// <summary>
42
+ /// Get an executable instance of <c>Criteria</c>,
43
+ /// to actually run the query.</summary>
44
+ public ICriteria GetExecutableCriteria ( ISession session )
45
+ {
46
+ impl . Session = ( SessionImpl ) session ;
47
+ return impl ;
48
+ }
49
+
50
+ public static DetachedCriteria For ( System . Type entityType )
51
+ {
52
+ return new DetachedCriteria ( entityType ) ;
53
+ }
54
+
55
+ #if NET_2_0
56
+ public static DetachedCriteria For < T > ( )
57
+ {
58
+ return new DetachedCriteria ( typeof ( T ) ) ;
59
+ }
60
+ #endif
61
+
62
+ public static DetachedCriteria For ( System . Type entityType , String alias )
63
+ {
64
+ return new DetachedCriteria ( entityType , alias ) ;
65
+ }
66
+
67
+
68
+ public DetachedCriteria Add ( ICriterion criterion )
69
+ {
70
+ criteria . Add ( criterion ) ;
71
+ return this ;
72
+ }
73
+
74
+ public DetachedCriteria AddOrder ( Order order )
75
+ {
76
+ criteria . AddOrder ( order ) ;
77
+ return this ;
78
+ }
79
+
80
+ public DetachedCriteria CreateAlias ( String associationPath , String alias )
81
+ {
82
+ criteria . CreateAlias ( associationPath , alias ) ;
83
+ return this ;
84
+ }
85
+
86
+ public DetachedCriteria createCriteria ( String associationPath , String alias )
87
+ {
88
+ return new DetachedCriteria ( impl , criteria . CreateCriteria ( associationPath ) ) ;
89
+ }
90
+
91
+ public DetachedCriteria createCriteria ( String associationPath )
92
+ {
93
+ return new DetachedCriteria ( impl , criteria . CreateCriteria ( associationPath ) ) ;
94
+ }
95
+
96
+ public String Alias
97
+ {
98
+ get { return criteria . Alias ; }
99
+ }
100
+
101
+ public DetachedCriteria SetFetchMode ( String associationPath , FetchMode mode )
102
+ {
103
+ criteria . SetFetchMode ( associationPath , mode ) ;
104
+ return this ;
105
+ }
106
+
107
+ public DetachedCriteria SetProjection ( IProjection projection )
108
+ {
109
+ criteria . SetProjection ( projection ) ;
110
+ return this ;
111
+ }
112
+
113
+ public DetachedCriteria SetResultTransformer ( IResultTransformer resultTransformer )
114
+ {
115
+ criteria . SetResultTransformer ( resultTransformer ) ;
116
+ return this ;
117
+ }
118
+
119
+ public override String ToString ( )
120
+ {
121
+ return "DetachableCriteria(" + criteria . ToString ( ) + ')' ;
122
+ }
123
+ }
124
+ }
0 commit comments