开发者

SubSonic SimpleRepository storing member class

I'm new to C# and Subsonic. I'm trying to solve the following case:

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

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

I'm using SimpleRepository. Is there a way I can get UnknownInt serialized before storing it in 开发者_如何学Gothe SQL database (perhaps as XML text field?)

I'm trying to build a questionnaire system in which a user can provide an 'integer' answer, an 'Unknown' answer, as well as a Null answer (question not answered yet)

In other words - what interfaces does my UnknownInt class need to implement in order to be eligible and convertible into SubSonic 3.0 Simple Repository?

Cheers!


I would do this:

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);
    }

}

The basic idea is to have to columns that store your userdefined type but are hidden from intellisense (System.ComponentModel.EditorBrowsable attribute). Than you have a complex type (I prefer a struct rather than a class in this case) that is hidden from SubSonic's simple repository. The overrides and operator overloads are optional but make working with this type easier.

Example usage:

// 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!!!");


Try using a nullable int (int?) instead of you UnknownInt class - you can store it via subsonic. No XML conversion needed!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜