一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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語義,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产一区二区自拍视频 | 亚洲激情欧美 | 婷婷在线免费观看 | 91久久线看在观草草青青 | 青青草97国产精品免费观看 | 91正在播放极品白嫩在线观看 | 久久久久国产一级毛片高清片 | 91精品国产91久久久久久麻豆 | 成人精品视频一区二区在线 | 91短视频在线观看免费最新91 | 天天干b | 四虎激情 | 色悠影院 | 男女免费视频网站 | 久久综合偷偷噜噜噜色 | 日本久久道一区二区三区 | 欧美亚洲国产精品久久久 | 成年美女黄网站色大片视频 | 成年人免费在线视频观看 | 亚洲影院手机版777点击进入影院 | 视频一区 精品自拍 | 黄色视屏在线免费观看 | 亚洲黄色高清视频 | 377日本大胆欧美人术艺术 | 婷婷在线网站 | 激情图片视频小说 | 久久亚洲国产成人精品性色 | 午夜久久久精品 | 亚洲 欧美 国产另类首页 | 国产福利小视频在线播放观看 | 一区二区在线观看视频在线 | 国产麻豆综合视频在线观看 | 久久ww | 4399一级成人毛片 | 探花在线播放 | 91高清免费视频 | 色毛片 | 国产玖玖视频 | 日韩在线观看中文字幕 | 人人干在线视频 | 国产在线永久视频 |