我遇到了和你几乎完全相同的问题,并发现以下解决方案是可行的。
创建 SQL 视图
-- I'm guessing at the table join structure here
create view LookupView
as
select t.TableName,
ci.ColumnName,
bi.Id, --This ID column needs to be the one used as the FK from other tables
bi.*, --Or whatever columns you need
coalesce(di.TextDescription, di.NumericDescription) as Description
from TableInfo t
join ColumnInfo ci on t.Id=ci.TableId
join BusinessInfo bi on bi.Id=ci.BusinessId
join LookupDescriptionInfo di on di.id=ci.id
创建基本查找类
public class Lookup {
public virtual string Tablename {get; set;}
public virtual string ColumnName {get; set;}
public virtual string Description {get; set;}
public virtual int Id {get; set;}
//Other BusinessInfo properties
}
创建一个继承的 LookupClass
public class ArmourLookup : Lookup{}
在您的业务对象上使用 ArmourLookup 类。
public class HeroArmour{
//Usual properties etc....
public virtual ArmourLookup Lookup {get; set;}
}
创建子类区分映射集
public class LookupMap : ClassMap<Lookup> {
public LookupMap(){
Id(x=>x.Id).GeneratedBy.Assigned(); //Needs to be a unique ID
Map(x=>x.Tablename);
Map(x=>x.ColumnName);
Map(x=>x.Description);
//Business Info property mappings here
Table("LookupView")
DiscriminateSubClassesOnColumn<string>("ColumnName");
ReadOnly();
}
}
public class ArmourLookupMap : SubClassMap<ArmourLookup> {
public ArmourLookupMap (){
DiscriminatorValue("ArmourColumn");
}
}
现在您可以轻松地为您创建新类型的每一列重复子类映射。这里的问题是您无法在视图中更新或插入新的查找,因此您处于只读模式。
此方法使用列名作为鉴别符,因此无需表名,但如果您的查找表中有重复的列名,您可以为每个表创建一个基本查找类并在映射中指定过滤条件。
另一个可能的解决方案是使用由查找表中的 T4 模板生成的枚举。虽然这也是一种只读方法。
您还可以将每个查找表映射为一个类,并使用鉴别器模式从 ColumnInfo 表中获取不同的类型。
public class TableInfo {
public virtual int Id {get; set;}
public virtual string Tablename {get; set;}
public IList<ColumnInfo> Columns {get; set;}
}
public class ColumnInfo {
public virtual int Id {get; set;}
public virtual TableInfo TableInfo {get; set;}
public virtual BusinessInfo BusinessInfo {get; set;}
public virtual LookupDescriptionInfo LookupDescriptionInfo {get; set;}
//Other properties
}
public class ArmourInfoColumn : ColumnInfo {
//In the mapping you would discriminate on the columnname column.
}
etc...
如果您在列信息表中有重复的列名但 tableid 不同,您可以再次选择区分某些 XTable 类。
您还可以区分 ColumnType(数字或文本)并将 LookupDescription 类子类化以将不同的列用于“Description”属性。
如果您能提供您的表结构和一些示例值,我可以为您进一步充实这些想法。