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 name | Description |
connection.provider | Provider class to open and close database connections. |
connection.driver_class | This is specific to the RDBMS used, and is typically set by the dialect. |
connection.connection_string | Database connection string. |
connection.connection_string_ name | Name of connection string in |
connection.isolation | Transaction isolation level. |
dialect | Required. A class to build RDBMS-specific SQL strings. Typically, this is one of the many dialects from the NHibernate.Dialect namespace |
show_sql | Boolean 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_class | Class to manage contextual sessions |
query.substitutions | Comma-separated list of translations to perform on query strings. For example, True=1, Yes=1, False=0, No=0. |
sql_exception_converter | Class to convert RDBMS-specific ADO.NET Exceptions to custom exceptions |
prepare_sql | Boolean value. Prepares SQL statements and caches the execution plan for the duration of the database connection. |
command_timeout | Number of seconds to wait for a SQL command to complete before timing out. |
adonet.batch_size | Number of SQL commands to send at once before waiting for a response from the database |
generate_statistics | Enables tracking of some statistical information, such as the number of queries executed and entities loaded |
proxyfactory.factory_class | Required. Specifies a factory class for our chosen proxy framework, in this case Castle DynamicProxy2 |
format_sql | Adds 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://pwwzsj.com/article/ccchec.html