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 OOPvar 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 classorm.TablePerClass(entities);// Defining relationsorm.ManyToMany<Person, Address>();// Show the mapping to the consolevar 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定义选择了一个正确的访问策略。
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