NHibernate項(xiàng)目中都有App.config,主要是用來(lái)配置項(xiàng)目中的日志與數(shù)據(jù)庫(kù)等。
創(chuàng)新互聯(lián)專注于企業(yè)成都全網(wǎng)營(yíng)銷、網(wǎng)站重做改版、單縣網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站定制開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)營(yíng)銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為單縣等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。典型的App.config配置文件(這里包括log4net):
View Code<?xml version="1.0" encoding="utf-8"?><configuration> <configSections> <!--定義配置節(jié)點(diǎn)--> <sectionname="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/> <sectionname="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net"/> </configSections> <connectionStrings> <!--配置數(shù)據(jù)庫(kù)--> <addname="db" connectionString="Data Source=.;Initial Catalog=NHCookbook;Integrated Security=True"/> </connectionStrings> <log4net> <appendername="trace"
type="log4net.Appender.TraceAppender, log4net"> <layouttype="log4net.Layout.PatternLayout, log4net"> <paramname="ConversionPattern"
value=" %date %level %message%newline" /> </layout> </appender> <root> <levelvalue="ALL" /> <appender-refref="trace" /> </root> <loggername="NHibernate"> <levelvalue="INFO" /> </logger> <!--<logger name="MyApp.Project.SomeNamespace.Foo">
<level value="WARN" />
</logger>--> <loggername="NHibernate.SQL"> <levelvalue="DEBUG" /> </logger> </log4net>
<hibernate-configuration
xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <!--指定代理工廠類--> <propertyname="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</property> <!--數(shù)據(jù)庫(kù)類型--> <propertyname="dialect">
NHibernate.Dialect.MsSql2008Dialect,
NHibernate
</property> <!--數(shù)據(jù)庫(kù)名--> <propertyname="connection.connection_string_name">
db
</property> <!--數(shù)據(jù)庫(kù)并發(fā)數(shù)--> <propertyname="adonet.batch_size">
100
</property> <!--映射的類名--> <mappingassembly="Eg.Core"/> </session-factory> </hibernate-configuration></configuration>
一些配置節(jié)點(diǎn)說(shuō)明(水平有限就不翻譯了,直接貼書上的表格):
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 <connectionStrings> element. |
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的數(shù)據(jù)訪問(wèn)架構(gòu):
將hibernate-configuration配置在單獨(dú)的文件中
我們也可以將App.config中的hibernate-configuration節(jié)點(diǎn)抽取出來(lái),單獨(dú)配置在另一個(gè)XML文件中,并將該XML文件的屬性,復(fù)制到輸出目錄設(shè)置為:如果較新復(fù)制。
利用代碼配置hibernate-configuration
我們也可以通過(guò)代碼來(lái)配置hibernate-configuration節(jié)點(diǎn)中的內(nèi)容.
App.config文件還是要的:
<?xml version="1.0" encoding="utf-8"?><configuration><connectionStrings><addname="db" connectionString="Server=.SQLExpress;
Database=NHCookbook; Trusted_Connection=SSPI"/></connectionStrings></configuration>
然后在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<ProxyFactoryFactory>())
.DataBaseIntegration(db=>
{
db.Dialect<MsSql2008Dialect>();
db.ConnectionStringName= "db";
db.BatchSize= 100;
})
.AddAssembly("Eg.Core");
var sessionFactory = nhConfig.BuildSessionFactory();
Console.WriteLine("NHibernate Configured!");
Console.ReadKey();
注意:DataBaseIntegration是一個(gè)擴(kuò)展方法,要引用NHibernate.Cfg.Loquacious命名空間。
利用Fluent NHibernate
App.config:
<?xml version="1.0" encoding="utf-8"?><configuration><connectionStrings> <addname="db" connectionString="Server=.SQLExpress;
Database=NHCookbook; Trusted_Connection=SSPI"/></connectionStrings></configuration>
增加代碼:
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<ProxyFactoryFactory>()
.AdoNetBatchSize(100)
)
.Mappings(mappings=> mappings.FluentMappings
.AddFromAssemblyOf<ProductMapping>()
)
.BuildConfiguration();
var sessionFactory = nhConfig.BuildSessionFactory();
Console.WriteLine("NHibernate configured fluently!");
Console.ReadKey();
利用ConfORM Mappings
App.config:
View Code<?xml version="1.0" encoding="utf-8"?><configuration><configSections> <sectionname="hibernate-configuration"
type="NHibernate.Cfg.ConfigurationSectionHandler,
NHibernate"/></configSections><connectionStrings> <addname="db" connectionString="Server=.SQLExpress;
Database=NHCookbook; Trusted_Connection=SSPI"/></connectionStrings><hibernate-configuration
xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <propertyname="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle
</property> <propertyname="dialect">
NHibernate.Dialect.MsSql2008Dialect,
NHibernate
</property> <propertyname="connection.connection_string_name">
db
</property> <propertyname="adonet.batch_size">
100
</property> </session-factory></hibernate-configuration></configuration>
增加代碼:
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();
本文標(biāo)題:Nhibernate3.0cookbook學(xué)習(xí)筆記配置與架構(gòu)-創(chuàng)新互聯(lián)
當(dāng)前鏈接:http://jinyejixie.com/article42/ccchec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、云服務(wù)器、品牌網(wǎng)站設(shè)計(jì)、軟件開(kāi)發(fā)、標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容