四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

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

  ConfORM概述

  如果你不熟悉ConfORM請(qǐng)查看前幾篇文章,你可以到http://code.google.com/p/codeconform/獲取ConfORM最新版本。

  在Domain設(shè)計(jì)中經(jīng)常使用集合,在.NET中的集合有四種:Iesi.Collections.Generic.ISet<T>、System.Collections.Generic.ICollection<T>、System.Collections.Generic.IList<T>、System.Collections.Generic.IDictionary<TKey,TValue>,NHibernate分別使用Set、Bag、List、Map映射來(lái)映射這些集合,有關(guān)這些集合映射基礎(chǔ)可以參考下面四篇文章,手動(dòng)編寫(xiě)簡(jiǎn)單的Mapping,從此杜絕啥啥啥生成工具(生成的東西太糟蹋了):

  這篇我們使用ConfORM“映射”多對(duì)多關(guān)聯(lián)。關(guān)聯(lián)關(guān)系有單向關(guān)聯(lián)和雙向關(guān)聯(lián)兩種,所以分為單向多對(duì)多關(guān)聯(lián)(Unidirectional many-to-many)、雙向多對(duì)多關(guān)聯(lián)(Bidirectional many-to-many)。

  Many-To-Many語(yǔ)義

  使用ConfORM“映射”多對(duì)多關(guān)聯(lián),無(wú)論是單向關(guān)聯(lián)還是雙向關(guān)聯(lián),我們只需要使用ObjectRelationalMapper類(lèi)中的ManyToMany方法即可(下面示例代碼的黑體)。ConfORM會(huì)根據(jù)Domain解析集合成員進(jìn)行配置。

[Test]
public void ManyToManyMappingDemo()
{
//show how work with many-to-many 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.ManyToMany<Person, Address>();
// Show the mapping to the console
var mapping = mapper.CompileMappingFor(entities);
Console.Write(mapping.AsString());
}

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

  我們使用各種集合定義Domain:

  1.Unidirectional using Set<T>

public class Person
{
private ISet<Address> addresses;
public Person()
{
addresses = new HashedSet<Address>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Address> Addresses
{
get { return addresses; }
}
}
public class Address
{
public Guid Id { get; set; }
public string Street { get; set; }
public int CivicNumber { get; set; }
}

  Mapping

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

  注意紅色塊是關(guān)鍵代碼,只是按其要求配置了必需標(biāo)簽,其中access是指訪問(wèn)策略,ConfORM根據(jù)Domain定義選擇了一個(gè)正確的訪問(wèn)策略。

UnidirectionalManyToManyMappingSet  2.Unidirectional using Bag<T>

public class Person
{
private ICollection<Address> addresses;
public Person()
{
addresses = new List<Address>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Address> Addresses
{
get { return addresses; }
}
}

  Mapping

  輸出HbmMapping的映射字符串結(jié)果:

UnidirectionalManyToManyMappingBag  3.Unidirectional using List<T>

public class Person
{
private IList<Address> addresses;
public Person()
{
addresses = new List<Address>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<Address> Addresses
{
get { return addresses; }
}
}

  Mapping

  輸出HbmMapping的映射字符串結(jié)果:

UnidirectionalManyToManyMappingList  4.Unidirectional using Map<TK,TV>

public class Person
{
private IDictionary<string, Address> addresses;
public Person()
{
addresses = new Dictionary<string, Address>();
}
public Guid Id { get; set; }
public string Name { get; set; }
public IDictionary<string, Address> Addresses
{
get { return addresses; }
}
}

  Mapping

  輸出HbmMapping的映射字符串結(jié)果:

UnidirectionalManyToManyMappingMap  雙向多對(duì)多關(guān)聯(lián)(Bidirectional many-to-many)

  Domain

public class User
{
public Guid Id { get; set; }
public string Name { get; set; }
public ISet<Role> Roles { get; set; }
}

public class Role
{
public Guid Id { get; set; }
public string Name { get; set; }
public ISet<User> Users { get; set; }
}

  ConfORM

public void DomainDefinition(ObjectRelationalMapper orm)
{
orm.TablePerClass(new[] { typeof(User), typeof(Role) });
orm.ManyToMany<User, Role>();
}

  Mapping

  輸出HbmMapping的映射字符串結(jié)果:

BidirectionalManyToManyMapping  結(jié)語(yǔ)

  這篇文章展示ConfORM的Many-To-Many語(yǔ)義應(yīng)用,映射了兩種Many-To-Many映射。大家同時(shí)也注意到了,上面列名的命名規(guī)則感覺(jué)有點(diǎn)別扭,這是ConfORM按照默認(rèn)的模式適配器配置了,我們可以增加自定義模式適配器或者使用通用定制化、特定定制化達(dá)到自定義的目的,這些內(nèi)容接下來(lái)的文章中介紹。

  參考資料

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

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

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

主站蜘蛛池模板: 包头电视台| 浙江卫视回放观看入口| 雪天使演员表介绍| 战狼15电影在线观看 | 珠帘玉幕上映时间| 孽债电视剧演员表| 玉匣记全文免费| 迷斯拉| 微信头像图片2024最新| 五月天丁香婷婷| 灌篮高手日语版免费观看| 周传雄黄昏歌词| 清水美里| 妈妈的朋电影| 北京卫视手机直播| 胡晶| 卡士酸奶尽量少吃| 美姐妹肉奴隶赤坂丽| 孕妇直播肚子疼揉肚子| 韩国一级免费| 减肥喝什么榨汁减肥快| 双生姐妹| 真的爱你最标准谐音歌词| 金敏喜个人简历| 皇冠小刀清痘视频| 吾栖之肤完整版在线观看| 赌侠 1990 刘德华| 中长发图片2024最新款女| 大红一师| 周传雄黄昏歌词| 少年智力开发报| 爱情面包房| 邓稼先教案设计一等奖优秀教案 | marie dee| 十大名茶排名顺序| 救急战队| 白雪公主和七个小矮人电影| 湖北特产| 江苏卫视节目预告| 永不瞑目演员表| k总直播间|