2012-02-02 1 views
0

C#에서 Fluent NHibernate를 테스트하기 시작했습니다. 20 개의 관련 클래스가있는 잘 정규화 된 객체 구조를 가지고 있습니다. 현재 NHibernate 3.2에서 Fluent 1.3을 사용하고 있습니다. 지금까지 나는 잘 맞는 자동 맵 기능을 사용했습니다. 매우 편리합니다!Fluent AutoMap으로 ID 열을 일시적으로 끄시겠습니까?

BUT ... 테이블 중 3 개가 특정 ID 값으로 설정된 레코드가 필요한 "enum 테이블"입니다. 이 테이블을 수동으로 매핑하고 나머지는 자동 매핑하려고했습니다. 그러나 수동 테이블을 만들 때 자동 매핑되는 테이블을 참조하므로 실패합니다 (수동 매퍼에서는 사용할 수 없으며 수동 매퍼에서는 사용할 수 없음)

일부 자동 클래스 매핑은 기본 매핑을 사용하지 않을 수 있습니까? 커스텀 컨벤션을 만들려고했으나 성공하지 못했습니다.

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

도움 주셔서 감사합니다! –

관련 문제