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,从此杜绝啥啥啥生成工具(生成的东西太糟蹋了):

NHibernate3剖析:Mapping篇之集合映射基础(1):Set映射 NHibernate3剖析:Mapping篇之集合映射基础(2):Bag映射 NHibernate3剖析:Mapping篇之集合映射基础(3):List映射 NHibernate3剖析:Mapping篇之集合映射基础(4):Map映射

  这篇我们使用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定义选择了一个正确的访问策略。

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

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

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

  双向多对多关联(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的映射字符串结果:

  结语

  这篇文章展示ConfORM的Many-To-Many语义应用,映射了两种Many-To-Many映射。大家同时也注意到了,上面列名的命名规则感觉有点别扭,这是ConfORM按照默认的模式适配器配置了,我们可以增加自定义模式适配器或者使用通用定制化、特定定制化达到自定义的目的,这些内容接下来的文章中介绍。

  参考资料

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

时间: 2024-09-20 05:52:34

NHibernate3剖析:Mapping篇之ConfORM实战(4):ManyToMany语义的相关文章

一起谈.NET技术,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.C

一起谈.NET技术,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设计中,关联关系有单

NHibernate3剖析:Mapping篇之ConfORM实战(5):Component语义

Component语义 使用ConfORM"映射"组件,我们无需特别设置,ConfORM内部会根据Domain定义来判定组件,一般而言,没有主键的类就是组件. [Test] public void ComponentMappingDemo() { //show how work with components and how ConfORM understands OOP var orm = new ObjectRelationalMapper(); var mapper = new

一起谈.NET技术,NHibernate3剖析:Mapping篇之ConfORM实战(5):Component语义

Component语义 使用ConfORM"映射"组件,我们无需特别设置,ConfORM内部会根据Domain定义来判定组件,一般而言,没有主键的类就是组件. [Test]public void ComponentMappingDemo(){//show how work with components and how ConfORM understands OOPvar orm = new ObjectRelationalMapper();var mapper = new Mappe

NHibernate3剖析:Mapping篇之ConfORM实战(1):概览

ORuM思想浮出 对于ORM(Object Relational Mapping)我们太熟悉了,但是我们从另一个角度可以想象出ORuM(Object Relational un-Mapping)的思想理念.我们在程序中仅仅定义Domain,而想有个工具可以帮助我们"自动化"实现Mapping,我们无需按传统的ORM思想那样为Domain手动编码Mapping. ORuM对于使用者来说更像ORAM(Object-Relational Auto-Mapping)或者ORIM(Object-

一起谈.NET技术,NHibernate3剖析:Mapping篇之ConfORM实战(2):原理

ConfORM概述 在上一节中,我用一个简单的例子描述了ConfORM简单使用.留下了很多疑问,大家不解为何使用ConfORM以及怎么使用ConfORM,其内部原理是什么.这节,我们先注重了解一些ConfORM的原理. 你可以到http://code.google.com/p/codeconform/ 获取ConfORM ConfORM重要接口 ConfORM的核心就是实例化一个ObjectRelationalMapper对象和Mapper对象,配置Domain对象,调用Mapper对象的Com

一起谈.NET技术,NHibernate3剖析:Mapping篇之ConfORM实战(1):概览

ORuM思想浮出 对于ORM(Object Relational Mapping)我们太熟悉了,但是我们从另一个角度可以想象出ORuM(Object Relational un-Mapping)的思想理念.我们在程序中仅仅定义Domain, 而想有个工具可以帮助我们"自动化"实现Mapping,我们无需按传统的ORM思想那样为Domain手动编码Mapping. ORuM对于使用者来说更像ORAM(Object-Relational Auto-Mapping)或者ORIM(Object

linux0.11内核源码剖析:第一篇 内存管理、memory.c【转】

转自:http://www.cnblogs.com/v-July-v/archive/2011/01/06/1983695.html linux0.11内核源码剖析第一篇:memory.c   July  二零一一年一月六日 ----------------------------------------- 博主声明:1.本系列非linux系统教程,仅仅是针对linux0.11内核源码,所做的剖析,注释.2.本系列参考:深入理解linux内核.linux内核完全注释,linux内核源代码情景分析

Duwamish深入剖析-配置篇

Duwamish深入剖析-配置篇 摘要: 本文详细介绍了Duwamish网上电子书店的Web.config配置文件的结构处理方式以及用途,阐述了配置文件的各功能模块中的作用. 目录: 引言 配置节处理程序声明 自定义配置节 配置节处理程序 总结 参考资料 作者 引言: 几乎在每本介绍Asp.Net编程的书里,在谈到如何管理数据库连接字符串的时候,都是采用将数据库连接字符串以如下形式放在Web.Config文件中: ppSettings> dd key="ConnectionString&q