Asp. Net large project practice (4)-use nhib.pdf to save and query the Session management and initialization of nhib.pdf of our business objects (with source code)

Source: Internet
Author: User
Tags sessionstorage

The sessions of NHibernate and Asp. Net are two different things. Do not confuse them. What is the NHibernate Session used? If you have used Linq2Sql, you can understand it as DataContext. All objects to be persisted must be hosted in the Session. Session is also a cache. For example, if you Get a User through nhib.pdf within a certain range, when you Get the same User again, nhib.pdf will not operate on the database, instead, the User obtained for the first time is retrieved from the Session cache.

  1. To fully utilize the delayed loading and caching mechanism of nhib.pdf, we should reasonably control the Session lifecycle. For example, in a WCF application, we should bind the Session to a communication request, in Asp. in netMVC, we 'd better bind the Session lifecycle to an Action. Since our system design will use WCF or other request forms in the future, we make an interface:
       using System;
    using NHibernate;

    namespace Demo.HIS.FrameWork.Repository.NHb
    {
    public interface ISessionStorage
    {
    ISession Get();
    void Set(ISession value);
    }
    }

    Note that adding reference to nhib.pdf. dll is currently using a NHibernate-2.1.0 version

  2. At present, we still use Asp.net MVC to create a class HttpSessionStorage implementation interface ISessionStorage: Code

       Using nhib.pdf;
    Using System. Web;

    Namespace Demo. HIS. FrameWork. Repository. NHb
    {
    Public class HttpSessionStorage: ISessionStorage
    {
    # Region ISessionStorage Member

    Public nhibib. ISession Get ()
    {
    Return (ISession) HttpContext. Current. Items ["NhbSession"];
    }

    Public void Set (ISession value)
    {
    If (value! = Null)
    {
    HttpContext. Current. Items. Add ("NhbSession", value );
    }
    }

    # Endregion
    }
    }

    Here we will bind the life cycle of the NHibernate Session with an Action request (HttpContext. Current. Item). Note that the reference System. Web

  3. Let's create a SessionBuilder class to initialize the NHibernate Session: Code

       Namespace Demo. HIS. FrameWork. Repository. NHb
    {
    Public static class SessionBuilder
    {
    Private static object locker = new object ();
    Private static Configuration configuration = null;
    Private static ISessionFactory sessionFactory = null;
    Public static ISessionStorage sessionStorage {set; get ;}

    Private static void CreateConfiguration ()
    {
    // HibernatingRhinos. nhib.pdf. Profiler. Appender. NHibernateProfiler. Initialize (); // view the SQL statement generated by HQL
    // Configuration = new Configuration (). Configure (System. Web. HttpContext. Current. Request. PhysicalApplicationPath + "Configuration \ hibernate. cfg. xml ");
    Configuration = new Configuration (). Configure ();
    }

    Public static Configuration
    {
    Get
    {
    Lock (locker)
    {
    If (configuration = null)
    {
    CreateConfiguration ();
    }
    Return configuration;
    }
    }
    Set {configuration = value ;}
    }

    Internal static ISessionFactory SessionFactory
    {
    Get
    {
    If (sessionFactory = null)
    {
    If (Configuration = null)
    {
    CreateConfiguration ();
    }
    Lock (locker)
    {
    SessionFactory = Configuration. BuildSessionFactory ();
    }
    }

    Return sessionFactory;
    }
    }

    Public static ISession CreateSession ()
    {
    ISession s = sessionStorage. Get ();
    If (s = null)
    {
    S = SessionFactory. OpenSession ();
    SessionStorage. Set (s );
    }
    Return s;
    }

    }
    }

    Note configuration = new Configuration (). configure (); read the configuration file of nhibm.com. The configuration file must be placed in the root directory of the website (DemoHisSite) and named hibernate. cfg. xml. If you want to place the configuration file somewhere else or take another name, see the line commented out in the Code.

  4. Nhib.pdf configuration file
    Location:

    Content:

    Code

       <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    <section name="hibernate-configuration"
    type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    </configSections>
    <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="connection.connection_string">
    User ID=dawnhis;Password=his;Data Source=dawhisdb
    </property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="use_outer_join">true</property>
    <property name="command_timeout">10</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
    <mapping assembly="Infrastructure.Repositories"/>

    </session-factory>
    </configuration>

    For more information about the configuration section, see it here.

  5. Specify an ISessionStorage instance in Global. asax:

    Code

       Namespace Demo. HIS. MVC
    {
    Public class DemoHISApplication: HttpApplication
    {
    Protected virtual void OnStart ()
    {
    InitSessionBuilder ();
    }

    Private void initSessionBuilder ()
    {
    SessionBuilder. sessionStorage = new HttpSessionStorage (); // create a Session instance here
    }

    Public static void RegisterRoutes (RouteCollection routes)
    {
    Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");

    Routes. MapRoute (
    "Default", // Route name
    "{Controller}/{action}/{id}", // URL with parameters
    New {controller = "Home", action = "Index", id = ""} // Parameter defaults
    );

    }
    Protected void Application_Start ()
    {
    OnStart ();
    }
    }
    }

    Note that I changed the reference of Global. asax here:

       <%@ Application Inherits="Demo.HIS.MVC.DemoHISApplication" Language="C#" %>

Source code download: HISDemo-2.rar

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.