NHibernate3剖析:Mapping篇之ConfORM實戰(5):Component語義

作者: 李永京  來源: 博客園  發布時間: 2010-10-09 17:57  閱讀: 993 次  推薦: 0   原文鏈接   [收藏]  

  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

0
0
 
標簽:NHibernate
 
 

文章列表

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()