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

NHibernate3剖析:Mapping篇之ConfORM實戰(4):ManyToMany語義

  ConfORM概述

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

  在Domain設計中經常使用集合,在.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映射來映射這些集合,有關這些集合映射基礎可以參考下面四篇文章,手動編寫簡單的Mapping,從此杜絕啥啥啥生成工具(生成的東西太糟蹋了):

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

  Many-To-Many語義

  使用ConfORM“映射”多對多關聯,無論是單向關聯還是雙向關聯,我們只需要使用ObjectRelationalMapper類中的ManyToMany方法即可(下面示例代碼的黑體)。ConfORM會根據Domain解析集合成員進行配置。

[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());
}

  單向多對多關聯(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

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

  注意紅色塊是關鍵代碼,只是按其要求配置了必需標簽,其中access是指訪問策略,ConfORM根據Domain定義選擇了一個正確的訪問策略。

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的映射字符串結果:

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的映射字符串結果:

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的映射字符串結果:

UnidirectionalManyToManyMappingMap  雙向多對多關聯(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的映射字符串結果:

BidirectionalManyToManyMapping  結語

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

  參考資料

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

NET技術NHibernate3剖析:Mapping篇之ConfORM實戰(4):ManyToMany語義,轉載需保留來源!

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

主站蜘蛛池模板: 中文在线无码高潮潮喷在线 | 四房播播最新地址 | 51无码人妻精品1国产 | 无码一卡二卡三卡四卡 | FERRCHINA内入内射| 十九岁韩国电影在线观看 | 理论片午午伦夜理片I | 欧美人与禽ZOZO性伦交视频 | 亚洲薄码区 | 亚洲欧美综合乱码精品成人网 | 妻子+免费观看完整 | 7m凹凸国产刺激在线视频 | 亚洲黄色大片 | 久久99蜜桃精品麻豆 | 久久AV国产麻豆HD真实 | 蜜芽tv在线www | 古月娜下面好紧好爽 | 男人一生要读的书 | 国产精品人妻无码久久久奥特曼 | 国产精品久久欧美一区 | 日本午夜精品理论片A级APP发布 | 久久久久久人精品免费费看 | 麻豆国产精品AV色拍综合 | 青青久在线视频免费观看 | 狠狠人妻久久久久久综合九色 | 亚洲精品国偷拍自产在线观看蜜臀 | 4399的视频BD高清在线观看免费 | 一区三区三区不卡 | 小寡妇好紧进去了好大看视频 | 纲手胸被爆羞羞免费 | 无码人妻精品国产婷婷 | 学生妹被爆插到高潮无遮挡 | 免费观看亚洲视频 | 国产1000部成人免费视频 | 天天躁躁水汪汪人碰人 | 亚洲色图影院 | 亚洲精品网址 | 欧美6O老妪与小伙交 | 耻辱の奴隷淑女中文字幕 | 免费人妻AV无码专区五月 | 美女穿丝袜被狂躁动态图 |