存储成员类的SubSonic SimpleRepository

时间:2010-07-31 16:04:57

标签: c# sql serialization subsonic subsonic-simplerepository

我是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 Simple Repository?

干杯!

2 个答案:

答案 0 :(得分: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);
    }

}

基本思想是必须存储存储用户定义类型的列,但这些列是从intellisense(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!!!");

答案 1 :(得分:0)

尝试使用nullable int(int?)而不是UnknownInt类 - 您可以通过亚音速存储它。无需转换XML!

相关问题