File tree 18 files changed +663
-0
lines changed
18 files changed +663
-0
lines changed Load Diff Large diffs are not rendered by default.
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Web ;
3
+ using NHibernate . Context ;
4
+
5
+ namespace NHibernate . Example . Web
6
+ {
7
+ public class CurrentSessionModule : IHttpModule
8
+ {
9
+ public void Init ( HttpApplication context )
10
+ {
11
+ context . BeginRequest += new EventHandler ( Application_BeginRequest ) ;
12
+ context . EndRequest += new EventHandler ( Application_EndRequest ) ;
13
+ }
14
+
15
+ public void Dispose ( )
16
+ {
17
+ }
18
+
19
+ private void Application_BeginRequest ( object sender , EventArgs e )
20
+ {
21
+ ManagedWebSessionContext . Bind ( HttpContext . Current , ExampleApplication . SessionFactory . OpenSession ( ) ) ;
22
+ }
23
+
24
+ private void Application_EndRequest ( object sender , EventArgs e )
25
+ {
26
+ ISession session = ManagedWebSessionContext . Unbind ( HttpContext . Current , ExampleApplication . SessionFactory ) ;
27
+
28
+ if ( session . Transaction . IsActive )
29
+ {
30
+ session . Transaction . Rollback ( ) ;
31
+ }
32
+
33
+ if ( session != null )
34
+ {
35
+ session . Close ( ) ;
36
+ }
37
+ }
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+
3
+ namespace NHibernate . Example . Web . Domain
4
+ {
5
+ public class Item
6
+ {
7
+ private int id ;
8
+ private decimal price ;
9
+ private string description ;
10
+
11
+ public virtual int Id
12
+ {
13
+ get { return id ; }
14
+ set { id = value ; }
15
+ }
16
+
17
+ public virtual decimal Price
18
+ {
19
+ get { return price ; }
20
+ set { price = value ; }
21
+ }
22
+
23
+ public virtual string Description
24
+ {
25
+ get { return description ; }
26
+ set { description = value ; }
27
+ }
28
+ }
29
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . IO ;
3
+ using System . Web ;
4
+ using System . Web . Hosting ;
5
+ using NHibernate ;
6
+ using NHibernate . Example . Web . Domain ;
7
+
8
+ namespace NHibernate . Example . Web
9
+ {
10
+ public class ExampleApplication : HttpApplication
11
+ {
12
+ public static readonly Cfg . Configuration Configuration ;
13
+ public static readonly ISessionFactory SessionFactory ;
14
+
15
+ static ExampleApplication ( )
16
+ {
17
+ log4net . Config . XmlConfigurator . Configure ( ) ;
18
+ Configuration = new NHibernate . Cfg . Configuration ( )
19
+ . SetDefaultAssembly ( typeof ( Item ) . Assembly . FullName )
20
+ . SetDefaultNamespace ( typeof ( Item ) . Namespace )
21
+ . AddDirectory ( new DirectoryInfo ( HostingEnvironment . MapPath ( "~/App_Data/" ) ) ) ;
22
+
23
+ SessionFactory = Configuration . BuildSessionFactory ( ) ;
24
+ }
25
+
26
+ public static ISession GetCurrentSession ( )
27
+ {
28
+ return SessionFactory . GetCurrentSession ( ) ;
29
+ }
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using NHibernate . Example . Web . Domain ;
4
+
5
+ namespace NHibernate . Example . Web . Persistence
6
+ {
7
+ public class ItemList
8
+ {
9
+ public IList < Item > GetAllItems ( )
10
+ {
11
+ return ExampleApplication . GetCurrentSession ( ) . CreateQuery ( "from Item" ) . List < Item > ( ) ;
12
+ }
13
+
14
+ public void UpdateItem ( Item item )
15
+ {
16
+ ExampleApplication . GetCurrentSession ( ) . SaveOrUpdateCopy ( item ) ;
17
+ }
18
+
19
+ public void DeleteItem ( Item item )
20
+ {
21
+ ISession session = ExampleApplication . GetCurrentSession ( ) ;
22
+ session . Delete ( session . Load ( typeof ( Item ) , item . Id ) ) ;
23
+ }
24
+
25
+ public void InsertItem ( Item item )
26
+ {
27
+ ExampleApplication . GetCurrentSession ( ) . Save ( item ) ;
28
+ }
29
+ }
30
+ }
Original file line number Diff line number Diff line change
1
+ <?xml version =" 1.0" encoding =" utf-8" ?>
2
+ <hibernate-mapping xmlns =" urn:nhibernate-mapping-2.2" >
3
+ <!--
4
+ Class has to be non-lazy because Medium Trust Level restrictions
5
+ prevent proxy generation from working.
6
+ -->
7
+ <class name =" Item" lazy =" false" >
8
+ <id name =" Id" >
9
+ <generator class =" native" />
10
+ </id >
11
+ <property name =" Price" />
12
+ <property name =" Description" />
13
+ </class >
14
+ </hibernate-mapping >
Original file line number Diff line number Diff line change
1
+ <% @ Page Language= " C#" EnableViewState= " false" EnableViewStateMac= " false" AutoEventWireup= " true" CodeFile= " Default.aspx.cs" Inherits= " _Default" %>
2
+
3
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
+ <html xmlns =" http://www.w3.org/1999/xhtml" >
5
+ <head runat =" server" >
6
+ <title >NHibernate Demo</title >
7
+ </head >
8
+ <body >
9
+ <h1 >NHibernate Demo</h1 >
10
+ <asp:HyperLink ID =" Schema" runat =" server" NavigateUrl =" ~/Schema.aspx" >Schema Operations</asp:HyperLink >
11
+ <asp:HyperLink ID =" ViewData" runat =" server" NavigateUrl =" ~/ViewData.aspx" >View Data</asp:HyperLink >
12
+ </body >
13
+ </html >
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Data ;
3
+ using System . Configuration ;
4
+ using System . Web ;
5
+ using System . Web . Security ;
6
+ using System . Web . UI ;
7
+ using System . Web . UI . WebControls ;
8
+ using System . Web . UI . WebControls . WebParts ;
9
+ using System . Web . UI . HtmlControls ;
10
+
11
+ public partial class _Default : System . Web . UI . Page
12
+ {
13
+ protected void Page_Load ( object sender , EventArgs e )
14
+ {
15
+
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ <% @ Import namespace= " System.Web.Hosting" %>
2
+ <% @ Import namespace= " System.Xml" %>
3
+ <% @ Application Language= " C#" Inherits= " NHibernate.Example.Web.ExampleApplication" %>
4
+
5
+ <script runat =" server" >
6
+
7
+ void Application_Start (object sender, EventArgs e)
8
+ {
9
+ // Code that runs on application startup
10
+
11
+ }
12
+
13
+ void Application_End (object sender, EventArgs e)
14
+ {
15
+ // Code that runs on application shutdown
16
+
17
+ }
18
+
19
+ void Application_Error (object sender, EventArgs e)
20
+ {
21
+ // Code that runs when an unhandled error occurs
22
+
23
+ }
24
+
25
+ void Session_Start (object sender, EventArgs e)
26
+ {
27
+ // Code that runs when a new session is started
28
+
29
+ }
30
+
31
+ void Session_End (object sender, EventArgs e)
32
+ {
33
+ // Code that runs when a session ends.
34
+ // Note: The Session_End event is raised only when the sessionstate mode
35
+ // is set to InProc in the Web.config file. If session mode is set to StateServer
36
+ // or SQLServer, the event is not raised.
37
+
38
+ }
39
+
40
+ </script >
Original file line number Diff line number Diff line change
1
+ <% @ Page Language= " C#" AutoEventWireup= " true" CodeFile= " InsertItem.aspx.cs" Inherits= " InsertItem" %>
2
+
3
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
+
5
+ <html xmlns =" http://www.w3.org/1999/xhtml" >
6
+ <head runat =" server" >
7
+ <title >Insert New Item</title >
8
+ </head >
9
+ <body >
10
+ <form id =" form1" runat =" server" >
11
+ <div >
12
+ <asp:ObjectDataSource ID =" ItemList" runat =" server"
13
+ SelectMethod =" GetAllItems" TypeName =" NHibernate.Example.Web.Persistence.ItemList" DataObjectTypeName =" NHibernate.Example.Web.Domain.Item" DeleteMethod =" DeleteItem" InsertMethod =" InsertItem" UpdateMethod =" UpdateItem" >
14
+ </asp:ObjectDataSource >
15
+ <asp:FormView ID =" InsertForm" runat =" server" DefaultMode =" Insert" DataKeyNames =" Id" DataSourceID =" ItemList" >
16
+ <InsertItemTemplate >
17
+ <table >
18
+ <tr >
19
+ <td >Price:</td >
20
+ <td ><asp:TextBox ID =" Price" runat =" server" Text =' <%# Bind("Price") %>' /></td >
21
+ </tr >
22
+ <tr >
23
+ <td >Description:</td >
24
+ <td ><asp:TextBox ID =" Description" runat =" server" Text =' <%# Bind("Description") %>' /></td >
25
+ </tr >
26
+ <tr >
27
+ <td colspan =" 2" align =" right" >
28
+ <asp:Button ID =" OKButton" runat =" server" Text =" OK" Width =" 6em" OnClick =" OKButton_Click" />
29
+ <asp:Button ID =" CancelButton" runat =" server" Text =" Cancel" Width =" 6em" CausesValidation =" False" OnClick =" CancelButton_Click" /></td >
30
+ </tr >
31
+ </table >
32
+ </InsertItemTemplate >
33
+ </asp:FormView >
34
+ </div >
35
+ </form >
36
+ </body >
37
+ </html >
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using System . Data ;
3
+ using System . Configuration ;
4
+ using System . Collections ;
5
+ using System . Web ;
6
+ using System . Web . Security ;
7
+ using System . Web . UI ;
8
+ using System . Web . UI . WebControls ;
9
+ using System . Web . UI . WebControls . WebParts ;
10
+ using System . Web . UI . HtmlControls ;
11
+ using NHibernate . Example . Web ;
12
+
13
+ public partial class InsertItem : System . Web . UI . Page
14
+ {
15
+ protected void Page_Load ( object sender , EventArgs e )
16
+ {
17
+
18
+ }
19
+
20
+ protected void OKButton_Click ( object sender , EventArgs e )
21
+ {
22
+ ExampleApplication . GetCurrentSession ( ) . BeginTransaction ( ) ;
23
+ InsertForm . InsertItem ( true ) ;
24
+ ExampleApplication . GetCurrentSession ( ) . Transaction . Commit ( ) ;
25
+ Response . Redirect ( "~/ViewData.aspx" ) ;
26
+ }
27
+ protected void CancelButton_Click ( object sender , EventArgs e )
28
+ {
29
+ Response . Redirect ( "~/ViewData.aspx" ) ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ This is a minimal Web application using NHibernate under Medium Trust Level.
Original file line number Diff line number Diff line change
1
+ <% @ Page Language= " C#" AutoEventWireup= " true" CodeFile= " Schema.aspx.cs" Inherits= " Schema" %>
2
+
3
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
+
5
+ <html xmlns =" http://www.w3.org/1999/xhtml" >
6
+ <head runat =" server" >
7
+ <title >Schema Operations</title >
8
+ </head >
9
+ <body >
10
+ <asp:HyperLink runat =" server" NavigateUrl =" ~/Default.aspx" >Back to the main page</asp:HyperLink >
11
+ <form id =" SchemaOperationsForm" runat =" server" >
12
+ <div >
13
+ <asp:Button ID =" CreateSchema" runat =" server" Text =" Create Schema" OnClick =" CreateSchema_Click" />
14
+ <asp:Button ID =" DropSchema" runat =" server" Text =" Drop Schema" OnClick =" DropSchema_Click" />
15
+ </div >
16
+ </form >
17
+ <asp:Label ID =" Status" runat =" server" Text =" " />
18
+ </body >
19
+ </html >
Original file line number Diff line number Diff line change
1
+ using System ;
2
+ using NHibernate ;
3
+ using NHibernate . Example . Web ;
4
+ using NHibernate . Example . Web . Domain ;
5
+ using NHibernate . Tool . hbm2ddl ;
6
+
7
+ public partial class Schema : System . Web . UI . Page
8
+ {
9
+ protected void Page_Load ( object sender , EventArgs e )
10
+ {
11
+ }
12
+
13
+ protected void CreateSchema_Click ( object sender , EventArgs e )
14
+ {
15
+ SchemaExport export = new SchemaExport ( ExampleApplication . Configuration ) ;
16
+ export . Create ( false , true ) ;
17
+
18
+ ISession session = ExampleApplication . SessionFactory . GetCurrentSession ( ) ;
19
+ session . BeginTransaction ( ) ;
20
+ Item item1 = new Item ( ) ;
21
+ item1 . Description = "First item" ;
22
+ item1 . Price = 100m ;
23
+ session . Save ( item1 ) ;
24
+
25
+ Item item2 = new Item ( ) ;
26
+ item2 . Description = "Second item" ;
27
+ item2 . Price = 150m ;
28
+ session . Save ( item2 ) ;
29
+
30
+ session . Transaction . Commit ( ) ;
31
+
32
+ Status . Text = "Schema created" ;
33
+ }
34
+
35
+ protected void DropSchema_Click ( object sender , EventArgs e )
36
+ {
37
+ SchemaExport export = new SchemaExport ( ExampleApplication . Configuration ) ;
38
+ export . Drop ( false , true ) ;
39
+ Status . Text = "Schema dropped" ;
40
+ }
41
+ }
Original file line number Diff line number Diff line change
1
+ <% @ Page Language= " C#" AutoEventWireup= " true" CodeFile= " ViewData.aspx.cs" Inherits= " ViewData" %>
2
+
3
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
4
+
5
+ <html xmlns =" http://www.w3.org/1999/xhtml" >
6
+ <head runat =" server" >
7
+ <title >View Items</title >
8
+ </head >
9
+ <body >
10
+ <form id =" ListForm" runat =" server" >
11
+ <div >
12
+ <asp:GridView ID =" GridView1" runat =" server" AutoGenerateColumns =" False" AutoGenerateDeleteButton =" True" AutoGenerateEditButton =" True" AutoGenerateSelectButton =" false" Caption =" Item List" DataSourceID =" ItemList" DataKeyNames =" Id" >
13
+ <Columns >
14
+ <asp:BoundField DataField =" Id" HeaderText =" Id" SortExpression =" Id" />
15
+ <asp:BoundField DataField =" Price" HeaderText =" Price" SortExpression =" Price" />
16
+ <asp:BoundField DataField =" Description" HeaderText =" Description" SortExpression =" Description" />
17
+ </Columns >
18
+ </asp:GridView >
19
+ <asp:HyperLink ID =" InsertItem" runat =" server" NavigateUrl =" ~/InsertItem.aspx" >Add New Item</asp:HyperLink >
20
+ <asp:ObjectDataSource ID =" ItemList" runat =" server"
21
+ SelectMethod =" GetAllItems" TypeName =" NHibernate.Example.Web.Persistence.ItemList" DataObjectTypeName =" NHibernate.Example.Web.Domain.Item" DeleteMethod =" DeleteItem" InsertMethod =" InsertItem" UpdateMethod =" UpdateItem" >
22
+ <DeleteParameters >
23
+ <asp:Parameter Name =" id" Type =" Int32" />
24
+ </DeleteParameters >
25
+ </asp:ObjectDataSource >
26
+ </div >
27
+ </form >
28
+ </body >
29
+ </html >
You can’t perform that action at this time.
0 commit comments