0

TimeSheetActivity类具有一个Allocations集合。分配是一个值对象(不可变的)看起来像这样:如何映射此模型

public class Allocation : ValueObject { 
    public virtual StaffMember StaffMember { get; private set; } 
    public virtual TimeSheetActivity Activity { get; private set; } 
    public virtual DateTime EventDate { get ... } 
    public virtual TimeQuantity TimeSpent { get ... } 
} 

域内使用此关系最方便的方法是使用一个IDictionary,即:

public virtual IEnumerable<Allocation> Allocations { 
get { return _allocations.Values; } } 

private readonly IDictionary<DateTime, Allocation> _allocations = new SortedList<DateTime, Allocation>(); 

我还需要能够坚持并通过职员获取分配。如果这是一个数据驱动的设计,我的理念是,将有一个分配表,将解决活动和StaffMembers之间的许多一对多的关系:

table Allocations 
    ActivityID 
    StaffMemberID 
    EventDate 
    TimeQuantity 

我无法可视化映射。我试图从TimeSheetActivity的Allocations开始,但是当我尝试生成DDL时,我收到以下错误:

NHibernate.MappingException:(XmlDocument)(3,6):XML验证错误:元素'class'在命名空间'urn:nhibernate-mapping-2.2'中的命名空间'urn:nhibernate-mapping-2.2'中有无效的子元素'property'。在名字空间'urn:nhibernate-mapping-2.2'中,预期可能的元素列表:'meta,subselect,cache,synchronize,comment,tuplizer,id,composite-id'。

我不能说,如果我得到的错误,因为:

A). I haven't yet mapped StaffMember to a collection of Allocations. 
B). The mapping I have between Activity and Allocations is wrong. 
C). All of the above 
D). Other _____________________________________ 

AllocationMapping(FNH中)

public class AllocationMap : IAutoMappingOverride<Allocation> 
{ 
    public void Override(AutoMap<Allocation> mapping) 
    { 

     // these are both derived from the time period, so ignore 
     mapping.IgnoreProperty(x => x.TimeSpent); 
     mapping.IgnoreProperty(x => x.EventDate); 

     // TimePeriodUserType knows how to persist the time period start and end dates as DateTimes 
     mapping.Map(x => x.TimeSpentPeriod) 
      .ColumnNames.Add("PeriodStart", "PeriodEnd") 
      .SetAttribute("type", typeof(TimePeriodUserType).AssemblyQualifiedName); 

     mapping.References(x => x.Activity).WithForeignKey().FetchType.Join().Not.Nullable(); 
     mapping.References(x => x.StaffMember).WithForeignKey().FetchType.Join().Not.Nullable(); 
    } 
} 

分配映射(产生HBM)

.... 
<property name="TimeSpentPeriod" type="..., ..., ..."> 
    <column name="PeriodStart" /> 
    <column name="PeriodEnd" /> 
</property> 
<many-to-one foreign-key="FK_AllocationToStaffMember" fetch="join" not-null="true" name="StaffMember" column="StaffMemberID" /> 
<many-to-one foreign-key="FK_AllocationToActivity" fetch="join" not-null="true" name="Activity" column="TimeSheetActivityID" /> 
.... 

活动图(FNH)

public class TimeSheetActivityMap : IAutoMappingOverride<TimeSheetActivity> 
{ 
    public void Override(AutoMap<TimeSheetActivity> mapping) 
    { 
     // this can be replaced by some id given by NHib via an inheritance type mapping?? 
     mapping.IgnoreProperty(x => x.IdScheme); 

     // this is derived 
     mapping.IgnoreProperty(x => x.TotalTime); 

     // this is for client consumption only 
     mapping.IgnoreProperty(x => x.Allocations); 

     mapping.HasMany(x => x.Allocations) 
      .AsMap(x => x.EventDate) 
      .Cascade.All() 
      .Access.AsCamelCaseField(Prefix.Underscore) 
      .WithForeignKeyConstraintName("FK_ActivityAllocation"); 
    } 
} 

ActivityMapping(产生HBM)

<set name="Allocations" cascade="all" access="field.camelcase-underscore"> 
<key foreign-key="FK_ActivityAllocation" column="TimeSheetActivityID" /> 
<one-to-many class="Smack.ConstructionAdmin.Domain.Model.TimeSheet.Allocation, ..." /> 
</set> 

这是我的与NHibernate经验水平一个复杂的映射,所以我会很感激的映射技术的任何帮助,问题或批评,造型理念,或者我的方法来解决这个问题。

干杯, Berryl

回答

2

我不是FHN专家,但我看你的分配属性为一组产生。你应该得到一个地图元素是这样的:

<map name="Allocations" lazy="true" table="Allocations"> 
<key column="FK_ActivityAllocation"/> 
<index column="EVENT_DATE" type="DateTime"/> 
<composite-element class="Allocation"> 
... 
</composite> 
</map> 

你有一些先进的例子在这里: http://ayende.com/Blog/archive/2009/06/03/nhibernate-mapping-ndash-ltmapgt.aspx

+0

它肯定应该和现在一样,我改变了我的模型;我将Allocation作为一个实体,在我的框架中,它意味着它将结束一个Id,它将方便地用作关联表的代理键。这引发了关于Allocation是否在我的域模型中一直是实体的问题,或者我是否耗尽了一系列值类型以使其工作(如果是的话,是否有更清晰的方法来获取代理键或应该使用组合键还是..)。无论如何,感谢您的提示,并感谢Ayende的发布,我需要阅读!干杯 – Berryl 2009-07-08 23:13:13