2012-02-02 7 views
0

私はC#でFluent NHibernateをテストし始めました 20の関連クラスを持つよく正規化されたオブジェクト構造を持っています。 私は現在、NHibernate 3.2でFluent 1.3を使用しています。 これまでのところ私はうまく適合するオートマップ機能を使用することができました。 非常に便利です!Fluent AutoMapでID列を一時的に無効にしますか?

BUT ... 3つのテーブルは、特定のId値でレコードを設定する必要がある「列挙テーブル」です。 私はこれらのテーブルの手動マッピングを作成し、残りのテーブルを自動化しようとしました。 マニュアルテーブルを作成すると、自動マップされたテーブルを参照するために失敗します(手動マッパーでは使用できません)

オートマッピングは使用できますか? 私はカスタムコンベンションをしようとしましたが、成功しませんでした。

public class OverrideIdentityGeneration : Attribute 
{ 
} 

public class ConventionIdentity : AttributePropertyConvention<OverrideIdentityGeneration> 
{ 
    protected override void Apply(OverrideIdentityGeneration attribute, IPropertyInstance instance) 
    { 
     instance.Generated.Never(); 
    } 
} 

他にもいくつかの方法がありますか? 更新

答えて

-2

私はFifoの考えを使用し、代わりにカスタム属性を使用するように拡張しました。 他の慣習で同様のアイデアを使用すると、コードを読みやすくして冗長性を避けるために、カスタム属性を確認する拡張メソッドを追加しました。

これは私がなってしまったコードは次のとおりです。

/// <summary> 
/// Convention to instruct FluentNHIbernate to NOT generate identity columns 
/// when custom attribute is set. 
/// </summary> 
public class ConventionIdentity : IIdConvention 
{ 
    public void Apply(IIdentityInstance instance) 
    { 
     if(instance.CustomAttributeIsSet<NoIdentity>()) 
      instance.GeneratedBy.Assigned(); 
    } 
} 

/// <summary> 
/// Custom attribute definition. 
/// </summary> 
public class NoIdentity : Attribute 
{ 
} 

/// <summary> 
/// Example on how to set attribute. 
/// </summary> 
public class Category 
{ 
    [NoIdentity] 
    public int Id { get; set; } 
    public string Name { get; set; } 
} 

public static class IInspectorExtender 
{ 
    /// <summary> 
    /// Extender to make convention usage easier. 
    /// </summary> 
    public static T GetCustomAttribute<T>(this IInspector instance) 
    { 
     var memberInfos = instance.EntityType.GetMember(instance.StringIdentifierForModel); 
     if(memberInfos.Length > 0) 
     { 
      var customAttributes = memberInfos[0].GetCustomAttributes(false); 
      return customAttributes.OfType<T>().FirstOrDefault(); 
     } 
     return default(T); 
    } 
} 
4
class MyIdConvention : IIdConvention 
{ 
    public void Apply(IIdentityInstance instance) 
    { 
     if (instance.EntityType == ...) 
     { 
      instance.GeneratedBy.Assigned(); 
     } 
    } 
} 

....すべてのクラスの手動マッピングを使用するために戻って強制的に悲しいだろう:それはID

として列挙型を定義する方が簡単だ列挙のようなクラスのための

class ConfigValue 
{ 
    public virtual Config Id { get; set; } 
} 

// the convention is easy 
if (instance.EntityType.IsEnum) 
{ 
    instance.GeneratedBy.Assigned(); 
    // to save as int and not string 
    instance.CustomType(typeof(Config)); 
} 

// querying without magic int values 
var configValue = Session.Get<ConfigValue>(Config.UIColor); 
+0

おかげで、それがうまく働きました! 私が逃したのはIIdConventionでした.... 利用可能なすべての慣習を持つリストはどこにありますか? 私の次の課題は、ForeignKeyConstraintNameConventionを作成することですが、私はもう一度推測することに悩まされています。 –

+0

「FluentNHibernate.Conventions.I'」と入力してintellisenseに伝えるか、http://wiki.fluentnhibernate.org/Conventions – Firo

+0

ご協力ありがとうございます! –

関連する問題