2013-07-19 2 views
0

.Net HashSet을 랩핑하고 ICollection을 구현하는 커스텀 컬렉션이 있습니다. 나는 NHib이 ICollection 인터페이스를 사용할 수있는 방법으로 .net HashSet을 사용할 수 있기를 희망하면서 이것을 수행했다. Ihsi와는 반대로 NHib가 내부적으로 사용한다.NHibernate 커스텀 컬렉션이 수화되지 않습니다

다음은 DB에 저장이 잘 동작하는 것,하지만 난 얻을 예외는 수분은 제가 더 많은 일을 할 필요가 알 수 있습니다 때

Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericSet`1[ContactMechanisms.Domain.Emails.Email]' to type 'ContactMechanisms.Domain.Emails.EmailCollection'. 

These articles 자주 사용자 지정 컬렉션 처리를 처리 할 수있는 방법으로 인용된다 하지만 링크가 끊어져서 내가 볼 수있는 것은 확장을 사용하여 컬렉션을 쿼리하는 것입니다.

IUserCollectionType을 사용해야합니까? 그렇다면 누구나 샘플 구현을 보여주는 링크가 있습니까? 현재 코드/매핑에서 바보 같은 무언가가 있습니까?

좋은 해결책이 있습니까?

CODE (부모 엔티티 단편)

public class Contact : RoleEntity<Party>, IHaveContactMechanisms 
{  
    private readonly ICollection<Email> _emails = new EmailCollection(); 

    public virtual EmailCollection Emails { get { return (EmailCollection) _emails; } } 
} 

CODE (커스텀 컬렉션 단편)

public class EmailCollection : ContactMechanismSet<Email> { .... } 

public class ContactMechanismSet<T> : ValueObject, ICollection<T> where T : ContactMechanism 
{ 
    private readonly HashSet<T> _set = new HashSet<T>(); 
} 

MAPPING (HBM 값 유형 컬렉션)

<set name ="_emails" table="Emails" access="field"> 
    <key column="ContactId"></key> 
    <composite-element class="ContactMechanisms.Domain.Emails.Email, ContactMechanisms.Domain"> 
    <property name="IsPrimary" /> 
    <property name="Address" length="100"/> 
    <property name="DisplayName" length="50"/> 
    </composite-element> 
</set> 

* UPDATE *

그래서 내 개체 setter 작품 아래에 뭐하는거야,하지만 - 내가 더 잘할 수 있습니까 ?? EmailCollection이 매개 변수가없는 생성자 그 다음은 새로운 NH의 OOTB와 수 및 .NET ISET을 처리 CollectionTypeFactory 등록 노인해야한다있는 경우

public virtual EmailCollection Emails { 
    get { 
     if (!(_emails is EmailCollection)) { 
      // NHibernate is giving us an enumerable collection 
      // of emails but doesn't know how to turn that into 
      // the custom collection we really want 
      // 
      _emails = new EmailCollection (_emails); 
     } 
     return (EmailCollection) _emails ; 
    } 
} 
+0

은 할 수 그 것이다 (인터넷에서 찾을 수 있습니다) 'HashSet '대신에'ISet '을 사용하여 EmailCollection을 구현 하시겠습니까? – Firo

답변

2

public class Contact : RoleEntity<Party>, IHaveContactMechanisms 
{  
    private EmailCollection _emails = new EmailCollection(); 

    public virtual EmailCollection Emails { get { return _emails; } } 
} 

public class ContactMechanismSet<T> : ValueObject, ICollection<T> where T : ContactMechanism 
{ 
    ctor() 
    { 
     InternalSet = new HashSet<T>(); 
    } 

    private ISet<T> InternalSet { get; set; } 
} 

<component name="_emails"> 
    <set name="InternalSet" table="Emails"> 
    <key column="ContactId"></key> 
    <composite-element class="ContactMechanisms.Domain.Emails.Email, ContactMechanisms.Domain"> 
     <property name="IsPrimary" /> 
     <property name="Address" length="100"/> 
     <property name="DisplayName" length="50"/> 
    </composite-element> 
    </set> 
</component>