使用 Fluent NHibernate 解析将两列映射到一个字段

发布于 2024-12-08 05:03:12 字数 512 浏览 3 评论 0原文

我有一个对象(Limit),其中包含两个类型为 ParsedValue 的字段(Low、High)。 ParsedValue 具有:

constructor (double, string);
method string ToString();

每个 ParsedValue 都有它的值:double 和单位:string 并表示带有单位的某些测量值。

我想要一个包含字段的表:

low:double
high:double
unit:string

因此,当选择 Limit 对象时,我需要用 new ParsedValue(low, unit) 填充 Low 字段,用 new ParsedValue(high, unit) 填充 High 字段。将 Limit 对象插入数据库时​​,应插入 low=Low.Value、high=High.Value、unit=Low.Unit

如何使用 Fluent NHibernate 做到这一点?

I have an object (Limit) that contains two fields (Low, High) with type ParsedValue.
ParsedValue has:

constructor (double, string);
method string ToString();

Each ParsedValue has it's value:double and unit:string and represents some measurement with a unit.

I want to have a table with fields:

low:double
high:double
unit:string

So when selecting Limit object I need to fill Low field with new ParsedValue(low, unit) and High field with new ParsedValue(high, unit). When inserting Limit object to the database, it should insert low=Low.Value, high=High.Value, unit=Low.Unit

How can I do that using Fluent NHibernate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

比忠 2024-12-15 05:03:12

扩展布鲁克的评论:

假设以下类

class LimitHolder
{
    public Limit Limit { get; set; }
}

class Limit
{
    public Limit(ParsedValue low, ParsedValue high)
    {
        Low = low;
        High = high;
    }

    public virtual ParsedValue Low { get; private set; }
    public virtual ParsedValue High { get; private set; }
}

class ParsedValue
{
    public ParsedValue(double value, string unit)
    {
        Value = value;
        Unit = unit;
    }
    public virtual double Value { get; private set; }
    public virtual string Unit { get; private set; }
}

映射

class LimitHolderMap : ClassMap<LimitHolder>
{
    public LimitHolderMap()
    {
        Map(lh => lh.Limit).CustomType<LimitUserType>();
    }
}

class LimitUserType : ImmutableUserType
{
    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var str = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if (string.IsNullOrEmpty(str))
            return null;
        else
        {
            var splitted = str.Split('|');
            return new Limit(
                new ParsedValue(double.Parse(splitted[0]), splitted[2]),
                new ParsedValue(double.Parse(splitted[1]), splitted[2]));
        }
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        var limit = value as Limit;
        if (limit == null)
            NHibernateUtil.String.NullSafeSet(cmd, null, index);
        else
        {
            var str = string.Concat(limit.Low.Value, '|', limit.High.Value, '|', limit.Low.Unit);
            NHibernateUtil.String.NullSafeSet(cmd, str, index);
        }
    }

    public Type ReturnedType
    {
        get { return typeof(Limit); }
    }

    public SqlType[] SqlTypes
    {
        get { return new [] { SqlTypeFactory.GetString(100) }; }
    }
}

ImmutableUserType 是我对不可变类型的 IUserType 的实现。如果需要的话我可以发布代码。

这会将限额保存在与其持有者相同的表中

extending Brook's Comment:

assuming following classes

class LimitHolder
{
    public Limit Limit { get; set; }
}

class Limit
{
    public Limit(ParsedValue low, ParsedValue high)
    {
        Low = low;
        High = high;
    }

    public virtual ParsedValue Low { get; private set; }
    public virtual ParsedValue High { get; private set; }
}

class ParsedValue
{
    public ParsedValue(double value, string unit)
    {
        Value = value;
        Unit = unit;
    }
    public virtual double Value { get; private set; }
    public virtual string Unit { get; private set; }
}

the mapping

class LimitHolderMap : ClassMap<LimitHolder>
{
    public LimitHolderMap()
    {
        Map(lh => lh.Limit).CustomType<LimitUserType>();
    }
}

class LimitUserType : ImmutableUserType
{
    public object NullSafeGet(IDataReader rs, string[] names, object owner)
    {
        var str = (string)NHibernateUtil.String.NullSafeGet(rs, names[0]);

        if (string.IsNullOrEmpty(str))
            return null;
        else
        {
            var splitted = str.Split('|');
            return new Limit(
                new ParsedValue(double.Parse(splitted[0]), splitted[2]),
                new ParsedValue(double.Parse(splitted[1]), splitted[2]));
        }
    }

    public void NullSafeSet(IDbCommand cmd, object value, int index)
    {
        var limit = value as Limit;
        if (limit == null)
            NHibernateUtil.String.NullSafeSet(cmd, null, index);
        else
        {
            var str = string.Concat(limit.Low.Value, '|', limit.High.Value, '|', limit.Low.Unit);
            NHibernateUtil.String.NullSafeSet(cmd, str, index);
        }
    }

    public Type ReturnedType
    {
        get { return typeof(Limit); }
    }

    public SqlType[] SqlTypes
    {
        get { return new [] { SqlTypeFactory.GetString(100) }; }
    }
}

ImmutableUserType is my implementation of IUserType for immutable types. I can post the code if needed.

this would save the Limit in the same Table as the holder of it

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文