中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

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

  ConfORM概述

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

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

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

  One-to-One語(yǔ)義

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

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

  1.Domain

  設(shè)計(jì)單向一對(duì)一關(guān)聯(lián)Domain實(shí)例,Person對(duì)象和Address對(duì)象,人有一個(gè)地址。

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對(duì)象。(注意黑體)

[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

  上面測(cè)試輸出HbmMapping的映射字符串,如果你使用ReSharper或者TestDriven.NET工具測(cè)試,你可以看見下面輸出:

UnidirectionalO<a href=/itjie/NETjishu/ target=_blank class=infotextkey>NET</a>oOneMapping  4.原理

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

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

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

  1.Domain

  設(shè)計(jì)雙向一對(duì)一關(guān)聯(lián)Domain實(shí)例,Person對(duì)象和Address對(duì)象,人有一個(gè)地址,地址有一個(gè)人。

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對(duì)象。其實(shí)這個(gè)代碼和上面的一樣:

[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

  測(cè)試生成字符串:

BidirectionalO<a href=/itjie/NETjishu/ target=_blank class=infotextkey>NET</a>oOneMapping  4.原理

  對(duì)于雙向一對(duì)一關(guān)聯(lián),實(shí)際就是設(shè)置IONEToOneMapper,ConfORM會(huì)在IPatternsAppliersHolder的ONEToOne和ONEToOnePath集合中匹配對(duì)應(yīng)模式適配器,即匹配到以下三個(gè)模式適配器,進(jìn)行相應(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)

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

  Domain與雙向一對(duì)一主鍵關(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

  測(cè)試生成字符串:

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é)語(yǔ)

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

  參考資料

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

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

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

主站蜘蛛池模板: 国内91在线 | 国产精品久久久久久久久久久久冷 | 日韩欧美在线观看 | 一区二区亚洲 | 欧美5区| 日本特黄a级高清免费大片 成年人黄色小视频 | 91久久综合| 一区二区三区免费 | 中文字幕一区二区三 | 国产日韩欧美综合 | 亚洲综合一区二区三区 | 久青草影院 | 午夜天堂| 午夜欧美 | 91传媒在线观看 | 成人精品国产免费网站 | 蜜月aⅴ国产精品 | 中文字幕综合在线 | 欧美一区二区三区在线观看视频 | 久久久久国产精品 | 国产在线播 | 亚洲国产一区二区三区在线观看 | 99精品欧美一区二区三区 | 婷婷五月色综合香五月 | 久久毛片 | 最新中文在线视频 | 中文字幕 亚洲一区 | 欧美亚洲国产日韩 | 国产精品福利网站 | 国产精品久久久久久久粉嫩 | 韩三级在线观看 | 一区二区在线不卡 | 欧美群妇大交群中文字幕 | caoporn国产| 日本黄色短片 | 人人人人干| 欧洲精品久久久久毛片完整版 | 欧美极品在线视频 | 九色国产 | 中文字幕一区在线观看视频 | 免费黄色av |