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 }
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 }
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 }
分类:
NHibernate
标签:
ConfORM
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏
· Manus爆火,是硬核还是营销?
2006-08-30 .net 利用ssl加密,密码验证smtp发送邮件