代码改变世界

NHibernate Beginnger ch2

2011-07-02 18:05  一一九九  阅读(174)  评论(0编辑  收藏  举报
NHibernate Beginnger ch2
Technorati 标签: NHibernate

Like the foundataion of a building, the structure of your database forms the base for your entire application. if you take a little care and build it well, then you overall experience with any data access technology will be greatly improverd.
    作者在这里比较旗帜鲜明的提出来数据库设计是Application的基础,在NH的世界中,也有提出来说可以根据POCO和HBM.XML来生成新的数据库的观点,需要从两个方面来考虑,第一个是数据库层的效率和维护来说,另外一个是构建的POCO或者Domain Layer层的业务逻辑是否容易,是否在DAC层获取获取的效率来衡量。其实,只有一个影响:就是NH构建出来的SQL的效率如何,不同的表结构可能影响 NH的构建SQL的效率,也影响NH的获取数据的参数,比如会所LazyLoad。
    todo: 需要明确NH在使用哪种表结构的时候获取数据的效率会受影响?
    todo: 需要明确NH生成的数据库的时候是否需要考虑额外的数据库的设计?比如索引的创建、分区等考虑;甚至包括备份还原复制策略等。
   The two rules i like to follow when creating a database are:
   1. Lay out objects in the databse  so that they are organized logically, either by the data they store or the business logic they represent .
   2. Don't store duplicate data.
   第二条好说,就是3NF的应用。第一条感觉不大明白,分类么?
   todo: 在这里突然想到的一个问题就是,对于冗余表的处理,NH是如何做到的?
NHiberante assigned IDs
  one of the criteria for a good primary key is that it is assigned by an Nhiberante POID generator. Automatic assingnment lets NHibernate manage the assignment of primary keys. NHibernate has the "smarts" baked right in to create those IDs for us and put them back into our object.
   Object 的ID是在Client段由NH赋值的,这个一般来说是毫无疑问的,像现在大多数系统都是基于段号的,采用HL方式的较多,对于那种需要采用Identity的是怎么回事呢?
    todo: 需要了解一下为什么有些表需要采用Identity的ID生成方式?
回顾一下3NF:
  To be 1NF compliant, we need to eliminate duplicative columns from the same table, and create separate tables for each group of related data and identify each row with a unique column or set of columns(the primary key). In other words, we don't want to stroe duplicate data , we want to store it once and relate to it .
  Essentially a 3NF database will store data in multiple tables to normalize the data and reduce duplication as we talked about earlier, and additionally:
  1.functional dependencied on non-key fields are elimainated by putting them in a separate table. At this level, all non-key fields are dependent on the Primary key.
  2. A row is in 3Nf if and only if it is in Second Normal Form(2NF) and if attributes that do not contribute to a description of the primary key are moved into a separate table.