nHibernate与SQLite格式的日期 - c#

我在SQLite数据库中使用nHibernate + FluentNHibernate。
所有日期均以文本格式“ YYYY-MM-DD HH:MM:SS”存储

如何指示nHibernate(或sqlite驱动程序???)截断时间部分并以“ YYYY-MM-DD”格式存储某些字段的日期(并存储其他字段的完整日期)?

或者如何指示将所有日期存储为整数?

我担心空间使用情况,因为日期在数据库中占用大量空间,并且日期中也有索引,因此我需要使它们占用的空间尽可能少。

参考方案

如果手动将DateTime转换为String,则用YYYY-MM-DD替换字符串格式不会出现任何问题。

如果让NHibernate执行此转换,则可以像下面这样创建自定义NHibernate UserType

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

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

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

    public bool Equals(object x, object y)
    {
        if (ReferenceEquals(x, y))
            return true;

        if (x == null && y == null)
            return false;

        return x.Equals(y);
    }

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

    public bool IsMutable
    {
        get { return false; }
    }

    public object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner)
    {
        var serialized = NHibernateUtil.String.NullSafeGet(rs, names[0]) as string;

        if (string.IsNullOrEmpty(serialized))
            return null;

        return Deserialize(serialized);
    }

    protected abstract DateTime Deserialize(string value);
    protected abstract string Serialize(DateTime value);

    public void NullSafeSet(System.Data.IDbCommand cmd, object value, int index)
    {
        if (value == null)
            NHibernateUtil.String.NullSafeSet(cmd, DBNull.Value, index);
        else
            NHibernateUtil.String.NullSafeSet(cmd, Serialize((DateTime)value), index);
    }

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

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

    public NHibernate.SqlTypes.SqlType[] SqlTypes
    {
        get { return new[] { NHibernateUtil.String.SqlType }; }
    }
}

public class TruncatedDateTimeAsStringType : DateTimeAsStringType
{
    private const string Format = "yyyy-MM-dd";

    protected override string Serialize(DateTime value)
    {
        return value.ToString(Format, CultureInfo.InvariantCulture);
    }

    protected override DateTime Deserialize(string value)
    {
        return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
}

public class FullDateTimeAsStringType : DateTimeAsStringType
{
    private const string Format = "yyyy-MM-dd hh:mm:ss";

    protected override string Serialize(DateTime value)
    {
        return value.ToString(Format, CultureInfo.InvariantCulture);
    }

    protected override DateTime Deserialize(string value)
    {
        return DateTime.ParseExact(value, Format, CultureInfo.InvariantCulture, DateTimeStyles.None);
    }
}

并使用DateTimeTruncatedDateTimeAsStringType映射FullDateTimeAsStringType属性:

<property name="TruncatedDateTime" type="Your.Namespance.TruncatedDateTimeAsStringType, Your.Assembly" />
<property name="NotTruncatedDateTime" type="Your.Namespance.FullDateTimeAsStringType, Your.Assembly" />

将对象转换为List <object> - c#

我看过类似的问题,但没有什么合适的。我有一个碰巧包含列表的对象。我想把它变成我可以列举的东西。例如:object listObject; // contains a List<Something> List<object> list; list = listObject as List<object>; // list c…

使用数组时出现NullPointerException - java

我正在尝试为在线Java课程创建程序。该程序包括Employee类和Name类。我必须创建多个Employee对象,并提示用户输入员工的姓名。我将所有Employee对象存储在一个employee数组中。这是代码: //Creates employee array with the number of array elements being however…

LeetCode题解计算机为什么是基于二进制的?

可以是三进制么?二进制有什么好处?题解:为什么叫电子计算机?算盘应该没有二进制

LeetCode题解统计城市的所有灯泡

这个是我刚毕业的时候,一个真实的面试题,这是一个开放题。题目描述:想办法,将一个城市的所有灯泡数量统计出来。题解:费米估算法1、如果某个城市常驻人口有1000万2、假设每5人居住在一套房里,每套房有灯泡5只,那么住宅灯泡共有1000万只3、假设公众场所每10人共享一只灯泡,那么共有100万只4、主要的这两者相加就得出了1100万只当然实际上这是估算的,具体应…

LeetCode题解黑白圆盘

一个圆盘被涂上了黑白二色,两种颜色各占一个半圆。圆盘以一个未知的速度、按一个未知的方向旋转。你有一种特殊的相机可以让你即时观察到圆上的一个点的颜色。你需要多少个相机才能确定圆盘旋转的方向?题解:可以用一个相机即可