Try fast search NHibernate

02 July 2009

NHibernate Configuration through lambdas

Part I : NHibernate Configuration

Part II : NHibernate Fluent Configuration

This will be, probably, the last way, to configure the session-factory, available in NH-Core.

  var configure = new Configuration();
configure.SessionFactoryName("SomeName");
configure.Cache(c =>
{
c.UseMinimalPuts = true;
c.DefaultExpiration = 15;
c.RegionsPrefix = "xyz";
c.Provider<HashtableCacheProvider>();
c.QueryCache<StandardQueryCache>();
});
configure.CollectionTypeFactory<DefaultCollectionTypeFactory>();
configure.HqlQueryTranslator<ClassicQueryTranslatorFactory>();
configure.Proxy(p =>
{
p.Validation = false;
p.ProxyFactoryFactory<ByteCode.LinFu.ProxyFactoryFactory>();
});
configure.Mappings(m=>
{
m.DefaultCatalog = "MyCatalog";
m.DefaultSchema = "MySche";
});
configure.DataBaseIntegration(db =>
{
db.Dialect<MsSql2000Dialect>();
db.KeywordsAutoImport = Hbm2DDLKeyWords.AutoQuote;
db.Batcher<SqlClientBatchingBatcherFactory>();
db.BatchSize = 15;
db.ConnectionProvider<DebugConnectionProvider>();
db.Driver<SqlClientDriver>();
db.ConnectionReleaseMode = ConnectionReleaseMode.AfterTransaction;
db.IsolationLevel = IsolationLevel.ReadCommitted;
db.ConnectionString = "The connection string";
db.AutoCommentSql = true;
db.ExceptionConverter<SQLStateConverter>();
db.PrepareCommands = true;
db.Timeout = 10;
db.MaximumDepthOfOuterJoinFetching = 11;
db.HqlToSqlSubstitutions = "true 1, false 0, yes 'Y', no 'N'";
db.SchemaAction = SchemaAutoAction.Validate;
});


Perhaps the configuration through lambda is less fluent but more clear, easy to use and R# friendly.

Resuming

With NH3.0.0, you have five or six ways to configure the session-factory. The advantage of the two added in NH3 is that both are strongly typed. The fluent-configuration and the lambda-configuration was implemented using extensions-methods; probably we will extend the configuration to have a fashion way to integrate some other project (I’m thinking in some extensions in NHV, for example).

All available configuration-ways are allowing merge/override with the previous configuration that mean you can do something like:

var configuration = new Configuration();
configuration.Configure()
.Configure(yourNhConfPath)
.SessionFactoryName("SomeName")
.Proxy(p =>
{
p.Validation = false;
p.ProxyFactoryFactory<ByteCode.LinFu.ProxyFactoryFactory>();
})
.AddResources(assembly
.GetManifestResourceNames().Where(r=> r.StartsWith("NameSpace.Mappings")),
assembly)
.SessionFactory()
.Caching
.Through<HashtableCacheProvider>()
.PrefixingRegionsWith("xyz")
.Queries
.Through<StandardQueryCache>()
.UsingMinimalPuts()
.WithDefaultExpiration(15);

that mean:

  1. configure through app.config
  2. override/merge with a hibernate.cfg.xml file
  3. override/merge the proxy configuration with lambda
  4. add resources from a namespace in one assembly
  5. override/merge the cache with the fluent-configuration

What’s next ?

After some needed thinking, the next step is the programmatic configuration of the mapping.

8 comments:

  1. Hi Fabio

    What is the different between this approach and FluentNhibernate configuration approach?

    ReplyDelete
  2. I don't understand what you mean ?
    Can you explain your question a little bit more ?

    ReplyDelete
  3. I believe he's referring to this: http://wiki.fluentnhibernate.org/show/DatabaseConfiguration

    ReplyDelete
  4. I didn't have review the code behind the implementation of FNH. From the view of the wiki page there are various difference. If @ryzam mean the same of James what I don't understand is the target a of comparison between NH configuration and FNH configuration.

    ReplyDelete
  5. They have the same goal. Why reinvent when you can improve?

    ReplyDelete
  6. @James
    I think the same.
    Perhaps for the same reason we have NH and EF, Spring and Windsor and StructureMap and AutoFact and Unity and LinFu and Funq, Log4Net and NLog and LAB, LinFu-AOP and Castle-AOP and Spring-AOP, J# and C# and VB.NET and Delphi.NET, NUnit and xUnit and MbUnit. Perhaps all are wheel but with different point of view and different structure, even if the goal of the wheel is the same.

    As you saw with nhibernatelambda, the integration had a different story.

    ReplyDelete
  7. Fabio

    I mean it the same what James had informed, it is about fluentnhibernate project. Just confuse with the one in NHibernate core.

    Thanks

    ReplyDelete
  8. NHibernate.Cfg.Configuration not in NHibernate 3.3.0.4000

    ReplyDelete