0

我是 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 简单存储库?

干杯!

4

2 回答 2

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!!!");
于 2010-12-07T20:35:42.227 回答
0

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

于 2010-08-01T06:13:35.287 回答