BLToolkit 中的字段映射到类类型属性

发布于 2024-11-08 00:19:36 字数 1140 浏览 3 评论 0原文

我的表架构(摘录)

create table dbo.MyEntity
(
    MyEntityID int identity not null
        primary key,
    Name nvarchar(50) not null
        unique,
    Description nvarchar(500) null,
    -- these two are optional fields
    MaxCount int null,
    MinSpace int null
)

实体类

[MapField("MaxCount", "Rule.MaxCount")]
[MapField("MinSpace", "Rule.MinSpace")]
public class MyEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    // when values are not null this property should have an instance
    public MyEntityRule Rule { get; set; }

    public bool HasRule
    {
        get { return this.Rule != null; }
    }
}

public class MyEntityRule
{
    public int MaxCount { get; set; }

    public int MinSpace { get; set; }
}

有问题吗?

将字段映射到我的类是问题所在。我想直接从来自数据表(顶部)的平面结果集中映射内部类属性。

我已经在类级别设置了 MapFieldAttribute 设置(如上面的代码所示),但我的规则始终为空。假设问题的一部分是必须首先实例化此内部类属性才能分配给它,因为所有 BLToolkit 示例都使用不可为 null 的内部对象。但就我而言,如果它应该为 null (大多数情况下它会是 null),我不想创建它的实例。

那该怎么办呢?

My table schema (excerpt)

create table dbo.MyEntity
(
    MyEntityID int identity not null
        primary key,
    Name nvarchar(50) not null
        unique,
    Description nvarchar(500) null,
    -- these two are optional fields
    MaxCount int null,
    MinSpace int null
)

Entity class(es)

[MapField("MaxCount", "Rule.MaxCount")]
[MapField("MinSpace", "Rule.MinSpace")]
public class MyEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    // when values are not null this property should have an instance
    public MyEntityRule Rule { get; set; }

    public bool HasRule
    {
        get { return this.Rule != null; }
    }
}

public class MyEntityRule
{
    public int MaxCount { get; set; }

    public int MinSpace { get; set; }
}

Problem?

Mapping of fields to my class is the problem. I would like to directly map inner class properties from flat results set that comes from data table (at the top).

I've set MapFieldAttribute settings at class level (as seen in upper code), but my rules are always null. Is suppose part of the problem is that this inner class property has to be instantiated first to be assigned to, because all BLToolkit examples use non nullable inner objects. But in my case I don't want to create an instance of it if it should be null (most of the time it will be null).

How should this be done then?

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

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

发布评论

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

评论(2

晌融 2024-11-15 00:19:36

工作溶液

由于文档和社区支持非常有限或缺乏(至少在英语方面),我真的开始讨厌 BLToolkit

我只是测试了可能与此有些相关的各种属性,实际上我已经使它起作用了。

如果您希望嵌套对象按预期工作,则必须使用额外的 NoInstanceAttribute。并且您必须像以前一样将这些字段映射属性保留在类上。生成的工作代码如下:

[MapField("MaxCount", "Rule.MaxCount")]
[MapField("MinSpace", "Rule.MinSpace")]
public class MyEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [NoInstance] // this will make it work
    public MyEntityRule Rule { get; set; }

    public bool HasRule
    {
        get { return this.Rule != null; }
    }
}

所有未定义值的规则均为空,其他规则被实例化。

Working solution

I'm really starting to hate BLToolkit due to very limited documentation and community support or lack thereof (at least in English).

I was just testing various attributes that may be somewhat related to this and actually I've made it work.

If you want nested objects to work as expected you have to use an additional NoInstanceAttribute. And you have to keep those field mapping attributes on the class as before. The resulting working code is as follows:

[MapField("MaxCount", "Rule.MaxCount")]
[MapField("MinSpace", "Rule.MinSpace")]
public class MyEntity
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Description { get; set; }

    [NoInstance] // this will make it work
    public MyEntityRule Rule { get; set; }

    public bool HasRule
    {
        get { return this.Rule != null; }
    }
}

All rules that don't define values are null, others are instantiated.

得不到的就毁灭 2024-11-15 00:19:36

BLToolkit 不会创建 MyEntityRule 的实例,您必须自己创建。

BLToolkit does not create an instance of MyEntityRule You have to do it yourself..

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