2008-11-17 7 views
7

Załóżmy, że mam trzy klasy. Ważne jest, aby utworzyć instancję A, ale są również specjalne przypadki B i D, które podklasą A, dodając dodatkowe informacje.Fluent NHibernate - jak zmapować podklasę jeden-do-jednego?

Jak zrobić pliki odwzorowań dla tego (płynnego) NHibernate?

public class A 
{ 
    public int ID { get; set;} 
    public string CommonProperty1 { get; set; } 
    public string CommonProperty2 { get; set; } 
} 

public class B : A 
{ 
    public string BSpecificProperty1 { get; set; } //not null 
    public string BSpecificProperty2 { get; set; } //not null 
} 

public class D : A 
{ 
    public string DSpecificProperty { get; set; } //not null 
} 

Próbowałem następujących, ale to nie działa w ogóle:

public class AMap : ClassMap<A> 
{ 
    public AMap() 
    { 
     Id(x => x.ID); 

     Map(x => x.CommonProperty1); 
     Map(x => x.CommonProperty2); 
    } 
} 

public class BMap : ClassMap<B> 
{ 
    public BMap() 
    { 
     References(x => x.ID); 
     Map(x => x.BSpecificProperty1) 
      .CanNotBeNull(); 
     Map(x => x.BSpecificProperty2) 
      .CanNotBeNull(); 
    } 
} 

public class DMap : ClassMap<D> 
{ 
    public DMap() 
    { 
     References(x => x.ID); 

     Map(x => x.DSpecificProperty) 
      .CanNotBeNull(); 
    } 
} 

Odpowiedz

7

Nie jestem pewien, czy rozumiem co masz na myśli przez „map podklasę jeden do jednego”, ale jeśli chcesz mapować dziedziczenie gdzie podklasy mają właściwości, które nie są pustych, można zrobić tak w Fluent-NHibernate:

// Domain classes 
public class Animal 
{ 
    public virtual int Id { get; set; } 
    public virtual string Name { get; set; } 
} 

public class Cat : Animal 
{ 
    public virtual int WhiskerLength { get; set; } 
    public virtual int ClawCount { get; set; } 
} 

public class Dog : Animal 
{ 
    public virtual int TailWagRate { get; set; } 
} 



// Mapping file 
public class AnimalMap : ClassMap<Animal> 
{ 
    public AnimalMap() 
    { 
     Id(x => x.Id) 
      .WithUnsavedValue(0) 
      .GeneratedBy.Native(); 

     Map(x => x.Name); 

     var catMap = JoinedSubClass<Cat>("CatId", sm => sm.Map(x => x.Id)); 

     catMap.Map(x => x.WhiskerLength) 
      .CanNotBeNull(); 
     catMap.Map(x => x.ClawCount) 
      .CanNotBeNull(); 

     JoinedSubClass<Dog>("DogId", sm => sm.Map(x => x.Id)) 
      .Map(x => x.TailWagRate) 
       .CanNotBeNull(); 
    } 
} 

Ponieważ chcesz właściwości podklasy być nie-null, trzeba użyj tabeli na klasę (join-subclass) sposób modelowania dziedziczenia. Dzieje się tak dlatego, że tabela-na-hierarchii wymaga, aby wszystkie właściwości podklasy były zerowalne.

Mam nadzieję, że to pomaga.

/Erik

+0

Erik, wielki post. – Berryl

5

Składnia mogły ulec zmianie w FNH od słupka Erika, ale jego przykładem jest trafne. Oto kod, którego użyłem na podstawie postu Erika, aby pracować przez FNH w dwóch strategiach podklas FNH, które znam teraz (SubClass (skomentowany kod poniżej i JoinedSubClass). Na marginesie widziałem inne nazwy używane do opisania Te same strategie, w tym w docs NHibernate, który jest nieco mylące, gdy jest to dla ciebie nowa. (https://www.hibernate.org/hib_docs/nhibernate/html/inheritance.html).

// Domain classes 
public class Animal : Entity 
{ 
    public virtual string Name { get; set; } 
    public virtual string Unwanted { get; set; } 
} 

public class Cat : Animal 
{ 
    public virtual int WhiskerLength { get; set; } 
    public virtual int ClawCount { get; set; } 
} 

public class Dog : Animal 
{ 
    public virtual int TailWagRate { get; set; } 
} 

public class Boxer : Dog 
{ 
    public string DroolBucket { get; set; } 
} 

public class AnimalMapJoinedSubclassOverride : IAutoMappingOverride<Animal> 
{ 
    public void Override(AutoMap<Animal> mapping) { 
     mapping.Map(x => x.Name); 

     mapping.IgnoreProperty(x => x.Unwanted); 

     mapping.JoinedSubClass("CatId", CatMap.AsJoinedSubClass()); 
     mapping.JoinedSubClass("DogId", DogMap.AsJoinedSubClass()); 
     //mapping.DiscriminateSubClassesOnColumn("Type") 
     // .SubClass<Cat>("CatId", CatMap.AsSubClass()) 
     // .SubClass<Dog>("CatId", DogMap.AsSubClass()); 
    } 
} 

public class CatMap 
{ 
    public static Action<JoinedSubClassPart<Cat>> AsJoinedSubClass() 
    { 
     return part => 
     { 
      part.Map(x => x.ClawCount).Not.Nullable(); 
      part.Map(x => x.WhiskerLength).Not.Nullable(); 
     }; 
    } 

    public static Action<SubClassPart<Cat>> AsSubClass() 
    { 
     return part => 
     { 
      part.Map(x => x.ClawCount); 
      part.Map(x => x.WhiskerLength); 
     }; 
    } 
} 

public class DogMap 
{ 
    public static Action<JoinedSubClassPart<Dog>> AsJoinedSubClass() 
    { 
     return sub => 
     { 
      sub.Map(x => x.TailWagRate).Not.Nullable(); 
     }; 
    } 

    public static Action<SubClassPart<Dog>> AsSubClass() 
    { 
     return sub => 
     { 
      sub.Map(x => x.TailWagRate); 
     }; 
    } 
} 

public class BoxerMap 
{ 
    public static Action<JoinedSubClassPart<Boxer>> AsJoinedSubClass() 
    { 
     return sub => 
     { 
      sub.Map(x => x.DroolBucket); 
     }; 
    } 

    public static Action<SubClassPart<Boxer>> AsSubClass() 
    { 
     return sub => 
     { 
      sub.Map(x => x.DroolBucket); 
     }; 
    } 
} 
Powiązane problemy