zoukankan      html  css  js  c++  java
  • ASP.NET MVC NHibernate 整合

     请注明转载地址:http://www.cnblogs.com/arhat

    在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做。同理这个三个技术也是。那么在整合之前,需要对
    ASP.NET,Spring.NET,NHibernate有所了解,最好是有一些经验,因为平白无故的就把3个技术融合到一起,会闹些小别扭,其实就像配置化学药品一样,如果3中原料有一点出路,那么整个实验也就失败了。

    在网上有很多高人都写过类似的文章,可能是鄙人不才,看了很多文章都没有把我的问题解决了,同时我在整合的过程中走了不少的弯路。今天做一下记录,防止自己忘掉。好了,言归正传。

    整合原理:

    我们在初中,高中都学过化学,知道有些物质是不能很好的反应,需要通过催化剂来促使两者之间更改好的反应。那么我们就把ASP.NET MVC看成是物质A,NHibernate看成是物质C。本身A+C=应用。但是由于A和C之间结合会带来开发上的一些难题(比如事务的控制),那么怎么办呢,我们可以加入催化剂Spring.NET(物质B)。这样通过B这个催化剂很好的解决了A+C之间的问题。于是公式就出来了A+B+C=应用。

    当然,我们这里是程序设计而不是化学反应,所以在结合的过程中我们遵循下面的步骤。

    1. ASP.NET + Spring.NET

    2. ASP.NET + NHibernate

    3. ASP.NET +Spring.NET +NHibernate

    wps_clip_image-26291

    一、ASP.Net MVC与NHibernate整合
    首先我们建立一个空的解决方案“MVCNHibernate”。在解决方案中加入三个个项目,一个Web项目,两个类库项目。如图所示:

    这里我们使用的是NHibernate-3.2.0。

    1. 在DAL项目中,我们加入:
       Iesi.Collections.dll,NHibernate.dll的引用。

    2. 配置Web.config

    <configSections>
    
            <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
    
        </configSections>
    
        <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    
            <session-factory>
    
                <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
    
                <property name="show_sql">true</property>            
    
                <property name="current_session_context_class">web</property>
    
                <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    
                <property name="connection.connection_string">
    
                    persistsecurityinfo=True;server=127.0.0.1;user id=root;password=symjie;database=nhibernate
    
                </property>
    
     <mapping assembly="" />
    
            </session-factory>
    
        </hibernate-configuration>


        其中青色部分先设置为空,这个是用来设置*.hbm.xml文件,也就是映射文件。

    3. 创建User.cs和User.hbm.xml文件
    在Com.Symjie.Model项目中,添加一个User.cs,并添加一个Config文件夹。

    User.cs文件如下:

    using System;
    
    using System.Collections;
    
    using System.ComponentModel;
    
    using System.Linq;
    
    using System.Text;
    
    using System.Collections.Generic;
    
    namespace Com.Symjie.Model
    
    {    
    
    public partial class User {
    
    partial void OnCreated();
    
    public User()
    
            {
    
                OnCreated();
    
            }
    
    public virtual int Id
    
            {
    
    get;
    
    set;
    
            }
    
    public virtual string Name
    
            {
    
    get;
    
    set;
    
            }
    
    public virtual string Pwd
    
            {
    
    get;
    
    set;
    
            }
    
        }
    
    }

    在Config文件夹中创建User.hbm.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    
    <hibernate-mapping assembly="Com.Symjie.Model" namespace="Com.Symjie.Model" xmlns="urn:nhibernate-mapping-2.2">
    
      <class name="User" table="users" schema="nhibernate">
    
        <id name="Id" type="Int32">
    
          <column name="id" not-null="true" precision="11" scale="0" sql-type="int" />
    
          <generator class="identity" />
    
        </id>
    
        <property name="Name" type="String">
    
          <column name="name" not-null="true" length="64" sql-type="varchar" />
    
        </property>
    
        <property name="Pwd" type="String">
    
          <column name="pwd" not-null="true" length="128" sql-type="varchar" />
    
        </property>
    
      </class>
    
    </hibernate-mapping>

    并把User.hbm.xml文件设置为潜入的资源。

    wps_clip_image-15812

    同时更改Web.config文件:

    把<mapping assembly="" />改为<mapping assembly="Com.Symjie.Model" />

    4. 在Com.Symjie.DAL项目中创建D_User.cs和NHibernateHelper.cs
        NHibernateHelper.cs内容如下:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using NHibernate;
    
    using NHibernate.Cfg;
    
    namespace Com.Symjie.DAL
    
    {
    
    public sealed class NHibernateHelper
    
        {
    
    static ISessionFactory instance = null;
    
    static readonly object padlock = new object();
    
            NHibernateHelper()
    
            {
    
            }
    
    private static ISessionFactory Instance
    
            {
    
    get
    
                {
    
    lock (padlock)
    
                    {
    
    if (instance == null)
    
                        {
    
                            instance = new Configuration().Configure().BuildSessionFactory();
    
                        }
    
    return instance;
    
                    }
    
                }
    
            }
    
    public static ISession OpenSession() 
    
            {
    
    return Instance.OpenSession();
    
            }
    
    /*报错,暂时不用,而到最后使用Spring.NET来管理。
    
    public static ISession GetCurrentSession()
    
            {
    
    return Instance.GetCurrentSession();
    
            }*/
    
    public static  T Get<T>(object id)
    
            {
    
                T t = default(T);
    
    ISession session = NHibernateHelper.OpenSession();
    
    ITransaction tran = session.BeginTransaction();
    
    try
    
                {
    
                    t = session.Get<T>(id);
    
                    tran.Commit();
    
    return t;
    
                }
    
    catch (Exception ex)
    
                {
    
                    tran.Rollback();
    
    throw new Exception(ex.Message);
    
                }
    
    finally
    
                {
    
    if (session != null && session.IsOpen)
    
                    {
    
                        session.Close();
    
                    }
    
                }
    
            }
    
    public static T UniqueQuery<T>(string hql, params string[] param)
    
            {
    
                T t = default(T);
    
    ISession session = NHibernateHelper.OpenSession();
    
    ITransaction tran = session.BeginTransaction();
    
    try
    
                {
    
    IQuery query = session.CreateQuery(hql);
    
    if (param != null)
    
                    {
    
    for (int i = 0; i < param.Length; i++)
    
                        {
    
                            query.SetString(i, param[i]);
    
                        }
    
                    }
    
                    t = query.UniqueResult<T>();
    
    return t;
    
                }
    
    catch (Exception ex)
    
                {
    
                    tran.Rollback();
    
    throw new Exception(ex.Message);
    
                }
    
    finally
    
                {
    
    if (session != null && session.IsOpen)
    
                    {
    
                        session.Close();
    
                    }
    
                }
    
            }
    
    public static List<T> ExecuteQuery<T>(string hql, params string[] param)
    
            {
    
    ISession session = NHibernateHelper.OpenSession();
    
    ITransaction tran = session.BeginTransaction();
    
    List<T> list = null;
    
    try
    
                {
    
    IQuery query = session.CreateQuery(hql);
    
    if (param != null)
    
                    {
    
    for (int i = 0; i < param.Length; i++)
    
                        {
    
                            query.SetParameter(i, param[i]);
    
                        }
    
                    }
    
                    list = query.List<T>() as List<T>;
    
                    tran.Commit();
    
    return list;
    
                }
    
    catch (Exception ex)
    
                {
    
                    tran.Rollback();
    
    throw new Exception(ex.Message);
    
                }
    
    finally
    
                {
    
    if (session != null && session.IsOpen) 
    
                    {
    
                        session.Close();
    
                    }
    
                }
    
            }
    
        }
    
    }

    D_User.cs代码如下:

    using System;
    
    using System.Collections.Generic;
    
    using System.Linq;
    
    using System.Text;
    
    using NHibernate;
    
    namespace Com.Symjie.DAL
    
    {
    
    public class D_User
    
        {
    
    public List<Model.User> GetAllUsers() 
    
            {
    
    return NHibernateHelper.ExecuteQuery<Model.User>("from User", null);
    
            }
    
        }
    
    }

    5. 在Web项目中,添加一个HomeConroller和视图,内容如下
    HomeController.cs

    public class HomeController : Controller
    
    {
    
    private DAL.D_User dal = new DAL.D_User();
    
    public ActionResult Index()
    
            {
    
    List<Model.User> list = dal.GetAllUsers();
    
                ViewBag.Data = list;
    
    return View();
    
            }
    
    }

        Index.cshtml

    @using Com.Symjie.Model;
    
    @{
    
        Layout = null;
    
    }
    
    <!DOCTYPE html>
    
    <html>
    
    <head>
    
    <title>Index</title>
    
    </head>
    
    <body>
    
    <div>
    
    @foreach (User user in ViewBag.Data) 
    
            {
    
    @user.Name;
    
            }
    
    </div>
    
    </body>
    
    </html>

    执行,我们就会看到效果。

    在本节中,我们写了一个NHibernateHelper的帮助类,这个类可以说是模仿JAVA写的,但是对于GetCurrentSession这个方法一直有问题,不知道到底怎么回事,报了“No Hibernate Session bound to thread, and configuration does not allow”这个错误。很是郁闷,但是没关系,到下一节,我们使用Spring.NET来管理NHibernate就可以解决这个问题。

    好了,这一章节到此结束,本章把ASP.NET MVC 和 NHibernate只是一个简单的组合。下一章节,我们将讲解ASP.NET MVC 和 Spring.NET,NHibernate的结合。

    源码下载

  • 相关阅读:
    27 Spring Cloud Feign整合Hystrix实现容错处理
    26 Spring Cloud使用Hystrix实现容错处理
    25 Spring Cloud Hystrix缓存与合并请求
    24 Spring Cloud Hystrix资源隔离策略(线程、信号量)
    23 Spring Cloud Hystrix(熔断器)介绍及使用
    22 Spring Cloud Feign的自定义配置及使用
    21 Spring Cloud使用Feign调用服务接口
    20 Spring Cloud Ribbon配置详解
    19 Spring Cloud Ribbon自定义负载均衡策略
    18 Spring Cloud Ribbon负载均衡策略介绍
  • 原文地址:https://www.cnblogs.com/arhat/p/3541032.html
Copyright © 2011-2022 走看看