我正在使用Nhibernate 3.0.0.1001和Castle.Facilities.NHibernateIntegration 1.1.0.0
我正在使用Castle将ISessionManager注入我的会话管理类。
我在web.config中指定My ConfigurationBuilder,如下所示:
<castle>
<facilities>
<facility id="MyNhibernateFacility"
type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration"
isWeb="false"
configurationBuilder="MyDomainModelAssembly.ConfigurationBuilder, MyDomainModelAssembly">
<factory id="nhibernate.factory">
<settings>
<item key="show_sql">true</item>
<item key="connection.provider">NHibernate.Connection.DriverConnectionProvider</item>
<item key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
<item key="dialect">NHibernate.Dialect.MsSql2005Dialect</item>
<item key="connection.connection_string">Data Source=.\SQL2005;Initial
Catalog=MyDb;User Id=xxx;Password=xxx;MultipleActiveResultSets=true</item>
<item key="proxyfactory.factory_class">
NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle</item>
</settings>
</factory>
</facility>
我的“ConfigurationBuilder”类看起来像这样:
public class ConfigurationBuilder : IConfigurationBuilder
{
public Configuration GetConfiguration(IConfiguration facilityConfiguration)
{
var defaultConfigurationBuilder = new DefaultConfigurationBuilder();
var configuration =
defaultConfigurationBuilder.GetConfiguration(facilityConfiguration);
return Fluently.Configure(configuration)
.Mappings(ConfigureMappings)
.BuildConfiguration();
}
public void ConfigureMappings(MappingConfiguration mappingConfiguration)
{
mappingConfiguration.FluentMappings
.AddFromAssemblyOf<MyModelClass>()
.Conventions.Add(
ForeignKey.EndsWith("Id"),
PrimaryKey.Name.Is(n => "Id"),
DefaultCascade.None(),
new HiLoPrimarykeyConvention());
}
public class HiLoPrimarykeyConvention : IIdConvention
{
public void Apply(IIdentityInstance instance)
{
instance.GeneratedBy.HiLo("1000");
}
}
}
我想使ConfigurationBuilder更通用,以便它不依赖于它映射到的模型。
所以而不是:
.AddFromAssemblyOf<MyModelClass>().
我想说的是:
.AddFromAssemblyOf<T>().
我可以在ConfigurationBuilder类中执行此操作
但是有没有办法在web.config工具中指定“T”?
例如,我知道我不能这样做:
<castle>
<facilities>
<facility ...
configurationBuilder="MyGenericNhibernateSessionManagementAssembly.
ConfigurationBuilder<MyModelClass>, MyGenericNhibernateSessionManagementAssembly">
答案 0 :(得分:1)
搞定了!这是怎么做的:
配置构建器中的:
public class ConfigurationBuilder<T> :
...
public static void ConfigureMappings(MappingConfiguration mappingConfiguration)
{
mappingConfiguration.FluentMappings
.AddFromAssemblyOf<T>()
然后在你的web.config
中<castle>
<facilities>
<facility ...
configurationBuilder="MyGenericNhibernateSessionManagementAssembly.
ConfigurationBuilder`1[[YourModelsAssembly.AModel, YourModelsAssembly]],
MyGenericNhibernateSessionManagementAssembly">