代码改变世界

NHibernate in Action 2.3 Basic Configuration

2011-07-31 12:03  一一九九  阅读(152)  评论(0编辑  收藏  举报

    Generallly, NHibernate is used in two and three tiered client /server applications, with nhibernate deployed only on the server, the client application is usually a web browser, but windows client application aren’t uncommon.

2.3.1 Creating a SessionFactory

    to create an ISessionFactory instance, you first create a single instance of Configuration during application initialization and use it to set the database access and mapping information. Once configured, the Configuration instance is used to create the SessionFactory.

    SessionFactory 是需要在database access info 和mpping information 都设置好以后才能Build的。

Method chaining

method chaining is a programmin style supportd by many NHibernate interfaces(also caolled fluent interfaces). this style is more popular in smalltalk than in .net and is considered by some people to be less readalbe and more difficult to debug than be more acceptd .net style, but it’s convenient in most cases.

most .net devlopers declare setter or adder methods to be of type void, meaning they return no value. in smlltalk,which has no void type, setter and adder methods ususally return the receiving object. this would let you rewrite the previous code examle as follows:

          ISessionFactory sessionFactory = new Configuration().Configure().AddXMLFile(“Employee.hbm.xml”).BuildSessionFactory();

notice that you don’t need to declare a local variable for the Configuration.

这种method chain 的关键在chain,如果某一个chain坏掉了,比如说返回的是NULL,就容易导致无效引用的错误,需要采用这种方式的client心中有数,确保chain是完整的。

 

Working with the mapping files
   作者在这里提到了几种添加Mapping Files的方式,前提是one class one mapping file , 有如下几种:

  • AddXMLFile().  直接添加相应的XML文件。
    • cfg.AddXMLFile(“Employee.hbm.xml”)
  • AddClass().  前提是 embed xml mapping files inside  .net assemblies. you havae to tell the compiler that each of these files is an embedded resource.
    • cfg.AddClass(typeof(Model.Bid))
  • AddAssembly().  前提同样是embed xml mapping files inside  .net assemblies. you havae to tell the compiler that each of these files is an embedded resource.
    • cfg.AddAssembly(typeof(Model.Item).Assembly)
    • <mapping assembly="HelloNH"/>  这种是在hibernate .cfg.xml中指定的,作者没有明说,估计是3.0GA中有的。

Mutliple databases and session factories

        each sessionfactory if then avaialbe for one database and ready to produce ISession instance to work with that particular database and a set of class mappings.

Q: 怎么样在NH中连接多个数据库呢?
A: c.Configure(); 在这里指定配置文件的名称,需要同时实现一个FactorySessionFactory来获取不同的SessionFactory。

2.3.2 Configuring the ADo.net database access.

作者给了两幅图,关于ConnctionPool的,第一副是.net application interact with ADO.net without NHibernate,  the application code ususally receives an ADO.net connection from the conneciton pool(which is configured transparently) and uses it to execute SQL statements.

image

另外一幅则是NH作为中间的数据获取层的一个展示:

image

Nhibernate acts as a client of ADO.net and its connection pool.

Q:NH的Connection pool 怎么回事?

A:

接下来就是配置NH的几个部分了,通过App.config和Hibernate.cfg.xml两种方式来配置。

2.4 Loging

       简要说明了如何配置log4net