0

I have a class that references another class with a composite Id:

SingleIdClassMap(){
  Id(x=>x.Id);
  References(x=>CompositeIdClass);
}

CompositeIdClass(){
  CompositeId().KeyReference(x => x.SingleIdClass).KeyReference(x => x.DynamicProperty);
}

Now this does not compile since in the SingleIdClassMap, there is no information about the DynamicProperty. I want this to be loaded from another class at runtime:

PropertyClass.Singleton.GetCurrentProperty();

Is there a way to tell NHibernate that it can retrieve the value for the second part of the composite key from PropertyClass.GetCurrentProperty()?

Dani
  • 2,602
  • 2
  • 23
  • 27
  • is `PropertyClass.Singleton.GetCurrentProperty();` static or does it change through the lifetime of the app – Firo Apr 16 '13 at 12:17
  • I would bet that what you want is hard (not impossible) to accomplish. Is this some kind of localization implementation? Then there are many easier alternatives – Firo Apr 17 '13 at 10:54
  • Yes it is. I have one table per entity that contains texts. Each line has a key to the entity and a language key. It was a demand to have one table per entity table. – Dani Apr 17 '13 at 11:30

1 Answers1

0

IMO a filter is easiest

EntityMap()
{
    Id(x => x.Id);
    HasMany(x => Texts)
        .KeyColumn("entity_id")
        .ApplyFilter("languageFilter", "language_id = :lid");
}

EntityTextClass()
{
    CompositeId()
        .KeyReference(x => x.Entity, "entity_id")
        .KeyProperty(x => x.LanguageId);
}

// at beginning of request
session.EnableFilter("languageFilter").SetParameter(":lid", languageId);

var entity = session.Query<Entity>().Fetch(e => e.Texts).First();
string text = entity.Texts.First();  // could be a seperate property

or if you need all Texts (e.g. When reviewing/editing localization)

var entity = session.Query<Entity>().Fetch(e => e.Texts).First();
var allTexts = entity.Texts;
Firo
  • 30,626
  • 4
  • 55
  • 94