【问题标题】:SubSonic SimpleRepository storing member classSubSonic SimpleRepository 存储成员类
【发布时间】:2010-12-08 04:35:42
【问题描述】:

我是 C# 和 Subsonic 的新手。我正在尝试解决以下情况:

public class UnknownInt { 
  public int val;
  public bool known;
}

public class Record {
  public int ID;
  public UnknownInt data;
}

我正在使用 SimpleRepository。 有没有办法在将 UnknownInt 存储在 SQL 数据库中之前对其进行序列化(可能是 XML 文本字段?)

我正在尝试构建一个问卷系统,其中用户可以提供“整数”答案、“未知”答案以及空答案(尚未回答的问题)

换句话说 - 我的 UnknownInt 类需要实现哪些接口才能符合条件并转换为 SubSonic 3.0 简单存储库?

干杯!

【问题讨论】:

    标签: c# sql serialization subsonic subsonic-simplerepository


    【解决方案1】:

    我会这样做:

    public class Record
    {
        public int ID {get;set;}
    
        [EditorBrowsable(EditorBrowsableState.Never)]
        public int UnknownIntValue {get;set;}
    
        [EditorBrowsable(EditorBrowsableState.Never)]
        public bool UnknownIntKnown {get;set;}
    
        [SubSonicIgnore]
        public UnknownInt UnknownInt
        {
            get
            {
                return new UnknownInt()
                {
                    val = UnknownIntValue,
                    known = this.UnknownIntKnown
                };
            }
            set
            {
                 this.UnknownIntValue = value.val;
                 this.UnknownIntKnown = value.known;
            }
        }
    
    }
    
    public struct UnknownInt
    { 
        public readonly int Val;
        public readonly bool Known;
    
        public UnknownInt(int val, bool known) 
        {
            this.Val = val;
            this.Known = known;
        }
    
        public override string ToString()
        {
            return String.Format("{0} ({1})",
               Val, Known == true ? "known" : "unknown");
        }
        public override bool Equals(Object obj) 
        {
           return obj is UnknownInt && this == (UnknownInt)obj;
        }
        public static bool operator ==(UnknownInt x, UnknownInt y) 
        {
           return x.Val == y.Val && x.Known == y.Known;
        }
        public static bool operator !=(UnknownInt x, UnknownInt y) 
        {
           return !(x == y);
        }
    
    }
    

    基本思想是必须存储您的用户定义类型但对智能感知隐藏的列(System.ComponentModel.EditorBrowsable 属性)。 比你有一个隐藏在 SubSonic 的简单存储库中的复杂类型(在这种情况下我更喜欢结构而不是类)。覆盖和运算符重载是可选的,但可以更轻松地使用此类型。

    示例用法:

    // 1. Step Create item1
    var item1 = new Record();
    item1.ID = 1;
    item1.UnknownInt = new UnknownInt(1, true);
    
    // 2. Setp Create item2
    var item2 = new Record();
    item2.ID = 2;
    item2.UnknownImt = new UnknownInt(1, false);
    
    if (item1.UnknownInt == item2.UnknownInt)
        Console.WriteLine("???");
    else
        Console.WriteLine("Profit!!!");
    

    【讨论】:

    • 这似乎是一个解决方案,不幸的是,我已经以一种不太优雅的方式解决了这个问题。不过,很好的答案,对后代来说很好!
    【解决方案2】:

    尝试使用可为空的 int (int?) 而不是 UnknownInt 类 - 您可以通过 subsonic 存储它。无需 XML 转换!

    【讨论】:

    • 不幸的是,这不是我想要的(我在其他地方经常使用 System.Nullable 并且我知道这一点)基本上,“null”值应该代表缺失的答案(即回答哪个统计模型应该无法报告需要更新的内容)。 “未知”答案是有效答案(即不为空),用户查看了问题并决定不回答。我可以通过创造性地使用超出可接受范围的整数值对其进行编码(本质上,年龄 -1 是“未知”,年龄 -2 是不想分享等),但这并不优雅。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多