NHibernate3剖析:Mapping篇之ConfORM实战(3):OneToOne语义

  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工具测试,你可以看见下面输出:

UnidirectionalO<a href=/itjie/NETjishu/ target=_blank class=infotextkey>NET</a>oOneMapping  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

  测试生成字符串:

BidirectionalO<a href=/itjie/NETjishu/ target=_blank class=infotextkey>NET</a>oOneMapping  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

  测试生成字符串:

BidirectionalO<a href=/itjie/NETjishu/ target=_blank class=infotextkey>NET</a>oOneMapping  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

NET技术NHibernate3剖析:Mapping篇之ConfORM实战(3):OneToOne语义,转载需保留来源!

郑重声明:本文版权归原作者所有,转载文章仅为传播更多信息之目的,如作者信息标记有误,请第一时间联系我们修改或删除,多谢。