C# 如何映射组件集合(值对象)

C# 如何映射组件集合(值对象),c#,.net,nhibernate,C#,.net,Nhibernate,我有文章和图片 形象是价值对象,物品是实体。图像映射为类似组件的组件 public class ImageMap { public static Action<IComponentMapper<Image>> Mapping() { return c => { c.Property(p => p.AltText);

我有文章和图片

形象是价值对象,物品是实体。图像映射为类似组件的组件

public class ImageMap 
    {
        public static Action<IComponentMapper<Image>> Mapping()
        {
            return c =>
                {
                    c.Property(p => p.AltText);
                    c.Property(p => p.Name, m => 
                    {
                        m.Length(255);
                    });
                    c.Property(p => p.Path, m =>
                    {
                        m.Length(255);
                    });
                    c.Property(p => p.Height);
                    c.Property(p => p.Width);
                    c.Parent(x => x.Article, p => p.Access(Accessor.ReadOnly));                    
                };
        }
    }
如何映射组件(图像)的集合

Article.cs
公共虚拟IList映像{get;set;}
我们需要的是。虽然原理与的情况几乎相同,但本例中的元素名称是。事实上,按代码映射更容易混淆,我们需要
IComponentElementMapper

这就是我们的映射方法

public class ImageMap 
{
    // the <composite-element> as IComponentElement<>
    // will replace the above mapper

    // public static Action<IComponentMapper<Image>> Mapping()
    public static Action<IComponentElementMapper<Image>> Mapping()
    {
        return c =>
            {
                c.Property(p => p.AltText);
                c.Property(p => p.Name, m => 
                {
                    m.Length(255);
                });
                c.Property(p => p.Path, m =>
                {
                    m.Length(255);
                });
                c.Property(p => p.Height);
                c.Property(p => p.Width);
                c.Parent(x => x.Article, p => p.Access(Accessor.ReadOnly));                    
            };
    }
}
公共类ImageMap
{
//as i组件元素
//将替换上述映射器
//公共静态动作映射()
公共静态动作映射()
{
返回c=>
{
c、 属性(p=>p.AltText);
c、 属性(p=>p.Name,m=>
{
m、 长度(255);
});
c、 属性(p=>p.Path,m=>
{
m、 长度(255);
});
c、 属性(p=>p.Height);
c、 属性(p=>p.Width);
c、 父(x=>x.Article,p=>p.Access(Accessor.ReadOnly));
};
}
}
现在我们将把它传递到映射中,正如Adam Bar在这里记录的那样

// mapping for property:
// public virtual IList<Image> Images {get; set;}

Bag(x => x.Images // reference
       , b => { } // bag properties mapper
       , v => v.Component(ImageMap.Mapping()) // here we pass the <composite-element>
   ); 
//属性的映射:
//公共虚拟IList映像{get;set;}
行李(x=>x.图像//参考
,b=>{}//包属性映射器
,v=>v.Component(ImageMap.Mapping())//这里我们传递
); 
xml结果将如预期的那样:

<bag name="Images" ... >
  <key column="ArticleId" ... />
  <composite-element class="Occupation">
    <parent name="Article" access="readonly" />
    <property name="AltText" />
    <property name="Name" length="255" />
    ...
  </composite-element>
</bag>

...
// mapping for property:
// public virtual IList<Image> Images {get; set;}

Bag(x => x.Images // reference
       , b => { } // bag properties mapper
       , v => v.Component(ImageMap.Mapping()) // here we pass the <composite-element>
   ); 
<bag name="Images" ... >
  <key column="ArticleId" ... />
  <composite-element class="Occupation">
    <parent name="Article" access="readonly" />
    <property name="AltText" />
    <property name="Name" length="255" />
    ...
  </composite-element>
</bag>