网站建设资讯

NEWS

网站建设资讯

Nhibernate3.0cookbook学习笔记配置与架构-创新互联

NHibernate项目中都有App.config,主要是用来配置项目中的日志与数据库等。

创新互联专注于企业成都全网营销、网站重做改版、单县网站定制设计、自适应品牌网站建设、HTML5商城网站定制开发、集团公司官网建设、外贸营销网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为单县等各大城市提供网站开发制作服务。

典型的App.config配置文件(这里包括log4net):

View Code
                                                            
        
                NHibernate.ByteCode.Castle.ProxyFactoryFactory,
                NHibernate.ByteCode.Castle
   
                NHibernate.Dialect.MsSql2008Dialect,
                NHibernate
   
                db
   
                100
        

一些配置节点说明(水平有限就不翻译了,直接贴书上的表格):

Property nameDescription
connection.providerProvider class to open and close database
connections.
connection.driver_classThis is specific to the RDBMS used, and is typically set
by the dialect.
connection.connection_stringDatabase connection string.
connection.connection_string_
name
Name of connection string in
element.
connection.isolationTransaction isolation level.
dialectRequired. A class to build RDBMS-specific SQL strings.
Typically, this is one of the many dialects from the
NHibernate.Dialect namespace
show_sqlBoolean value. Set to true to log all SQL statements to
Console.Out. Alternatively, log4net may be used to
log to other locations
current_session_context_classClass to manage contextual sessions
query.substitutionsComma-separated list of translations to perform on
query strings. For example, True=1, Yes=1, False=0,
No=0.
sql_exception_converterClass to convert RDBMS-specific ADO.NET Exceptions
to custom exceptions
prepare_sqlBoolean value. Prepares SQL statements and caches
the execution plan for the duration of the database
connection.
command_timeoutNumber of seconds to wait for a SQL command to
complete before timing out.
adonet.batch_sizeNumber of SQL commands to send at once before
waiting for a response from the database
generate_statisticsEnables tracking of some statistical information,
such as the number of queries executed and entities
loaded
proxyfactory.factory_classRequired. Specifies a factory class for our chosen
proxy framework, in this case Castle DynamicProxy2
format_sqlAdds line endings for easier-to-read SQL statements

顺便贴一下Nhibernate的数据访问架构:

将hibernate-configuration配置在单独的文件中

我们也可以将App.config中的hibernate-configuration节点抽取出来,单独配置在另一个XML文件中,并将该XML文件的属性,复制到输出目录设置为:如果较新复制。

利用代码配置hibernate-configuration

我们也可以通过代码来配置hibernate-configuration节点中的内容.

App.config文件还是要的:

然后在Program.cs代码中先引用命名空间:

using NHibernate.ByteCode.Castle;
using NHibernate.Cfg;
using NHibernate.Cfg.Loquacious;
using NHibernate.Dialect;

在Main方法中增加如下代码:

var nhConfig = new Configuration()
.Proxy(proxy=>
proxy.ProxyFactoryFactory())
.DataBaseIntegration(db=>
{
db.Dialect();
db.ConnectionStringName= "db";
db.BatchSize= 100;
})
.AddAssembly("Eg.Core");
var sessionFactory = nhConfig.BuildSessionFactory();
Console.WriteLine("NHibernate Configured!");
Console.ReadKey();

注意:DataBaseIntegration是一个扩展方法,要引用NHibernate.Cfg.Loquacious命名空间。

利用Fluent NHibernate

App.config:

  

增加代码:

using Eg.FluentMappings.Mappings;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.ByteCode.Castle;
var nhConfig = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(connstr=>
connstr.FromConnectionStringWithKey("db")
)
.ProxyFactoryFactory()
.AdoNetBatchSize(100)
)
.Mappings(mappings=> mappings.FluentMappings
.AddFromAssemblyOf()
)
.BuildConfiguration();
var sessionFactory = nhConfig.BuildSessionFactory();
Console.WriteLine("NHibernate configured fluently!");
Console.ReadKey();

利用ConfORM Mappings

App.config:

View Code
       
        NHibernate.ByteCode.Castle.ProxyFactoryFactory,
        NHibernate.ByteCode.Castle
 
        NHibernate.Dialect.MsSql2008Dialect,
        NHibernate
 
        db
 
        100
  

增加代码:

using Eg.ConfORMMapping.Mappings;
using NHibernate.Cfg;
var mappingFactory = new MappingFactory();
var mapping = mappingFactory.CreateMapping();
var nhConfig = new Configuration().Configure();
nhConfig.AddDeserializedMapping(mapping,null);
var sessionFactory = nhConfig.BuildSessionFactory();
Console.WriteLine("NHibernate configured!");
Console.ReadKey();

本文名称:Nhibernate3.0cookbook学习笔记配置与架构-创新互联
文章网址:http://cdweb.net/article/ccchec.html