Free NHibernate, dynamically changes the mapping table?

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

For instance:

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

Given that there are several 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);
    (...)

Thus, each action method can work with the same class, and only he will need to set this property to “Table Name”.

Thanks for any help

Steffen

+3
source share
1 answer

Mappings are compiled at application startup, and after that they cannot be changed.

, (, , ). , , , .

- , : , , , .

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");
  }
}

.

+2

Source: https://habr.com/ru/post/1733383/


All Articles