0

TimeSheetActivity 클래스에는 할당 모음이 있습니다. 나는 또한 필요이 모델을 매핑하는 방법

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

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

:

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 함께, 예 : 할당은 값 객체 같은 (불변)보고 뭔가 StaffMember가 할당을 유지하고 가져올 수 있어야합니다.

table Allocations 
    ActivityID 
    StaffMemberID 
    EventDate 
    TimeQuantity 

나는 문제가 매핑을 시각화하는 데 문제가 있습니다 :이 데이터 중심의 디자인이라면, 나의 개념은 활동 및 StaffMembers 사이의 다 대다 관계를 해결하는 것 인 할당 테이블이있을 것입니다. TimeSheetActivity의 할당을 시작하려고하는데 DDL을 생성하려고하면 아래 오류가 발생합니다.

NHibernate.MappingException : (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'

내가 오류를 받고 있어요 만약 내가 말할 수 있기 때문에 (FNH)에

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

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 an Entity를 만들었는데, 이는 내 프레임 워크에서 연관 테이블의 대리 키로 편리하게 사용할 수있는 ID로 마무리 될 것임을 의미합니다. Allocation이 실제로 내 도메인 모델의 엔티티인지 여부에 대한 질문을 제기합니다. 그렇지 않으면 작동하도록 값 유형 모음을 모으는 것입니다 (그렇다면 대용 할 수있는 더 깨끗한 방법이 있습니다). 키 또는 복합 키 또는 ..)를 사용해야합니다. 어쨌든, 팁을 주셔서 감사하고 Ayende 게시에 대한 감사의 글을 읽어야합니다! 건배 – Berryl

관련 문제