Csharp: Processing ing ASP. NET with Spring. NET and fluentnhibing, csharpsorting
Domain: FluentNhibernateLocalSessionFactoryObject. cs
Using System; using System. collections. generic; using System. linq; using System. text; using Spring. data. nhib.pdf; using nhib.pdf. cfg; using fluentnhib.pdf. automapping; using fluentnhib.pdf. conventions. helpers; using fluentnhib.pdf. cfg; using fluentnhib.pdf. cfg. db; using System. reflection; using fluentnhibction; using NHibernate. tool. hbm2ddl; using nhib.pdf; namespace Geovin. du. domain {/// <summary> /// tu juwen 20170623 /// </summary> public class FluentNhibernateLocalSessionFactoryObject: localSessionFactoryObject {//// <summary> //// Sets the assemblies to load that contain fluent nhibernate ings. ///// </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 = tu juwen"; 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 (fluentnhib.pdf. 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 --> <description> the data access configuration information includes DbProvider nhib.pdf </description> <! -- Reference through the context configuration file of the main application --> <object type = "Spring. objects. factory. config. propertyPlaceholderConfigurer, Spring. core "> <property name =" ConfigSections "value =" spring/databaseSettings "/> </object> <! -- Database Configuration --> <db: provider id = "DbProvider" provider = "SqlServer-2.0" connectionString = "Server =$ {db. server}; Database =$ {db. database}; User ID =$ {db. userid}; Password =$ {db. password} "/> <! -- Fluent nhib.pdf configure tu juwen Geovin Du --> <! -- You can use name to specify the alias name = "SessionFactory" --> <object id = "NHibernateSessionFactory" type = "Geovin. Du. Domain. FluentNhibernateLocalSessionFactoryObject, Domain"> <! -- For database connection configuration, directly use the settings in DbProvider, no need to provide the connection string and driver for Hibernate --> <property name = "DbProvider" ref = "DbProvider"/> <! -- Contains an assembly with a ing file, hbm Assembly name to be analyzed --> <property name = "FluentNhibernateMappingAssemblies"> <list> <value> Domain </value> </list> </property> <! -- Other parameters --> <property name = "HibernateProperties"> <dictionary> <! -- Dialect MsSql2005Dialect --> <entry key = "dialect" value = "nhibect. dialect. msSql2012Dialect "/> <entry key =" use_proxy_validator "value =" false "/> <entry key =" show_ SQL "value =" true "/> </dictionary> </property> <! -- This description must be added to integrate with Spring declarative transactions --> <property name = "ExposeTransactionAwareSessionFactory" value = "true"/> </object>
Nhib.pdf Configuration
<! -- Nhib.pdf configure tu juwen Geovin Du --> <! -- You can use name to specify the alias name = "SessionFactory" --> <object id = "NHibernateSessionFactory" type = "Spring. data. nhib.pdf. localSessionFactoryObject, Spring. data. NHibernate4 "> <! -- For database connection configuration, directly use the settings in DbProvider, no need to provide the connection string and driver for Hibernate --> <property name = "DbProvider" ref = "DbProvider"/> <! -- Contains an assembly with a ing file, hbm Assembly name to be analyzed --> <property name = "MappingAssemblies"> <list> <value> Domain </value> </list> </property> <! -- Other parameters --> <property name = "HibernateProperties"> <dictionary> <! -- Dialect --> <entry key = "dialect" value = "nhib.pdf. dialect. msSql2005Dialect "/> <entry key =" use_proxy_validator "value =" false "/> <entry key =" show_ SQL "value =" true "/> </dictionary> </property> <! -- This description must be added to integrate with Spring declarative transactions --> <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 indicates the referenced object --> <property name = "SessionFactory" ref = "NHibernateSessionFactory"/> </object> <object id = "SendMailDaoImpl" type = "Geovin. du. dao. sendMailDao, Dao "> <! -- Ref indicates the referenced object --> <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 indicates the referenced object --> <property name = "CustomerDao" ref = "CustomerDaoImpl"/> </object> <object id = "SendMailServiceImpl" type = "Geovin. du. service. sendMailService "> <! -- Ref indicates the referenced object --> <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> <! -- Page object --> <object type = "~ /Default. aspx "> <! -- Ref indicates the referenced object --> <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"?> <! -- For more information about how to configure ASP. NET applications, visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <Configuration> <configSections> <! -- Spring configuration --> <sectionGroup name = "spring"> <section name = "context" type = "Spring. Context. Support. WebContextHandler, Spring. Web"/> <! -- Database Configuration parameters --> <section name = "databaseSettings" type = "System. Configuration. NameValueSectionHandler"/> </sectionGroup> <! -- Log configuration --> <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 "/> <! -- The configuration file embedded in the assembly is the assembly name first, The namespace, and the final file name. Note that the names must be in the same case --> <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> <! -- Database configuration server address --> <databaseSettings> <add key = "db. server "value =" GEOVINDU-PC "/> <add key =" db. database "value =" DuMailSystem "/> <add key =" db. userid "value =" tu juwen "/> <add key =" db. password "value =" tu juwen "/> </databaseSettings> </spring> <! -- <Common> <logging> <factoryAdapter type = "Common. logging. log4Net. log4NetLoggerFactoryAdapter, Common. logging. log4Net "> use the log4net configuration FILE <arg key =" configType "value =" FILE-WATCH "/> <arg key =" configFile "value = "~ /Config/log4net. xml "/> </factoryAdapter> </logging> </common> --> <etettings> <! -- Provide the name for SessionFactory of OpenSessionInViewModule --> <add key = "Spring. data. nhib.pdf. 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> -->
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)); } } } }}