どのようにNHibernateは(流暢)と、データベース内の2つのvarchar列にDateTimeプロパティをマッピングしていますか?

StackOverflow https://stackoverflow.com/questions/2526912

質問

(:mm:ssの、それぞれフォーマットさYYYYMMDDとHH)テーブルの一部では

私は、日付と時刻のフィールドcharとして(8)列があり、従来のデータベースを扱っています。どのように私は、単一の.NETのDateTimeプロパティに2つの文字列をマッピングすることができますか?私は以下のことを試してみましたが、DateTimeの日付とTIMEOFDAYプロパティは読み取り専用ですので、私はもちろん、「できないアクセスセッター」のエラーが表示されます。

public class SweetPocoMannaFromHeaven
{    
    public virtual DateTime? FileCreationDateTime { get; set; }
}

mapping.Component<DateTime?>(x => x.FileCreationDateTime,
            dt =>
            {
                dt.Map(x => x.Value.Date,
                    "file_creation_date");
                dt.Map(x => x.Value.TimeOfDay,
                    "file_creation_time");
            });

私はまた、DateTimeのためIUserTypeを定義しようとしているが、私はそれを把握することはできません。私は答えをグーグルのトンをやったが、私はまだそれを把握することはできません。この<ストライキ>愚かレガシーデータベース規則を処理するための私の最高のオプションは何ですか?これらのより曖昧なシナリオのいくつかのドキュメントのために多くのアウトはありませんので、コード例が参考になる。

役に立ちましたか?

解決

あなたはICompositeUserTypeが複数の列を処理する必要があります。あなたはなど、フォーマットを解析し、エラーチェックをアップ牛肉する必要がありますが、ここではあなたのための開始点です。

HTH、
Berryl

public class LegacyDateUserType : ICompositeUserType
{

    public new bool Equals(object x, object y)
    {
        if (x == null || y == null) return false;
        return ReferenceEquals(x, y) || x.Equals(y);
    }

    public int GetHashCode(object x) {
        return x == null ? typeof (DateTime).GetHashCode() + 473 : x.GetHashCode();
    }

    public object NullSafeGet(IDataReader dr, string[] names, ISessionImplementor session, object owner)
    {
        if (dr == null) return null;

        var datePortion = NHibernateUtil.String.NullSafeGet(dr, names[0], session, owner) as string;
        var timePortion = NHibernateUtil.String.NullSafeGet(dr, names[1], session, owner) as string;

        var date = DateTime.Parse(datePortion);
        var time = DateTime.Parse(timePortion);
        return date.AddTicks(time.Ticks);
    }

    ///<summary>
    /// Write an instance of the mapped class to a prepared statement. Implementors 
    /// should handle possibility of null values. A multi-column type should be written 
    /// to parameters starting from index.
    ///</summary>
    public void NullSafeSet(IDbCommand cmd, object value, int index, ISessionImplementor session) {
        if (value == null) {
            // whatever
        }
        else {
            var date = (DateTime) value;
            var datePortion = date.ToString("your date format");
            NHibernateUtil.String.NullSafeSet(cmd, datePortion, index, session);
            var timePortion = date.ToString("your time format");
            NHibernateUtil.String.NullSafeSet(cmd, timePortion, index + 1, session);
        }
    }

    public object GetPropertyValue(object component, int property)
    {
        var date = (DateTime)component;
        return property == 0 ? date.ToString("your date format") : date.ToString("your time format");
    }

    public void SetPropertyValue(object component, int property, object value)
    {
        throw new NotSupportedException("DateTime is an immutable object.");
    }

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

    public object Disassemble(object value, ISessionImplementor session) { return value; }

    public object Assemble(object cached, ISessionImplementor session, object owner) { return cached; }

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

    ///<summary>Get the "property names" that may be used in a query.</summary>
    public string[] PropertyNames { get { return new[] { "DATE_PORTION", "TIME_PORTION" }; } }

    ///<summary>Get the corresponding "property types"</summary>
    public IType[] PropertyTypes { get { return new IType[] { NHibernateUtil.String, NHibernateUtil.String }; } }

    ///<summary>The class returned by NullSafeGet().</summary>
    public Type ReturnedClass { get { return typeof(DateTime); } }

    ///<summary>Are objects of this type mutable?</summary>
    public bool IsMutable { get { return false; } }

}

===流暢マッピング(自動マッピングW /オーバーライドクラスを想定)====

 public void Override(AutoMapping<MyClass> m)
 {
     ....
     m.Map(x => x.MyDateTime).CustomType<LegacyDateUserType>();
 }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top