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

2010-10-03 21:46

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

by 李永京

at 2010-10-03 13:46:00

original http://www.cnblogs.com/lyj/archive/2010/10/03/inside-nh3-conform-component.html


作者: 李永京 发表于 2010-10-03 13:46 原文链接 阅读: 597 评论: 3

本节内容

系列引入

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

ConfORM概述

ConfORM实战系列:

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

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 Mapper(orm);
    //use the definition of table-to-class strategy class by class
    orm.TablePerClass<Person>();
    // Show the mapping to the console
    var mapping = mapper.CompileMappingFor(new[] { typeof(Person) });
    Console.Write(mapping.AsString());
}

一些Domain范例

我们使用各种集合定义Domain:

1.Mapping a class with components

Person实体有两个组件:

public class Person
{
    public int Id { get; set; }
    public Name Name { get; set; }
    public Address Address { get; set; }
}

public class Name
{
    public string First { get; set; }
    public string Last { get; set; }
}
public class Address
{
    public string Street { get; set; }
    public int CivicNumber { get; set; }
}

Mapping

输出HbmMapping的映射字符串结果:

ClassWithComponents

2.Mapping a class with double usage of same component

在上面Domain的基础上新增一个Name类型的属性:

public class Person
{
    public int Id { get; set; }
    public Name Name { get; set; }
    public Name ShowBusinessAlias { get; set; }
    public Address Address { get; set; }
}

Mapping

输出HbmMapping的映射字符串结果:

ClassWithDoubleSameComponents

3.Collection of components

使用一个集合,而不是单一组件:

public class Person
{
    private Iesi.Collections.Generic.ISet<Address> addresses;
    public Person()
    {
        addresses = new Iesi.Collections.Generic.HashedSet<Address>();
    }
    public int Id { get; set; }
    public Name Name { get; set; }
    public ICollection<Address> Addresses { get { return addresses; } }
}

Mapping

输出HbmMapping的映射字符串结果:

CollectionOfComponents

4.Bidirectional relation

实现实体与组件的双向关联关系:

public class Person
{
    public int Id { get; set; }
    public Name Name { get; set; }
    public Name ShowBusinessAlias { get; set; }
    public Address Address { get; set; }
}
public class Name
{
    public Person Person { get; set; }
    public string First { get; set; }
    public string Last { get; set; }
}

Mapping

输出HbmMapping的映射字符串结果:

BidirectionalRelation

结语

这篇文章展示ConfORM的Components语义应用,映射了一些Domain示例。接下来继续介绍ConfORM。Are you ConfORM?

参考资料

Fabio Maulo:ConfORM:“Mapping” Components

希望本文对你有所帮助。

评论: 3 查看评论 发表评论

程序员找工作,就在博客园


最新新闻:
· 传雅虎前高管戴维加盟Zynga负责移动业务(2010-10-05 07:03)
· Twitter联合创始人威廉姆斯辞去CEO一职(2010-10-05 07:01)
· 传Skype即将任命新CEO 或为其年底IPO铺路(2010-10-05 07:00)
· Gmail 开设官方 twitter 帐号(2010-10-05 06:58)
· Google TV 迷你网站发布,透露新平台细节[视频](2010-10-05 06:56)

编辑推荐:2010年10月编程语言排行榜:Java的混乱之治

网站导航:博客园首页  个人主页  新闻  闪存  小组  博问  社区  知识库