整合ASP.NET MVC5,Spring.Net,NHibernate

本文介绍了一个基于ASP.NET MVC的应用程序如何整合NHibernate进行数据持久化操作,包括配置步骤、各层间的依赖关系及具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

        所有的程序集引用,都可以在Nuget中获取,需要注意除了Spring和NHibenate的程序集外,还要引用Spring.Net NHibernate Support。

        为了结构清晰,我把各层的配置都放在了Config文件夹下,NH的映射文件存放于Model层中的Mappings文件夹下,如图,额,由于课程题目,这是一个简单的二手车交易系统 。



首先是Car.Web种web.config的配置,除了配置还有MVC5自动给添加的EF的东西,此外添加了log4net,配置很简单,具体的使用可以自定义全局错误过滤器,写日志即可,这里就不贴代码了。

<?xml version="1.0" encoding="utf-8"?>
<!--
  有关如何配置 ASP.NET 应用程序的详细信息,请访问
  http://go.microsoft.com/fwlink/?LinkId=301880
  -->
<configuration>


  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!--Log4Net配置-->
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    <!--Spring-->
    <sectionGroup name="spring">
      <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc4" /><!--项目是MVC5,但是由于依赖关系,这里Spring的MVC仍然使用了MVC4-->
      <section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
    </sectionGroup>
    <!--数据库连接信息-->
    <section name="databaseSettings" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <!--Spring-->
  <spring>
    <context>
      <resource uri="assembly://Car.Dao/Car.Dao.Config/SpringNHibernate.xml" />
      <resource uri="assembly://Car.Dao/Car.Dao.Config/Dao.xml" />
      <resource uri="assembly://Car.Service/Car.Service.Config/Service.xml" />
      <resource uri="file://~/Config/Controllers.xml" />
    </context>
  </spring>

  <!--数据库连接信息-->
  <databaseSettings>
    <add key="db.server" value="127.0.0.1" />
    <add key="db.database" value="ASP.NET" />
    <add key="db.user" value="sa" />
    <add key="db.password" value="***" />
  </databaseSettings>

  <!--Log4Net配置-->
  <log4net>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <!--日志路径-->
      <param name="File" value="E:\CarApp_Log\" />
      <!--是否是向文件中追加日志-->
      <param name="AppendToFile" value="true" />
      <!--log保留天数-->
      <param name="MaxSizeRollBackups" value="10" />
      <!--日志文件名是否是固定不变的-->
      <param name="StaticLogFileName" value="false" />
      <!--日志文件名格式为:2008-08-31.log-->
      <param name="DatePattern" value="yyyy-MM-dd".log"" />
      <!--日志根据日期滚动-->
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n %loggername" />
        <!--换行-->
        <param name="Header" value="[Header]

" />
        <param name="Footer" value="[Footer]

" />
      </layout>
    </appender>
    <root>
      <!--(高) OFF > FATAL > ERROR > WARN > INFO > DEBUG > ALL (低) -->
      <level value="all" />
      <appender-ref ref="ColoredConsoleAppender" />
      <appender-ref ref="RollingLogFileAppender" />
    </root>
  </log4net>

  <!--默认配置-->
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Car.Web-20161104045913.mdf;Initial Catalog=aspnet-Car.Web-20161104045913;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    <add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="MyNHibernateSessionFactory" />
  </appSettings>
  <system.web>
    <!--开启自动跳转错误页-->
    <customErrors mode="On"></customErrors>
    <authentication mode="None" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="FormsAuthentication" />
      <add name="OpenSessionInView" type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.4000" newVersion="4.0.0.4000" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>


然后是Dao层的SpringNHibernate.xml配置

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:db="http://www.springframework.net/database"
         xmlns:tx="http://www.springframework.net/tx"
         >

  <!-- 引用web.config中的配置 -->
  <object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
    <property name="ConfigSections" value="databaseSettings"/>
  </object>

  <!-- 数据库的配置 -->
  <db:provider id="DbProvider"
                   provider="System.Data.SqlClient"
                   connectionString="server=${db.server};Database=${db.database};uid=${db.user};pwd=${db.password}"
               />

  <!-- NHibernate 配置,主要是配置了SessionFactory对象及其依赖属性 -->

  <!-- 可以通过 name 为其指定别名 name="SessionFactory" 这里让SessionFactory保持单例-->
  <object id="MyNHibernateSessionFactory"
          type="Spring.Data.NHibernate.LocalSessionFactoryObject,Spring.Data.NHibernate4" singleton="true"
          >

    <!-- 关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动 -->
    <property name="DbProvider" ref="DbProvider"/>

    <!-- 包含有映射文件的程序集,需要分析的hbm程序集名称 -->
    <property name="MappingAssemblies">
      <list>
        <value>Car.Model</value>
      </list>
    </property>

    <!-- 其他的参数 -->
    <property name="HibernateProperties">
      <dictionary>
        <!-- 方言 如果是其他数据库,或者是其他版本的MSSQL 换成对应的dialect即可  -->
        <entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
        <entry key="use_proxy_validator" value="false" />
        <entry key="show_sql" value="true"/>
      </dictionary>
    </property>
    <!-- 必须增加此项说明,与 Spring 的声明式事务集成 -->
    <property name="ExposeTransactionAwareSessionFactory" value="true" />
  </object>
</objects>


说明:

(1)我们把数据库的连接信息放在了web.config中,SpringNHibernate的配置中先使用PropertyPlaceholderConfigurer让Spring去读取了web.config中的配置信息,再用${xxx}方式获取Spring中的这些信息,也就是间接从web.config中读取配置,因为Spring.NET的配置文件要求我们需要将xml设置为“嵌入的资源”,如果直接写在Spring相关的配置文件,这样一来在程序编译后就不能够修改,所以配置于web.config可以动态改变



以上是基本的配置,下面看看要完成一个完整的业务逻辑还需要做哪些事。就以添加一个User信息为例,从下到上的配置。

(1)首先从Dao层说起,在Model层的类库中添加一个User实体,并在Mappings文件夹下添加User.hbm.xml,完成NH对实体的映射,这里主要看配置,就不贴User类的代码了,就是几个属性。

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Car.Model" namespace="Car.Model">
  <!--用户表-->
  <class name="User" table="Car_User">

    <!--id-->
    <id name="Id" column="Id" type="int">
      <generator class="identity" />
    </id>

    <property name="Name" column="Name" type="string" />
    <property name="City" column="City" type="string"/>
    <property name="Phone" column="Phone" type="string"/>
    <property name="Passwd" column="Passwd" type="string"/>
    <property name="LoginName" column="LoginName" type="string"/>
    <property name="IsAdmin" column="IsAdmin" type="bool"/>
    
  </class>
</hibernate-mapping>
在Dao层添加INHibernateSessionFactory接口

public interface INHibernateSessionFactory
    {
        ISessionFactory SessionFactory { set; get; }
    }

以及Dao类,Dao接口的代码我就直接省略了,无非是方法声明,另外我这里命名是BaseDao,不是UserDao,包括下面的Service层,因为封装的是泛型方法,可以供任意实体使用。

另外,实现INHibernateSessionFactory接口是需要让spring帮我们管理SessionFactory对象。

public class BaseDao:IBaseDao,INHibernateSessionFactory
    {
        /// <summary>
        /// 通过注入
        /// </summary>
        public ISessionFactory SessionFactory { get; set; }
        public void SaveOrUpdate(Object obj)
        {
            ISession session = SessionFactory.OpenSession();
            ITransaction tran = session.BeginTransaction();
            try
            {
                session.SaveOrUpdate(obj);
                tran.Commit();
            }
            catch (Exception)
            {
                tran.Rollback();
                throw;
            }
        }

    }
最后配置Dao层,在Config文件夹中添加Dao.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"
         xmlns:tx="http://www.springframework.net/tx">
  <description>
    这里是Dao层的配置
  </description>
<!--id只是一个标识,type则是类全名称,程序集,程序集可以省略-->
  <object id="BaseDaoImpl" type="Car.Dao.Impl.BaseDao,Car.Dao">
    <property name="SessionFactory" ref="MyNHibernateSessionFactory" /><!--这里引用的是SpringNHibernate.xml中配置的MyNHibernateSessionFactory-->
  </object>

</objects>
可以看到,这里添加了Dao类的声明,希望由Spring给我们管理它,property还告诉Spring,Dao类需要依赖一个属性,SessionFactory,希望Spring给我们自动注入它。

(2)然后是Service层,同样添加Service接口和它的实现类,代码主要看实现类

    public class BaseService : IBaseService
    {
        /// <summary>
        /// 通过注入
        /// </summary>
        public IBaseDao BaseDao { get; set; }
        public void SaveOrUpdate(Object obj)
        {
            BaseDao.SaveOrUpdate(obj);
        }
    }

同样告诉Spring,Service类需要被管理和注入属性,在Config文件夹添加配置Service.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object id="BaseServiceImpl" type="Car.Service.Impl.BaseService,Car.Service">
    <property name="BaseDao" ref="BaseDaoImpl"  /><!--这里引用Dao层对象-->
  </object>

</objects>

(3)Web层,首先添加一个控制器HomeControoller

    public class HomeController : Controller
    {
        /// <summary>
        /// 通过注入
        /// </summary>
        public IBaseService BaseService { get; set; }
        public ActionResult Index()
        {
            BaseService.SaveOrUpdate(new User{ Name="ASP.NET" });
            return View();
        }
    }

同样,在Config文件夹下添加配置,Controller.xml

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
  <object name="HomeController" type="Car.Web.Controllers.HomeController,Car.Web" singleton="false">
    <property name="BaseService" ref="BaseServiceImpl"></property><!--这里引用Service层的对象-->
  </object>
</objects>




好了,还差最后一步,当具体的请求通过映射到具体的Controller时,不能再让MVC帮我们实例化Controller,那么这一切就跟Spring没什么关系了,也就不会帮我们注入,所以我们需要使用Spring来映射请求。这里有2种方法。

1、比较原始的,自定义MVC扩展的Contoller工厂,手动的调用Spring上下文根据路由匹配的名称来实例化Controller,首先自定义Contoller工厂SpringControllerFactory

public class SpringControllerFactory : IControllerFactory
    {
        private static DefaultControllerFactory defalutf = null;

        public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            //spring context
            IApplicationContext ctx = ContextRegistry.GetContext();
            string controller = controllerName + "Controller";
            //查找是否配置该Controller
            if (ctx.ContainsObject(controller))
            {
                object controllerf = ctx.GetObject(controller);
                return (IController)controllerf;

            }
            else
            {
                if (defalutf == null)
                {
                    defalutf = new DefaultControllerFactory();
                }

                return defalutf.CreateController(requestContext, controllerName);

            }

        }

        public void ReleaseController(IController controller)
        {
            //get spring context
            IApplicationContext ctx = ContextRegistry.GetContext();
            if (!ctx.ContainsObject(controller.GetType().Name))
            {
                if (defalutf == null)
                {
                    defalutf = new DefaultControllerFactory();
                }

                defalutf.ReleaseController(controller);
            }
        }

        public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
        {
            return SessionStateBehavior.Default;
        }
    }

然后在Global.asax的Application_Start中注册工厂

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


           ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory));
        }

2、第二个办法当然是全自动化的Spring管理,直接让MvcApplication 继承于 Spring.Web.Mvc.SpringMvcApplication

public class MvcApplication : Spring.Web.Mvc.SpringMvcApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            //ControllerBuilder.Current.SetControllerFactory(typeof(SpringControllerFactory));
        }
    }

大功告成。


看到前辈们12年就有这几个框架的整合了,而今MVC6也已全面支持依赖注入,EF也是各种强大,倒是不知道哪个组合更胜一筹。


另外说明一下

1、其实我删去了一些关于事务和AOP的配置,还没理解Spring.Net对NH声明式事务的配置是怎样管理和使用的,上面贴的代码都是手动管理的事务。。,事务配置参考这篇文章

2、其实还应该让spring来管理Hibernate的Session对象,它会自动的帮我们实现一次请求中Session对象的唯一性,我这个例子也没有实现对Session对象的处理,还不太了解怎么做配置和使用。如果需要手动管理的话,也不是很复杂,因为Hibernate提供了一些这方面的实现,不需要我们手动去搞什么setData,getData了。参考这篇文章


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值