天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

NHibernate3剖析:Mapping篇之ConfORM實戰(zhàn)(3):OneToOne語義

  ConfORM概述

  在ConfORM實戰(zhàn)(1):概覽中,描述了ConfORM簡單使用。在ConfORM實戰(zhàn)(2):原理中介紹了ConfORM的基本實現(xiàn)原理。如果你不熟悉ConfORM請查看前幾篇文章,你也可以到http://code.google.com/p/codeconform/獲取ConfORM。

  在這之前,我們需要為HbmMapping寫AsString()擴展方法:用于輸出HbmMapping對象的Mapping,用于學習測試使用,具體代碼參考這里

  在Domain設(shè)計中,關(guān)聯(lián)關(guān)系有單向關(guān)聯(lián)和雙向關(guān)聯(lián)兩種,那么一對一我們可以分為單向一對一關(guān)聯(lián)(Unidirectional one-to-one)、雙向一對一主鍵關(guān)聯(lián)(Bidirectional one-to-one (primary key association))、雙向一對一外鍵關(guān)聯(lián)(Bidirectional one-to-one (foreign key association))三種情況。這篇使用ConfORM“映射”這些Domain實例吧。

  One-to-One語義

  我們使用ObjectRelationalMapper類中的ONEToOne方法定義兩個對象一對一關(guān)系。

  單向一對一關(guān)聯(lián)(Unidirectional one-to-one)

  1.Domain

  設(shè)計單向一對一關(guān)聯(lián)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.原理

  對于單向一對一關(guān)聯(lián),實際就是設(shè)置IManyToOneMapper,ConfORM會在IPatternsAppliersHolder的ManyToOne和ManyToOnePath集合中匹配對應(yīng)模式適配器,即匹配UnidirectionalONEToOneUniqueCascadeApplier模式適配器,進行相應(yīng)操作。

  UnidirectionalONEToOneUniqueCascadeApplier:應(yīng)用IManyToOneMapper.Unique(true)和ManyToOneMapper.Cascade(applyCascade.HasValue?applyCascade.Value : Cascade.All)。

  雙向一對一主鍵關(guān)聯(lián)(Bidirectional one-to-one (primary key association))

  1.Domain

  設(shè)計雙向一對一關(guān)聯(lián)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.原理

  對于雙向一對一關(guān)聯(lián),實際就是設(shè)置IONEToOneMapper,ConfORM會在IPatternsAppliersHolder的ONEToOne和ONEToOnePath集合中匹配對應(yīng)模式適配器,即匹配到以下三個模式適配器,進行相應(yīng)操作。

  BidirectionalPrimaryKeyAssociationMasterONEToOneApplier:應(yīng)用IONEToOneMapper.Cascade(Cascade.All)

  BidirectionalONEToOneAssociationPoidApplier:應(yīng)用IIdMapper.Generator(Generators.Foreign(BidirectionalONEToOneOrNull(subject.ReflectedType)))

  BidirectionalPrimaryKeyAssociationSlaveONEToOneApplier:應(yīng)用IONEToOneMapper.Constrained(true)

  雙向一對一外鍵關(guān)聯(lián)(Bidirectional one-to-one (foreign key association))

  Domain與雙向一對一主鍵關(guān)聯(lián)(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:應(yīng)用IManyToOneMapper.Unique(true)和IManyToOneMapper.Cascade(Cascade.All)

  BidirectionalForeignKeyAssociationONEToOneApplier:應(yīng)用IONEToOneMapper.PropertyReference(GetPropertyOf(manyToOneSideType, oNEToOneSideType))

  BidirectionalPrimaryKeyAssociationMasterONEToOneApplier:應(yīng)用IONEToOneMapper..Cascade(Cascade.All)

  結(jié)語

  這篇文章展示ConfORM的One-to-One語義應(yīng)用,映射了三種One-to-One映射。

  參考資料

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

NET技術(shù)NHibernate3剖析:Mapping篇之ConfORM實戰(zhàn)(3):OneToOne語義,轉(zhuǎn)載需保留來源!

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

主站蜘蛛池模板: 午夜视频在线观看国产 | 免费啪视频观试看视频 | 亚洲无吗在线视频 | WWW国产精品人妻一二三区 | 欧美互交人妖247 | 视频成人永久免费看 | 看80后操 | 偷拍久久国产视频免费 | 伦理79电影网在线观看 | 亚洲VA天堂VA欧美VA在线 | 免费三级网址 | 日韩中文字幕亚洲无线码 | 一本道手机无码在线看 | 极品少妇高潮啪啪AV无码 | 2019午夜75福利不卡片在线 | 国产乱妇乱子在线播视频播放网站 | 乱爱性全过程免费视频 | 女人夜夜春 | 国产三级精品三级男人的天堂 | 一级毛片在线免费视频 | 久久本道久久综合伊人 | 国产精品无码AV天天爽色欲 | 欧美在线激情 | 午夜国产羞羞视频免费网站 | 日日噜噜夜夜狠狠视频 | 久久女婷五月综合色啪 | 精品国产中文字幕在线视频 | 91欧洲在线视精品在亚洲 | 和姐姐做插得很深 | 一二三四视频免费社区5 | 忘忧草在线影院WWW日本动漫 | 青青热久久综合网伊人 | 成都电影免费中文高清 | 儿子操妈妈 | 臀精插宫NP文 | 日韩欧美高清一区 | 国产高清在线a视频大全 | 亚洲av欧美在我 | 秋霞电影院兔费理论84MB | 色婷婷综合久久久久中文一区二区 | 拔萝卜在线高清观看视频 |