您的位置:首页 > 其它

《NHibernate in Action》读书笔记【二】

2010-11-08 16:30 423 查看
Basic Configuration

1.Creating a SessionFactory :

private static ISessionFactory sessionFactory = null;
public static ISessionFactory MySessionFactory
{
get
{
if(sessionFactory==null)
{
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(Employee)));
// Or cfg.AddXmlFile("Employee.hbm.xml");
sessionFactory = cfg.BuildSessionFactory();
}
}
}


It's event possible to embed XML mapping files inside .NET assemblies. You have to tell the compiler that each of these files is an embedded resource.

ISessionFactory sessionFactory = new Configuration()
.Configure()
.AddClass( typeof(Model.Item) )
.AddClass( typeof(Model.User) )
.AddClass( typeof(Model.Bid) )
.BuildSessionFactory();


The AddClass() method assumes that the name of the mapping file ends with the .hbm.xml extension and is embedded in the same assembly as the mapped class file.

If you want to add all mapped classes

(with .NET attributes) in an assembly, you can use an overload of the method HbmSerializer.Serialize()

; or, if you want to add all mapping files

embedded in an assembly, you can use the method AddAssembly

():

ISessionFactory sessionFactory = new Configuration()

.Configure()

.AddInputStream( // .NET Attributes

HbmSerializer.Default.Serialize(typeof(Model.Item).Assembly) )

.AddAssembly( typeof(Model.Item).Assembly ) // XML

.BuildSessionFactory();

2.Configuring the ADO.NET database access

<?xml version="1.0" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">
NHibernate.Connection.DriverConnectionProvider
</property>
<property name="dialect">
NHibernate.Dialect.MsSql2000Dialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.SqlClientDriver
</property>
<property name="connection.connection_string">
Data Source=(local); Initial Catalog=nhibernate;
Integrated Security=SSPI
</property>
</session-factory>
</hibernate-configuration>


connection
.provider :specifies the name of the .NET class implementing the IConnectionProvider interface.

dialect
:specifies the name of the .NET class implementing the database Dialect .

connection.driver_class
: specifies the name of the .NET class implementing the ADO.NET Driver.

connection.connection_string
: specifies a standard ADO.NET connection string , used to create a database connection.

Setting the property show_sql
to the value true enables logging of all generated SQL to the console.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: