Using Fluent NHibernate in Spring.Net AutoMap自动Map
Using Fluent NHibernate in Spring.Net
http://blog.bennymichielsen.be/2009/01/04/using-fluent-nhibernate-in-spring-net/
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
Fluently.Configure(config)
.Database(MySQLConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey(“connectionString”)))
.Mappings( m => m.FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();
}
{
base.PostProcessConfiguration(config);
Fluently.Configure(config)
.Database(MySQLConfiguration.Standard.ConnectionString(
c => c.FromConnectionStringWithKey(“connectionString”)))
.Mappings( m => m.FluentMappings
.AddFromAssemblyOf())
.BuildSessionFactory();
}
protected override ISessionFactory NewSessionFactory(Configuration config)
{
config = Fluently.Configure(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.BuildConfiguration();
return base.NewSessionFactory(config);
}
{
config = Fluently.Configure(config)
.Mappings(m => m.FluentMappings.AddFromAssemblyOf())
.BuildConfiguration();
return base.NewSessionFactory(config);
}
here is the implementation if you are using FluentNhibernate auto mapping :
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
var autoMappingCfg = new AutoMappingConfiguration();
var autoMap = AutoMap.AssemblyOf(autoMappingCfg)
.Conventions.Add(DefaultCascade.All(), DefaultLazy.Never())
.Conventions.Add()
.Override(map => { map.IgnoreProperty(i => i.Total); });
Fluently.Configure(config)
.Mappings(m => m.AutoMappings.Add(autoMap))
.BuildConfiguration();
}
in outmapping you don’t need to inject FluentNhibernateMappingAssemblies. so configure Sping.Net accordingly
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
var autoMappingCfg = new AutoMappingConfiguration();
var autoMap = AutoMap.AssemblyOf(autoMappingCfg)
.Conventions.Add(DefaultCascade.All(), DefaultLazy.Never())
.Conventions.Add()
.Override(map => { map.IgnoreProperty(i => i.Total); });
Fluently.Configure(config)
.Mappings(m => m.AutoMappings.Add(autoMap))
.BuildConfiguration();
}
in outmapping you don’t need to inject FluentNhibernateMappingAssemblies. so configure Sping.Net accordingly
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using FluentNHibernate;
using System.Reflection;
using NHibernate.Cfg;
namespace SessionFactories
{
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
/// <summary>
/// Sets the assemblies to load that contain fluent nhibernate mappings.
/// </summary>
/// <value>The mapping assemblies.</value>
public string[] FluentNhibernateMappingAssemblies
{
get;
set;
}
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
if(FluentNhibernateMappingAssemblies != null)
{
foreach(string assemblyName in FluentNhibernateMappingAssemblies)
{
config.AddMappingsFromAssembly(Assembly.Load(assemblyName));
}
}
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.NHibernate;
using FluentNHibernate;
using System.Reflection;
using NHibernate.Cfg;
namespace SessionFactories
{
public class FluentNhibernateLocalSessionFactoryObject
: LocalSessionFactoryObject
{
/// <summary>
/// Sets the assemblies to load that contain fluent nhibernate mappings.
/// </summary>
/// <value>The mapping assemblies.</value>
public string[] FluentNhibernateMappingAssemblies
{
get;
set;
}
protected override void PostProcessConfiguration(Configuration config)
{
base.PostProcessConfiguration(config);
if(FluentNhibernateMappingAssemblies != null)
{
foreach(string assemblyName in FluentNhibernateMappingAssemblies)
{
config.AddMappingsFromAssembly(Assembly.Load(assemblyName));
}
}
}
}
}