0

NHibernate를 사용하면 테이블의 컬럼을 객체의 컬렉션에 매핑 할 수 있습니다.NHibernate - 콜렉션을 복합 객체로 매핑 할 수 있습니까?

예를 들어

나는 다음과 같은 열이있는 아주 나쁘게 설계된 데이터베이스 테이블이있는 경우 : 된 ClientID CLIENTNAME First_AmountPaid Second_AmountPaid Third_AmountPaid Fourth_AmountPaid

그것을 통해 위치를 다음 클래스 구조에 First_AmountPaid를이지도하는 것이 가능 Fourth_AmountPaid에 자체 클래스 구현이 있습니까?

public class Client 
{ 
    public int ClientId { get; set; } 
    public string ClientName { get; set; } 

    public IList<AmountPaid> Amounts { get; set; } 
} 

public class AmountPaid 
{ 
    public decimal Amount { get; set; } 
} 

public class FirstAmountPaid : AmountPaid{ } 
public class SecondAmountPaid : AmountPaid{ } 
public class ThirdAmountPaid : AmountPaid{ } 
public class FourthAmountPaid : AmountPaid{ } 

따라서보다 의미있는 코드 구조를 제공하십시오.

내가 listposition 이미 양의 순서를 정의 할 때 서브 클래스가 왜 확실하지 않다

답변

0

주셔서 감사합니다

Map(x => x.Amounts) 
    .Columns.Add("First_AmountPaid", "Second_AmountPaid", "Third_AmountPaid", "Fourth_AmountPaid") 
    .CustomType<AmountPaidType>(); 

class AmountPaid : IUserType 
{ 
    public object Assemble(object cached, object owner) 
    { 
     return cached; 
    } 

    public object DeepCopy(object value) 
    { 
     return ((IList<AmountPaid>)x).Select(a => a.Clone()).ToList(); 
    } 

    public object Disassemble(object value) 
    { 
     return value; 
    } 

    bool IUserType.Equals(object x, object y) 
    { 
     // assuming AmountPaid implements Equals 
     return ((IList<AmountPaid>)x).SequenceEquals((IList<AmountPaid>)y); 
    } 

    public int GetHashCode(object x) 
    { 
     return x.GetHashCode(); 
    } 

    public bool IsMutable 
    { 
     get { return true; } 
    } 

    public void NullSafeSet(cmd, value, index) 
    { 
     var list = (IList<AmountPaid>)value; 
     NHibernateUtil.Double.NullSafeSet(cmd, list[0].Amount, index); 
     NHibernateUtil.Double.NullSafeSet(cmd, list[1].Amount, index + 1); 
     NHibernateUtil.Double.NullSafeSet(cmd, list[2].Amount, index + 2); 
     NHibernateUtil.Double.NullSafeSet(cmd, list[3].Amount, index + 3); 
    } 

    public object NullSafeGet(rs, names, owner) 
    { 
     var list = new List<AmountPaid>(); 
     foreach (var name in names) 
     { 
      list.Add(new AmountPaid((double)NHibernateUtil.Double.Get(rs, name))); 
     } 
     return list; 
    } 

    public object Replace(object original, object target, object owner) 
    { 
     return original; 
    } 

    public Type ReturnedType 
    { 
     get { return typeof(IList<AmountPaid>); } 
    } 

    public SqlType[] SqlTypes 
    { 
     get { return new[] { SqlTypeFactory.Double, SqlTypeFactory.Double, SqlTypeFactory.Double, SqlTypeFactory.Double }; } 
    } 
} 
관련 문제