1
+ ---
2
+ layout:
3
+ ---
4
+ < html > < head >
5
+ < meta charset ="utf-8 ">
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 " />
7
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
8
+ < meta http-equiv ="content-type " content ="text/html;charset=utf-8 " />
9
+ < link rel ="canonical " href ="http://nhibernate.info/doc/nhibernate-reference/architecture.html " />
10
+ < title > Chapter 2. Architecture</ title > < link rel ="stylesheet " href ="../shared/css/html.css " type ="text/css "> < meta name ="generator " content ="DocBook XSL Stylesheets V1.65.1 "> < link rel ="home " href ="index.html " title ="NHibernate - Relational Persistence for Idiomatic .NET "> < link rel ="up " href ="index.html " title ="NHibernate - Relational Persistence for Idiomatic .NET "> < link rel ="previous " href ="quickstart.html " title ="Chapter 1. Quickstart with IIS and Microsoft SQL Server "> < link rel ="next " href ="session-configuration.html " title ="Chapter 3. ISessionFactory Configuration "> </ head > < body bgcolor ="white " text ="black " link ="#0000FF " vlink ="#840084 " alink ="#0000FF "> < div class ="navheader "> < table width ="100% " summary ="Navigation header "> < tr > < th colspan ="3 " align ="center "> Chapter 2. Architecture</ th > </ tr > < tr > < td width ="20% " align ="left "> < a accesskey ="p " href ="quickstart.html "> Prev</ a > </ td > < th width ="60% " align ="center "> </ th > < td width ="20% " align ="right "> < a accesskey ="n " href ="session-configuration.html "> Next</ a > </ td > </ tr > </ table > < hr > </ div > < div class ="chapter " lang ="en "> < div class ="titlepage "> < div > < div > < h2 class ="title "> < a name ="architecture "> </ a > Chapter 2. Architecture</ h2 > </ div > </ div > < div > </ div > </ div > {% include google_adsense.html %}< div class ="sect1 " lang ="en "> < div class ="titlepage "> < div > < div > < h2 class ="title " style ="clear: both "> < a name ="architecture-overview "> </ a > 2.1. Overview</ h2 > </ div > </ div > < div > </ div > </ div > < p >
11
+ A (very) high-level view of the NHibernate architecture:
12
+ </ p > < div class ="mediaobject " align ="center "> < img src ="../shared/images/overview.png " align ="middle "> </ div > < p >
13
+ This diagram shows NHibernate using the database and configuration data to
14
+ provide persistence services (and persistent objects) to the application.
15
+ </ p > < p >
16
+ We would like to show a more detailed view of the runtime architecture.
17
+ Unfortunately, NHibernate is flexible and supports several approaches. We will
18
+ show the two extremes. The "lite" architecture has the application
19
+ provide its own ADO.NET connections and manage its own transactions. This approach
20
+ uses a minimal subset of NHibernate's APIs:
21
+ </ p > < div class ="mediaobject " align ="center "> < img src ="../shared/images/lite.png " align ="middle "> </ div > < p >
22
+ The "full cream" architecture abstracts the application away from the
23
+ underlying ADO.NET APIs and lets NHibernate take care of the details.
24
+ </ p > < div class ="mediaobject " align ="center "> < img src ="../shared/images/fullcream.png " align ="middle "> </ div > < p >
25
+ Heres some definitions of the objects in the diagrams:
26
+
27
+ </ p > < div class ="variablelist "> < dl > < dt > < span class ="term "> ISessionFactory (< tt class ="literal "> NHibernate.ISessionFactory</ tt > )</ span > </ dt > < dd > < p >
28
+ A threadsafe (immutable) cache of compiled mappings for a single database.
29
+ A factory for < tt class ="literal "> ISession</ tt > and a client of
30
+ < tt class ="literal "> IConnectionProvider</ tt > . Might hold an optional (second-level)
31
+ cache of data that is reusable between transactions, at a
32
+ process- or cluster-level.
33
+ </ p > </ dd > < dt > < span class ="term "> ISession (< tt class ="literal "> NHibernate.ISession</ tt > )</ span > </ dt > < dd > < p >
34
+ A single-threaded, short-lived object representing a conversation between
35
+ the application and the persistent store. Wraps an ADO.NET connection. Factory
36
+ for < tt class ="literal "> ITransaction</ tt > . Holds a mandatory (first-level) cache
37
+ of persistent objects, used when navigating the object graph or looking up
38
+ objects by identifier.
39
+ </ p > </ dd > < dt > < span class ="term "> Persistent Objects and Collections</ span > </ dt > < dd > < p >
40
+ Short-lived, single threaded objects containing persistent state and business
41
+ function. These might be ordinary POCOs, the only special thing about
42
+ them is that they are currently associated with (exactly one)
43
+ < tt class ="literal "> ISession</ tt > . As soon as the < tt class ="literal "> Session</ tt > is closed,
44
+ they will be detached and free to use in any application layer (e.g. directly
45
+ as data transfer objects to and from presentation).
46
+ </ p > </ dd > < dt > < span class ="term "> Transient Objects and Collections</ span > </ dt > < dd > < p >
47
+ Instances of persistent classes that are not currently associated with a
48
+ < tt class ="literal "> ISession</ tt > . They may have been instantiated by
49
+ the application and not (yet) persisted or they may have been instantiated by a
50
+ closed < tt class ="literal "> ISession</ tt > .
51
+ </ p > </ dd > < dt > < span class ="term "> ITransaction (< tt class ="literal "> NHibernate.ITransaction</ tt > )</ span > </ dt > < dd > < p >
52
+ (Optional) A single-threaded, short-lived object used by the application to
53
+ specify atomic units of work. Abstracts application from underlying ADO.NET
54
+ transaction. An < tt class ="literal "> ISession</ tt > might span several
55
+ < tt class ="literal "> ITransaction</ tt > s in some cases.
56
+ </ p > </ dd > < dt > < span class ="term "> IConnectionProvider (< tt class ="literal "> NHibernate.Connection.IConnectionProvider</ tt > )</ span > </ dt > < dd > < p >
57
+ (Optional) A factory for ADO.NET connections and commands. Abstracts application
58
+ from the concrete vendor-specific implementations of < tt class ="literal "> IDbConnection</ tt >
59
+ and < tt class ="literal "> IDbCommand</ tt > . Not exposed to application, but can be
60
+ extended/implemented by the developer.
61
+ </ p > </ dd > < dt > < span class ="term "> IDriver (< tt class ="literal "> NHibernate.Driver.IDriver</ tt > )</ span > </ dt > < dd > < p >
62
+ (Optional) An interface encapsulating differences between ADO.NET providers, such
63
+ as parameter naming conventions and supported ADO.NET features.
64
+ </ p > </ dd > < dt > < span class ="term "> ITransactionFactory (< tt class ="literal "> NHibernate.Transaction.ITransactionFactory</ tt > )</ span > </ dt > < dd > < p >
65
+ (Optional) A factory for < tt class ="literal "> ITransaction</ tt > instances. Not exposed to the
66
+ application, but can be extended/implemented by the developer.
67
+ </ p > </ dd > </ dl > </ div > < p >
68
+ </ p > < p >
69
+ Given a "lite" architecture, the application bypasses the
70
+ < tt class ="literal "> ITransaction</ tt > /< tt class ="literal "> ITransactionFactory</ tt > and/or
71
+ < tt class ="literal "> IConnectionProvider</ tt > APIs to talk to ADO.NET directly.
72
+ </ p > </ div > < div class ="sect1 " lang ="en "> < div class ="titlepage "> < div > < div > < h2 class ="title " style ="clear: both "> < a name ="architecture-states "> </ a > 2.2. Instance states</ h2 > </ div > </ div > < div > </ div > </ div > < p >
73
+ An instance of a persistent classes may be in one of three different states,
74
+ which are defined with respect to a < span class ="emphasis "> < em > persistence context</ em > </ span > .
75
+ The NHibernate < tt class ="literal "> ISession</ tt > object is the persistence context:
76
+ </ p > < div class ="variablelist "> < dl > < dt > < span class ="term "> transient</ span > </ dt > < dd > < p >
77
+ The instance is not, and has never been associated with
78
+ any persistence context. It has no persistent identity
79
+ (primary key value).
80
+ </ p > </ dd > < dt > < span class ="term "> persistent</ span > </ dt > < dd > < p >
81
+ The instance is currently associated with a persistence
82
+ context. It has a persistent identity (primary key value)
83
+ and, perhaps, a corresponding row in the database. For a
84
+ particular persistence context, NHibernate
85
+ < span class ="emphasis "> < em > guarantees</ em > </ span > that persistent identity
86
+ is equivalent to CLR identity (in-memory location of the
87
+ object).
88
+ </ p > </ dd > < dt > < span class ="term "> detached</ span > </ dt > < dd > < p >
89
+ The instance was once associated with a persistence
90
+ context, but that context was closed, or the instance
91
+ was serialized to another process. It has a persistent
92
+ identity and, perhaps, a corrsponding row in the database.
93
+ For detached instances, NHibernate makes no guarantees
94
+ about the relationship between persistent identity and
95
+ CLR identity.
96
+ </ p > </ dd > </ dl > </ div > </ div > < div class ="sect1 " lang ="en "> < div class ="titlepage "> < div > < div > < h2 class ="title " style ="clear: both "> < a name ="architecture-current-session "> </ a > 2.3. Contextual Sessions</ h2 > </ div > </ div > < div > </ div > </ div > < p >
97
+ Most applications using NHibernate need some form of "contextual" sessions, where a given
98
+ session is in effect throughout the scope of a given context. However, across applications
99
+ the definition of what constitutes a context is typically different; and different contexts
100
+ define different scopes to the notion of current.
101
+ </ p > < p >
102
+ Starting with version 1.2, NHibernate added the < tt class ="literal "> ISessionFactory.GetCurrentSession()</ tt >
103
+ method. The processing behind < tt class ="literal "> ISessionFactory.GetCurrentSession()</ tt > is pluggable.
104
+ An extension interface (< tt class ="literal "> NHibernate.Context.ICurrentSessionContext</ tt > ) and a new
105
+ configuration parameter (< tt class ="literal "> hibernate.current_session_context_class</ tt > ) have been added
106
+ to allow pluggability of the scope and context of defining current sessions.
107
+ </ p > < p >
108
+ See the API documentation for the < tt class ="literal "> NHibernate.Context.ICurrentSessionContext</ tt >
109
+ interface for a detailed discussion of its contract. It defines a single method,
110
+ < tt class ="literal "> CurrentSession()</ tt > , by which the implementation is responsible for
111
+ tracking the current contextual session. Out-of-the-box, NHibernate comes with several
112
+ implementations of this interface:
113
+ </ p > < div class ="itemizedlist "> < ul type ="disc "> < li > < p >
114
+ < tt class ="literal "> NHibernate.Context.CallSessionContext</ tt > - current sessions are tracked
115
+ by < tt class ="literal "> CallContext</ tt > . You are responsible to bind and unbind an < tt class ="literal ">
116
+ ISession</ tt > instance with static methods of class < tt class ="literal "> CurrentSessionContext
117
+ </ tt > .
118
+ </ p > </ li > < li > < p >
119
+ < tt class ="literal "> NHibernate.Context.ThreadStaticSessionContext</ tt > - current session is
120
+ stored in a thread-static variable. This context only supports one session factory.
121
+ You are responsible to bind and unbind an < tt class ="literal "> ISession</ tt > instance with
122
+ static methods of class < tt class ="literal "> CurrentSessionContext</ tt > .
123
+ </ p > </ li > < li > < p >
124
+ < tt class ="literal "> NHibernate.Context.WebSessionContext</ tt > -
125
+ stores the current session in < tt class ="literal "> HttpContext</ tt > .
126
+ You are responsible to bind and unbind an < tt class ="literal "> ISession</ tt >
127
+ instance with static methods of class < tt class ="literal "> CurrentSessionContext</ tt > .
128
+ </ p > </ li > < li > < p >
129
+ < tt class ="literal "> NHibernate.Context.WcfOperationSessionContext</ tt > - current sessions are tracked
130
+ by WCF < tt class ="literal "> OperationContext</ tt > . You need to register the < tt class ="literal "> WcfStateExtension</ tt >
131
+ extension in WCF. You are responsible to bind and unbind an < tt class ="literal "> ISession
132
+ </ tt > instance with static methods of class < tt class ="literal "> CurrentSessionContext</ tt > .
133
+ </ p > </ li > < li > < p >
134
+ < tt class ="literal "> NHibernate.Context.ManagedWebSessionContext</ tt > - current
135
+ sessions are tracked by < tt class ="literal "> HttpContext</ tt > . Removed in NHibernate 4.0
136
+ - < tt class ="literal "> NHibernate.Context.WebSessionContext</ tt > should be used instead.
137
+ You are responsible to bind and unbind an < tt class ="literal "> ISession</ tt > instance with static methods
138
+ on this class, it never opens, flushes, or closes an < tt class ="literal "> ISession</ tt > itself.
139
+ </ p > </ li > </ ul > </ div > < p >
140
+ The < tt class ="literal "> hibernate.current_session_context_class</ tt > configuration parameter
141
+ defines which < tt class ="literal "> NHibernate.Context.ICurrentSessionContext</ tt > implementation
142
+ should be used. Typically, the value of this parameter would just name the implementation
143
+ class to use (including the assembly name); for the out-of-the-box implementations, however,
144
+ there are corresponding short names: "call", "thread_static", "web" and "wcf_operation",
145
+ respectively.
146
+ </ p > </ div > </ div > < div class ="navfooter "> < hr > < table width ="100% " summary ="Navigation footer "> < tr > < td width ="40% " align ="left "> < a accesskey ="p " href ="quickstart.html "> Prev</ a > </ td > < td width ="20% " align ="center "> < a accesskey ="u " href ="index.html "> Up</ a > </ td > < td width ="40% " align ="right "> < a accesskey ="n " href ="session-configuration.html "> Next</ a > </ td > </ tr > < tr > < td width ="40% " align ="left " valign ="top "> Chapter 1. Quickstart with IIS and Microsoft SQL Server </ td > < td width ="20% " align ="center "> < a accesskey ="h " href ="index.html "> Home</ a > </ td > < td width ="40% " align ="right " valign ="top "> Chapter 3. ISessionFactory Configuration</ td > </ tr > </ table > </ div > {% include google_analytics.html %}{% include metrika_yandex_ru.html %}</ body > </ html >
0 commit comments