csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate

Domain: FluentNhibernateLocalSessionFactoryObject.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using NHibernate.Cfg;
using FluentNHibernate.Automapping;
using FluentNHibernate.Conventions.Helpers;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using System.Reflection;
using FluentNHibernate;
using NHibernate.Tool.hbm2ddl;
using NHibernate; namespace Geovin.Du.Domain
{
/// <summary>
/// 涂聚文20170623
/// </summary>
public class FluentNhibernateLocalSessionFactoryObject : LocalSessionFactoryObject
{
///// <summary>
///// Sets the assemblies to load that contain fluent nhibernate mappings.
///// </summary>
///// <value>The mapping assemblies.</value>
//public string[] FluentNhibernateMappingAssemblies {
// get;
// set;
//}MySQLConfiguration.Standard public string[] FluentNhibernateMappingAssemblies { get; set; }
public string ConnectionStringName { get; set; }
static readonly object factorylock = new object(); protected override void PostProcessConfiguration(Configuration config)
{
ConnectionStringName = "Server=GEOVINDU-PC;Database=DuMailSystem;User ID=sa;Password=涂聚文";
base.PostProcessConfiguration(config);
FluentConfiguration fluentConfig = Fluently.Configure(config)
.Database(MsSqlConfiguration.MsSql2012.ConnectionString(ConnectionStringName)) //MsSql2005 MsSql2012
.ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(false, true));
Array.ForEach(FluentNhibernateMappingAssemblies,
assembly => fluentConfig.Mappings(
m => m.FluentMappings.AddFromAssembly(Assembly.Load(assembly))
.Conventions.Add(FluentNHibernate.Conventions.Helpers.DefaultLazy.Never())
)
);
fluentConfig.BuildSessionFactory();
}
} //class
}
Dao:dataAccess.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">
<!--描述-->
<description>
数据访问的配置信息
包括:DbProvider
NHibernate
</description> <!-- 通过主应用程序的上下文配置文件引用 -->
<object type="Spring.Objects.Factory.Config.PropertyPlaceholderConfigurer, Spring.Core">
<property name="ConfigSections" value="spring/databaseSettings"/>
</object> <!-- 数据库的配置-->
<db:provider id="DbProvider"
provider="SqlServer-2.0"
connectionString="Server=${db.server};Database=${db.database};User ID=${db.userid};Password=${db.password}"
/> <!-- Fluent NHibernate 配置 涂聚文 Geovin Du--> <!-- 可以通过 name 为其指定别名 name="SessionFactory" -->
<object id="NHibernateSessionFactory" type="Geovin.Du.Domain.FluentNhibernateLocalSessionFactoryObject, Domain">
<!--关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动-->
<property name="DbProvider" ref="DbProvider"/>
<!--包含有映射文件的程序集,需要分析的hbm程序集名称-->
<property name="FluentNhibernateMappingAssemblies">
<list>
<value>Domain</value>
</list>
</property>
<!--其他的参数-->
<property name="HibernateProperties">
<dictionary>
<!--方言MsSql2005Dialect-->
<entry key="dialect" value="NHibernate.Dialect.MsSql2012Dialect"/>
<entry key="use_proxy_validator" value="false" />
<entry key="show_sql" value="true"/>
</dictionary>
</property>
<!--必须增加此项说明,与 Spring 的声明式事务集成-->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
NHibernate 配置
<!-- NHibernate 配置 涂聚文 Geovin Du --> <!-- 可以通过 name 为其指定别名 name="SessionFactory" -->
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject,Spring.Data.NHibernate4">
<!--关于数据库连接的配置,直接使用 DbProvider 中的设置,这样,不需要为 Hibernate 再提供连接串和驱动-->
<property name="DbProvider" ref="DbProvider"/>
<!--包含有映射文件的程序集,需要分析的hbm程序集名称-->
<property name="MappingAssemblies">
<list>
<value>Domain</value>
</list>
</property>
<!--其他的参数-->
<property name="HibernateProperties">
<dictionary>
<!--方言-->
<entry key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
<entry key="use_proxy_validator" value="false" />
<entry key="show_sql" value="true"/>
</dictionary>
</property>
<!--必须增加此项说明,与 Spring 的声明式事务集成-->
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
Dao:objects.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"> <object id="CustomerDaoImpl" type="Geovin.Du.Dao.CustomerDao,Dao">
<!--ref 表示引用的对象-->
<property name="SessionFactory" ref="NHibernateSessionFactory" />
</object> <object id="SendMailDaoImpl" type=" Geovin.Du.Dao.SendMailDao,Dao">
<!--ref 表示引用的对象-->
<property name="SessionFactory" ref="NHibernateSessionFactory" />
</object> </objects>
Service:objects.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net"> <object id="CustomerServiceImpl" type="Geovin.Du.Service.CustomerService" >
<!--ref 表示引用的对象-->
<property name="CustomerDao" ref="CustomerDaoImpl" />
</object>
<object id="SendMailServiceImpl" type="Geovin.Du.Service.SendMailService">
<!--ref 表示引用的对象-->
<property name="SendMailDao" ref="SendMailDaoImpl" />
</object>
</objects>
FluentNHibernateSpingNetDemo:objects.xml
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="framework" type="FluentNHibernateSpingNetDemo.Framework">
<property name="Name" value="Spring.NET"/>
</object> <!-- 页面对象 -->
<object type="~/Default.aspx">
<!-- ref 表示引用的对象 -->
<property name="Framework" ref="framework"/>
</object> <object type="~/Customer/index.aspx">
<property name="CustomerService" ref="CustomerServiceImpl"/>
</object> <object type="~/Send/index.aspx">
<property name="SendMailService" ref="SendMailServiceImpl"/>
</object>
</objects>
FluentNHibernateSpingNetDemo:Web.config
<?xml version="1.0"?> <!--
有关如何配置 ASP.NET 应用程序的详细信息,请访问
http://go.microsoft.com/fwlink/?LinkId=169433
--> <configuration>
<configSections>
<!-- Spring 的配置 -->
<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.WebContextHandler, Spring.Web"/> <!-- 数据库的配置参数 -->
<section name="databaseSettings" type="System.Configuration.NameValueSectionHandler"/>
</sectionGroup> <!-- 日志配置 -->
<sectionGroup name="common">
<section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
</sectionGroup>
</configSections>
<connectionStrings>
<add name="SqlConnection" connectionString="Server=GEOVINDU-PC;Database=DuMailSystem;User ID=sa;Password=770214" providerName="System.Data.SqlClient"/>
<add name="SqlRole" connectionString="dbo"/>
<!--<add name="SqlConnection" connectionString="Server=124.172.156.191;Database=dupcitgeovindu;User ID=dupcitgeovindu_f;Password=lf770214;" providerName="System.Data.SqlClient"/>
<add name="SqlRole" connectionString="dupcitgeovindu_f"/>-->
</connectionStrings>
<spring>
<context>
<resource uri="~/Config/objects.xml"/> <!-- 嵌入在程序集中的配置文件 ,首先是程序集名称,然后命名空间,最后文件名, 注意名称的大小写必须完全一致 -->
<resource uri="assembly://Dao/Dao.Config/dataAccess.xml"/>
<resource uri="assembly://Dao/Dao.Config/objects.xml"/>
<resource uri="assembly://Service/Service.Config/objects.xml"/>
</context> <!--数据库配置服务器地址-->
<databaseSettings>
<add key="db.server" value="GEOVINDU-PC"/>
<add key="db.database" value="DuMailSystem"/>
<add key="db.userid" value="涂聚文"/>
<add key="db.password" value="涂聚文"/>
</databaseSettings>
</spring> <!--<common>
<logging>
<factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4Net">
使用 log4net 配置文件
<arg key="configType" value="FILE-WATCH" />
<arg key="configFile" value="~/Config/log4net.xml" />
</factoryAdapter>
</logging>
</common>--> <appSettings>
<!-- 为 OpenSessionInViewModule 的 SessionFactory 提供名字 -->
<add key="Spring.Data.NHibernate.Support.OpenSessionInViewModule.SessionFactoryObjectName" value="NHibernateSessionFactory"/>
</appSettings> <system.web>
<compilation debug="true" targetFramework="4.0" /> <!--<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>-->
<httpModules>
<!-- Spring 提供的 Module -->
<add name="Spring" type="Spring.Context.Support.WebSupportModule, Spring.Web"/> <!--
由 Spring 自动打开会话,必须提供一个名为 SessionFactory 的会话工厂
使用后,可以使用 SessionFactory 的 GetCurrentSession 方法获取会话
-->
<add name="OpenSessionInView"
type="Spring.Data.NHibernate.Support.OpenSessionInViewModule, Spring.Data.NHibernate4"/>
</httpModules>
<httpHandlers>
<!-- Spring 提供的处理程序 -->
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
<!-- 取消 Spring.NET 对于 Web 服务的处理 -->
<!--<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web"/>-->
<add verb="*" path="ContextMonitor.ashx" type="Spring.Web.Support.ContextMonitor, Spring.Web"/>
<add verb="*" path="*.ashx" type="Spring.Web.Support.DefaultHandlerFactory, Spring.Web"/>
</httpHandlers>
<!--<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership> <profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile> <roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>--> </system.web> <!--<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>-->
</configuration>
http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
Fluently.Configure(config)
.Database(MySQLConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey(“connectionString”)))
.Mappings( m => m.FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();
} protected override ISessionFactory NewSessionFactory(Configuration config)
{
config = Fluently.Configure(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.BuildConfiguration();
return base.NewSessionFactory(config);
} here is the implementation if you are using FluentNhibernate auto mapping :
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config); var autoMappingCfg = new AutoMappingConfiguration(); var autoMap = AutoMap.AssemblyOf(autoMappingCfg)
.Conventions.Add(DefaultCascade.All(), DefaultLazy.Never())
.Conventions.Add()
.Override(map => { map.IgnoreProperty(i => i.Total); }); Fluently.Configure(config)
.Mappings(m => m.AutoMappings.Add(autoMap))
.BuildConfiguration();
} in outmapping you don’t need to inject FluentNhibernateMappingAssemblies. so configure Sping.Net accordingly using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using FluentNHibernate;
using System.Reflection;
using NHibernate.Cfg; namespace SessionFactories
{
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
/// <summary>
/// Sets the assemblies to load that contain fluent nhibernate mappings.
/// </summary>
/// <value>The mapping assemblies.</value>
public string[] FluentNhibernateMappingAssemblies
{
get;
set;
} protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
if(FluentNhibernateMappingAssemblies != null)
{
foreach(string assemblyName in FluentNhibernateMappingAssemblies)
{
config.AddMappingsFromAssembly(Assembly.Load(assemblyName));
}
}
}
}
}
csharp: Configuring ASP.NET with Spring.NET and FluentNHibernate的更多相关文章
- [转]ASP.NET MVC Spring.NET NHibernate 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- ASP.NET MVC Spring.NET NHibernate 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- ASP.NET MVC Spring.NET 整合
请注明转载地址:http://www.cnblogs.com/arhat 在整合这三个技术之前,首先得说明一下整合的步骤,俗话说汗要一口一口吃,事要一件一件做.同理这个三个技术也是.那么在整合之前,需 ...
- JavaPersistenceWithMyBatis3笔记-第5章Configuring MyBatis in a Spring applications-001
一. 1.Mapper /** * */ package com.mybatis3.mappers; import java.util.List; import org.apache.ibatis.a ...
- Asp.Net mvc4 +Spring
添加相应的引用对象.(以下全部) 修改mvc的Global.asax文件内容 需要将控制器中原来需要new出来的对象改成属性成员 添加这个属性的注入对象 再去修改spring对web.config的一 ...
- Asp.Net 熟悉 Spring
注:(为加强记忆,所以记录下来,对于有些地方为什么那样写,我也不太理解) 一.我们先创建个窗体应用程序Demos,事先熟悉它是这么实现的 第一步,先在项目的根目录下建一个library文件夹,目的是放 ...
- 在网页中显示CHM (c# csharp .net asp.net winform)
CHM即“已编译的帮助文件”,主要由.hhc(目录文件)..hhk(索引文件)以及相应的帮助主题文件(.html,.htm)这些内容编译而成. 方法对比 在网页中显示CHM内容,大致有以下几种办法: ...
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- 【转载】ASP.NET MVC Web API 的路由选择
此文章描述了ASP.NET Web API如何将Http请求路由到controller. 路由表 在ASP.NET Web API中,controller是用来处理HTTP请求的一个类.这个类中用于处 ...
随机推荐
- zookeeper配置文件共享中心
最近频繁的系统上线,每次打包都要把配置文件替换为正式环境的配置文件,虽然说就是复制粘贴的事,架不住文件杂乱,而且多. 期初的想法是有没有办法将配置文件与系统隔离开来,这样在更新时候,就只需要更新代码部 ...
- Runtime之方法
前两篇介绍了类与对象.成员变量&属性&关联对象的相关知识,本篇我们将开始讲解Runtime中最有意思的一部分内容:消息处理机制.我们从一个示例开始. 在OC中,我们使用下面这种方式来调 ...
- flask源码解析之上下文
引入 对于flask而言,其请求过程与django有着截然不同的流程.在django中是将请求一步步封装最终传入视图函数的参数中,但是在flask中,视图函数中并没有请求参数,而是将请求通过上下文机制 ...
- Post传值到后台经典场景(C#)
经典场景 传输内容包含 文件 注意事项:类型必须为form-data [HttpPost] [Route("api/Test")] public JsonResult Test(s ...
- python实现归并排序算法
归并排序(英语:Merge sort,或mergesort),是创建在归并操作上的一种有效的排序算法,效率为O(nlogn). 1945年由约翰·冯·诺伊曼首次提出.该算法是采用分治法(Divide ...
- Hadoop生态系统之HDFS
一.介绍 HDFS : 分布式文件系统(distributed filesystem),主从结构. 以流式数据访问模式来存储超大文件,运行于商用硬件集群上. 超大文件: 几百M,几百G,甚至几百TB大 ...
- OkHttp3 任务队列
OkHttp3 有两种运行方式: 1.同步阻塞调用并且直接返回: 2.通过内部线程池分发调度实现非阻塞的异步回调; 下面讲的是非阻塞异步回调,OkHttp在多并发网络下的分发调度过程,主要是Dispa ...
- salesforce lightning零基础学习(六)Lightning Data Service(LDS)
本篇可参看:https://trailhead.salesforce.com/modules/lightning_data_service Lightning中针对object的detail页面,一个 ...
- nmcli工具详解
目录 1. nmcli 安装 2. nmcli 基本选项 3. general 常规选项 3.1 status 3.2 hostname 3.3 permissions 3.4 loggin 4. n ...
- go中的读写锁RWMutex
读写锁是针对于读写操作的互斥锁. 基本遵循两大原则: 1.可以随便读.多个goroutin同时读. 2.写的时候,啥都不能干.不能读,也不能写. 解释: 在32位的操作系统中,针对int64类型值的读 ...