Вопрос

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?

Это было полезно?

Решение

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.

Другие советы

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

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top