代码改变世界

NHibernate3剖析:Mapping篇之ConfORM实战(4):ManyToMany语义

2010-09-26 21:03 by 李永京, ... 阅读, ... 评论, 收藏, 编辑

本节内容

系列引入

NHibernate3剖析系列分别从Configuration篇、Mapping篇、Session篇、Core篇、Tool篇、Practice篇、Extension篇等方面全面揭示NHibernate3版本内容、特性及其应用,完全基于NHibernte3版本。

ConfORM概述

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

希望本文对你有所帮助。