Access to NHibernate Configuration using Fluent Configuration

101 views
Skip to first unread message

Nic Strong

unread,
Feb 5, 2009, 7:33:18 PM2/5/09
to fluent-n...@googlegroups.com
I have recently switched over to using the Fluent Configuration
syntax. I have been using SessionSource from
FluentNHibernate.Framework when doing my tests. The constructor
requires the configuration properties from the nhibernate
configuration. Is there a nice way to get at this from the
FluentConfiguration?

Currently I am using a workaround saving the Configuration from
ExposeConfiguration():

[SetUp]
public void SetUp()
{
var bootStrapper = new MediaManagerBootstrapper();
Configuration config = null;
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m =>
m.AutoMappings.Add(bootStrapper.AutoPersistenceModel))
.ExposeConfiguration(c => { config = c; })
.BuildSessionFactory();

Session = sessionFactory.OpenSession();
var sessionSource = new SessionSource(config.Properties,
bootStrapper.AutoPersistenceModel);
sessionSource.BuildSchema(Session);
}


Any suggestions?

Nic

Chris Marisic

unread,
Feb 6, 2009, 9:47:09 AM2/6/09
to Fluent NHibernate
Using the ExposeConfiguration method looks perfectly clean to me.

Nic Strong

unread,
Feb 7, 2009, 5:33:29 AM2/7/09
to fluent-n...@googlegroups.com
It works OK for me too, but always looking for a way to make life easier :)

Would be nice to do something like:

var sessionSource = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m
=>m.AutoMappings.Add(bootStrapper.AutoPersistenceModel))
.BuildSessionSource();

To create the session source as opposed to the session factory.

Could do this with an extension method on FluentConfiguration but have
no access to the configuration.

Erik van Brakel

unread,
Feb 7, 2009, 6:59:37 AM2/7/09
to Fluent NHibernate
Oh, I think I wasn't paying attention, you need the SessionFactory.
Anyway, for an extension method using the configuration you'd do
something like:

public static FluentConfiguration BuildSessionSource(this
FluentConfiguration config)
{
return config.ExposeConfiguration(c =>
{
// your action with the config right here //
});

Erik van Brakel

unread,
Feb 7, 2009, 6:52:06 AM2/7/09
to Fluent NHibernate
I think I'd do the same thing, except I'd wrap the whole bit of code
that needs the configuration object into the anonymous method part:

[SetUp]
public void SetUp()
{
var bootStrapper = new MediaManagerBootstrapper();
var sessionFactory = Fluently.Configure()
.Database(SQLiteConfiguration.Standard.InMemory)
.Mappings(m => m.AutoMappings.Add
(bootStrapper.AutoPersistenceModel))

.ExposeConfiguration(c =>
{
Session = sessionFactory.OpenSession();
var sessionSource = new SessionSource(
c.Properties,bootStrapper.AutoPersistenceModel);
sessionSource.BuildSchema(Session);
})

.BuildSessionFactory();
}

Which probably means the code that builds the schema should be
extracted to it's own method, right?

Regards,

Erik
Reply all
Reply to author
Forward
0 new messages