1

次のような条件付きゲッターを使用できますか:

プロパティがnullかどうかを確認する必要がある場合、そのnullが同じクラスの別のプロパティを返す場合。

これは nHibernate マッピング用のクラスです。

public virtual District District 
        {
            get 
            {
                return this.District == null ? this.Zone : this.District; 
            } 
            set
            {
                this.District = value;
            }
        }

これを試したところ、サーバーがハングアップしました...

4

3 に答える 3

2

NHibernate マッピングの場合、マップされたプロパティにロジックを追加しません。その場合、個別のメソッドを追加します。

public virtual District District  { get; set; }

public virtual District GetDistrictOrDefault()
{
    return District ?? this.Zone;
}

または、マッピングされた District-Property を保護したい場合があります。保護されたプロパティをラップする 2 つ目のプロパティを提供して、いくつかのロジックを提供することができます。

// Mapped with NHibernate
protected virtual District District  { get; set; }

public virtual District DistrictOrDefault // You might find a better naming... 
{
    get 
    {
        return District ?? this.Zone; 
    } 
    set
    {
        District = value;
    }
}
于 2013-09-05T06:43:40.010 に答える
1

It's possible and quite simple to do just that. NHibernate allows us to specify access strategy for individual property. I'm frequently use something like this:

protected District _district;
public virtual District District 
{
    get { return _district ?? this.Zone; }
    set { _district = value; }
}

And mapping for the property:

<property name="District" access="field.camelcase-underscore" />

In this mapping scheme, your code will use the property to get/set data, while NHibernate uses the field to do the same. If you leave out the access setting, in case the property District is really NULL, NHibernate will think that you have changed the property District to the new value and it will try to update the corresponding database record. This might result in bugs. I've never used Fluent NHibernate, so I don't know how to do that with Fluent.

于 2013-09-09T06:56:51.793 に答える