开发者

Fluent NHibernate, dynamically change Table of mapping?

with fluent nhibernate, is there a way to dynamically switch the table of a mapping at runtime?

For example:

开发者_开发百科public class XYClassMap : ClassMap<XY>
{
  public XYClassMap( )
  {
    Table("XYTable");
    Id(d => d.Id).GeneratedBy.Identity();
    Map(d => d.Value);
    (...)

Given that there are multiple plugins, each of them uses this one class, but they need to work with different tables. I am looking for something like this:

public class XY {
  public string Tablename {get; set;}
}

public class XYClassMap : ClassMap<XY>
{
  public XYClassMap( )
  {
    Table(Tablename);
    Id(d => d.Id).GeneratedBy.Identity();
    Map(d => d.Value);
    (...)

So every action method could work with the same class and only would need to set this one property "Tablename".

Thanks for any help,

Steffen


Mappings are compiled on application startup, and once that's happened they cannot be changed.

If your plugins use different tables, then by definition they aren't the same entities (and thus the same mappings). If your plugins all define different tables which all have the same structure, then you need separate mappings but you might be able to abstract the actual definitions.

Something like this, perhaps: each plugin defines it's own mapping for this table, which derive from an abstract class containing the actual mappings except for the table name.

public abstract class PluginMap<T> : ClassMap<T> where T : IPlugin
{
  public PluginMap()
  {
    Id(d => d.Id).GeneratedBy.Identity();
    Map(d => d.Value);
  }
}

public class PluginOneMap : PluginMap<PluginOne>
{
  public PluginOneMap()
  {
    Table("PluginOne");
  }
}

public class PluginTwoMap : PluginMap<PluginTwo>
{
  public PluginTwoMap()
  {
    Table("PluginTwo");
  }
}

Alternatively, you could define them as subclasses in an table-per-subclass or table-per-inheritance-hierarchy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜