FNH只支持NH3.1,而且本质上还是HBM.xml, 而ConfORM却是根据Domain Entities智能映射到DB.

解释一下基于约定的映射(Convention Based Mapping 或者叫Auto Mapping)

举个约定的例子: 表名和实体类名是一样的 ,属性名和字段名是一样的, 你的表是单主键的,主键叫 “Id” 或者 “<ClassName>Id’, etc.. 当然对特定的类/字段,你可以定义一些例外
 

直接上代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using ConfOrm;
 6 using ConfOrm.Patterns;
 7 using ConfOrm.NH;
 8 using NHibernate.Cfg.MappingSchema;
 9 using ConfOrm.Shop.CoolNaming;
10 
11 namespace WF.Map
12 {
13     public class ConfORMDomainMapper
14     {
15         public ConfORMDomainMapper()
16         {
17             orm = new ObjectRelationalMapper();
18             orm.Patterns.PoidStrategies.Add(new NativePoidPattern());
19             var patternsAppliers = new CoolPatternsAppliersHolder(orm);
20 
21             mapper = new Mapper(orm, patternsAppliers);
22 
23             DefineDomain();
24             DefineMapping();
25         }
26 
27         private void DefineMapping()
28         {
29             // Defining relations
30             orm.ManyToOne<WFInstanceLevelDetail, WFInstanceLevel>();
31             orm.ManyToOne<WFInstanceLevel, WFInstance>();
32 
33             orm.ManyToOne<WFTemplateLevelDetail, WFTemplateLevel>();
34             orm.ManyToOne<WFTemplateLevel, WFTemplate>();
35         }
36 
37         private void DefineDomain()
38         {
39             //假如所有Entity都是TablePerClass,就可以这样写.
40             orm.TablePerClass(GetTypes());
41 
42             //否则就要这样
43             //var entities = new[] { typeof(Person), typeof(Address) };
44             //orm.TablePerClass(entities);
45         }
46 
47         public HbmMapping Mapping
48         {
49             get { 
50                 var mapping= mapper.CompileMappingFor(GetTypes());
51                 Console.Write(mapping.ToString());
52                 return mapping;
53 
54             }
55         }
56         /// <summary>
57         /// all the classes in my domain model inherit from a common class called BaseEntity 
58         /// I can use reflection to get all types with a single line of code
59         /// </summary>
60         /// <returns></returns>
61         IEnumerable<Type> GetTypes()
62         {
63             var type = typeof(BaseEntity);
64             return type.Assembly.GetTypes().Where(t => t.BaseType == type);
65         }
66         private readonly ObjectRelationalMapper orm;
67         private readonly Mapper mapper;
68     }
69 }

 然后在NH的SessionFactory初始化的时候加入Mapping

 1     private void InitSessionFactory()
 2     {        
 3         //Default is Read Config from hibernate.cfg.xml
 4         Configuration configure = new Configuration().Configure();
 5 
 6         //now use ConfORM to set mapping
 7         WF.Map.ConfORMDomainMapper domainMapper = new WF.Map.ConfORMDomainMapper();
 8 
 9         configure.AddDeserializedMapping(domainMapper.Mapping, null);
10 
11         sessionFactory = configure.BuildSessionFactory();
12    
13     }

 

posted on 2011-08-30 17:00  Gu  阅读(427)  评论(0编辑  收藏  举报