一起谈.NET技术,NHibernate3剖析:Mapping篇之ConfORM实战(3):OneToOne语义

简介:   ConfORM概述  在ConfORM实战(1):概览中,描述了ConfORM简单使用。在ConfORM实战(2):原理中介绍了ConfORM的基本实现原理。如果你不熟悉ConfORM请查看前几篇文章,你也可以到http://code.google.com/p/codeconform/获取ConfORM。

  ConfORM概述

  在ConfORM实战(1):概览中,描述了ConfORM简单使用。在ConfORM实战(2):原理中介绍了ConfORM的基本实现原理。如果你不熟悉ConfORM请查看前几篇文章,你也可以到http://code.google.com/p/codeconform/获取ConfORM。

  在这之前,我们需要为HbmMapping写AsString()扩展方法:用于输出HbmMapping对象的Mapping,用于学习测试使用,具体代码参考这里

  在Domain设计中,关联关系有单向关联和双向关联两种,那么一对一我们可以分为单向一对一关联(Unidirectional one-to-one)、双向一对一主键关联(Bidirectional one-to-one (primary key association))、双向一对一外键关联(Bidirectional one-to-one (foreign key association))三种情况。这篇使用ConfORM“映射”这些Domain实例吧。

  One-to-One语义

  我们使用ObjectRelationalMapper类中的OneToOne方法定义两个对象一对一关系。

  单向一对一关联(Unidirectional one-to-one)

  1.Domain

  设计单向一对一关联Domain实例,Person对象和Address对象,人有一个地址。

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}

public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public int CivicNumber { get; set; }
}

  2.ConfORM

  使用ConfORM来配置Domain,使之编译生成我们需要的HbmMapping对象。(注意黑体)

[Test]
public void UnidirectionalOneToOneMappingDemo()
{
//show how work with one-to-one and how ConfORM understands OOP
var orm = new ObjectRelationalMapper();
var mapper = new Mapper(orm);
var entities = new[] { typeof(Person), typeof(Address) };
//use the definition of table-to-class strategy class by class
orm.TablePerClass(entities);
// Defining relations
orm.OneToOne<Person, Address>();
// Show the mapping to the console
var mapping = mapper.CompileMappingFor(entities);
Console.Write(mapping.AsString());
}

  3.Mapping

  上面测试输出HbmMapping的映射字符串,如果你使用ReSharper或者TestDriven.Net工具测试,你可以看见下面输出:

UnidirectionalOneToOneMapping  4.原理

  对于单向一对一关联,实际就是设置IManyToOneMapper,ConfORM会在IPatternsAppliersHolder的ManyToOne和ManyToOnePath集合中匹配对应模式适配器,即匹配UnidirectionalOneToOneUniqueCascadeApplier模式适配器,进行相应操作。

  UnidirectionalOneToOneUniqueCascadeApplier:应用IManyToOneMapper.Unique(true)和ManyToOneMapper.Cascade(applyCascade.HasValue?applyCascade.Value : Cascade.All)。

  双向一对一主键关联(Bidirectional one-to-one (primary key association))

  1.Domain

  设计双向一对一关联Domain实例,Person对象和Address对象,人有一个地址,地址有一个人。

public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; }
}

public class Address
{
public int Id { get; set; }
public Person Person { get; set; }
public string Street { get; set; }
public int CivicNumber { get; set; }
}

  2.ConfORM

  使用ConfORM来配置Domain,使之编译生成我们需要的HbmMapping对象。其实这个代码和上面的一样:

[Test]
public void BidirectionalOneToOneMappingDemo1()
{
//show how work with one-to-one and how ConfORM understands OOP
var orm = new ObjectRelationalMapper();
var mapper = new Mapper(orm);
var entities = new[] { typeof(Person), typeof(Address) };
//use the definition of table-to-class strategy class by class
orm.TablePerClass(entities);
// Defining relations
orm.OneToOne<Person, Address>();
//or orm.OneToOne<Address,Person>();
// Show the mapping to the console
var mapping = mapper.CompileMappingFor(entities);
Console.Write(mapping.AsString());
}

  3.Mapping

  测试生成字符串:

BidirectionalOneToOneMapping  4.原理

  对于双向一对一关联,实际就是设置IOneToOneMapper,ConfORM会在IPatternsAppliersHolder的OneToOne和OneToOnePath集合中匹配对应模式适配器,即匹配到以下三个模式适配器,进行相应操作。

  BidirectionalPrimaryKeyAssociationMasterOneToOneApplier:应用IOneToOneMapper.Cascade(Cascade.All)

  BidirectionalOneToOneAssociationPoidApplier:应用IIdMapper.Generator(Generators.Foreign(BidirectionalOneToOneOrNull(subject.ReflectedType)))

  BidirectionalPrimaryKeyAssociationSlaveOneToOneApplier:应用IOneToOneMapper.Constrained(true)

  双向一对一外键关联(Bidirectional one-to-one (foreign key association))

  Domain与双向一对一主键关联(Bidirectional one-to-one (primary key association))相同。

  2.ConfORM

  配置Domain,注意黑体:

[Test]
public void BidirectionalOneToOneMappingDemo2()
{
//show how work with one-to-one and how ConfORM understands OOP
var orm = new ObjectRelationalMapper();
var mapper = new Mapper(orm);
var entities = new[] { typeof(Person), typeof(Address) };
//use the definition of table-to-class strategy class by class
orm.TablePerClass(entities);
// Defining relations
orm.ManyToOne<Person, Address>();
orm.OneToOne<Address, Person>();

// Show the mapping to the console
var mapping = mapper.CompileMappingFor(entities);
Console.Write(mapping.AsString());
}

  3.Mapping

  测试生成字符串:

BidirectionalOneToOneMapping  4.原理

  类似的,匹配到以下模式适配器:

  BidirectionalForeignKeyAssociationManyToOneApplier:应用IManyToOneMapper.Unique(true)和IManyToOneMapper.Cascade(Cascade.All)

  BidirectionalForeignKeyAssociationOneToOneApplier:应用IOneToOneMapper.PropertyReference(GetPropertyOf(manyToOneSideType, oneToOneSideType))

  BidirectionalPrimaryKeyAssociationMasterOneToOneApplier:应用IOneToOneMapper..Cascade(Cascade.All)

  结语

  这篇文章展示ConfORM的One-to-One语义应用,映射了三种One-to-One映射。

  参考资料

  Fabio Maulo:ConfORM:“Mapping” One-To-One

目录
相关文章
|
3月前
|
开发框架 前端开发 .NET
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
七天.NET 8操作SQLite入门到实战 - (1)第七天BootstrapBlazor UI组件库引入
|
4月前
|
XML API 数据库
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
七天.NET 8操作SQLite入门到实战 - 第六天后端班级管理相关接口完善和Swagger自定义配置
|
16天前
|
安全 数据库 数据安全/隐私保护
七天.NET 8操作SQLite入门到实战 - 第五天引入SQLite-net ORM并封装常用方法
七天.NET 8操作SQLite入门到实战 - 第五天引入SQLite-net ORM并封装常用方法
|
16天前
|
开发框架 .NET API
七天.NET 8操作SQLite入门到实战 - 第四天EasySQLite前后端项目框架搭建
七天.NET 8操作SQLite入门到实战 - 第四天EasySQLite前后端项目框架搭建
|
16天前
|
存储 SQL 关系型数据库
七天.NET 8操作SQLite入门到实战 - 第三天SQLite快速入门
七天.NET 8操作SQLite入门到实战 - 第三天SQLite快速入门
|
4月前
|
SQL Shell 数据库
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
七天.NET 8操作SQLite入门到实战 - 第二天 在 Windows 上配置 SQLite环境
|
9月前
|
开发框架 前端开发 .NET
[回馈]ASP.NET Core MVC开发实战之商城系统(四)
经过一段时间的准备,新的一期【ASP.NET Core MVC开发实战之商城系统】已经开始,在之前的文章中,讲解了商城系统的整体功能设计,页面布局设计,环境搭建,系统配置,及首页【商品类型,banner条,友情链接,降价促销,新品爆款】,商品列表页面等功能的开发,今天继续讲解商品详情功能开发,仅供学习分享使用,如有不足之处,还请指正。
134 0
|
9月前
|
NoSQL 前端开发 JavaScript
MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(8)-Ant Design Blazor前端框架搭建
MongoDB从入门到实战之.NET Core使用MongoDB开发ToDoList系统(8)-Ant Design Blazor前端框架搭建
121 0
|
缓存 前端开发 JavaScript
采用.Net Core技术框架开发的医院云LIS平台源码,B/S架构
基于B/S架构的医学实验室检验系统源码,整个系统的运行基于WEB层面,只需要在对应的工作台安装一个浏览器软件有外网即可访问。全套系统采用云部署模式,部署一套可支持多家医院检验科共同使用。 采用.Net Core新的技术框架、DEV报表、前端js封装、分布式文件存储、分布式缓存等,支持LIS独立部署,Docker部署等多种方式。
|
弹性计算 数据安全/隐私保护
.Net Core实战之基于角色的访问控制的设计-
2年前开源了Sikiro.RBAC系统(https://github.com/SkyChenSky/Sikiro.RBAC)但是缺少了部署流程,这次通过申请免费的ECS,重新把流程梳理了下,并整理成改篇文章。 .Net Core实战之基于角色的访问控制的设计(https://www.cnblogs.com/skychen1218/p/13053878.html)