|
Component語(yǔ)義
使用ConfORM“映射”組件,我們無需特別設(shè)置,ConfORM內(nèi)部會(huì)根據(jù)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實(shí)體有兩個(gè)組件:
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的映射字符串結(jié)果:
2.Mapping a class with double usage of same component
在上面Domain的基礎(chǔ)上新增一個(gè)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的映射字符串結(jié)果:
使用一個(gè)集合,而不是單一組件:
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的映射字符串結(jié)果:
實(shí)現(xiàn)實(shí)體與組件的雙向關(guān)聯(lián)關(guān)系:
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的映射字符串結(jié)果:
這篇文章展示ConfORM的Components語(yǔ)義應(yīng)用,映射了一些Domain示例。接下來繼續(xù)介紹ConfORM。Are you ConfORM?
參考資料
Fabio Maulo:ConfORM:“Mapping” Components
NET技術(shù):NHibernate3剖析:Mapping篇之ConfORM實(shí)戰(zhàn)(5):Component語(yǔ)義,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。