SubSonic SimpleRepository 存储成员类

发布于 2024-09-12 10:47:32 字数 456 浏览 9 评论 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 文本字段?)之前对其进行序列化?

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

换句话说 - 我的 UnknownInt 类需要实现哪些接口才能符合资格并可转换为 SubSonic 3.0 Simple Repository?

干杯!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月隐月明月朦胧 2024-09-19 10:47:32

我会这样做:

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

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!!!");
猥琐帝 2024-09-19 10:47:32

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文