Question

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 the 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!

Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top