Try fast search NHibernate

01 July 2009

NHibernate Fluent Configuration

In this post you saw how use NH2.1.0 configuration in different ways. This post is about the fluent-interface based configuration of NH3.0.0 (well… perhaps it is not the final implementation but only closer to it). Another programmatic configuration based on lambda usage is coming.

Minimal

var cfg = new Configuration();
cfg.SessionFactory()
.Proxy.Through<ByteCode.LinFu.ProxyFactoryFactory>()
.Integrate
.Using<MsSql2005Dialect>()
.Connected
.Using(new SqlConnectionStringBuilder
{
DataSource = "(local)",
InitialCatalog = "nhibernate",
IntegratedSecurity = true
});
To configure the connection-string I’m using the DbConnectionStringBuilder of a specific DataProvider (in this case System.Data.SqlClient.SqlConnectionStringBuilder; your DataProvider should have its own implementation).

Pretty Complete

var cfg = new Configuration();
cfg.SessionFactory().Named("SomeName")
.Caching
.Through<HashtableCacheProvider>()
.PrefixingRegionsWith("xyz")
.Queries
.Through<StandardQueryCache>()
.UsingMinimalPuts()
.WithDefaultExpiration(15)
.GeneratingCollections
.Through<DefaultCollectionTypeFactory>()
.Proxy
.DisableValidation()
.Through<ProxyFactoryFactory>()
.ParsingHqlThrough<ClassicQueryTranslatorFactory>()
.Mapping
.UsingDefaultCatalog("MyCatalog")
.UsingDefaultSchema("MySche")
.Integrate
.Using<MsSql2000Dialect>()
.AutoQuoteKeywords()
.BatchingQueries
.Through<SqlClientBatchingBatcherFactory>()
.Each(15)
.Connected
.Through<DebugConnectionProvider>()
.By<SqlClientDriver>()
.Releasing(ConnectionReleaseMode.AfterTransaction)
.With(IsolationLevel.ReadCommitted)
.Using("The connection string")
.CreateCommands
.AutoCommentingSql()
.ConvertingExceptionsThrough<SQLStateConverter>()
.Preparing()
.WithTimeout(10)
.WithMaximumDepthOfOuterJoinFetching(11)
.WithHqlToSqlSubstitutions("true 1, false 0, yes 'Y', no 'N'")
.Schema
.Validating()
;

Reminder

Another programmatic configuration based on lambda usage is coming

8 comments:

  1. While I understand why one would use fluent mapping I am not really sure about this one. Yes, we have yet another option how to configure things, but what's so wrong with xml (using xsl schema and intellisense)? How often is anyone going to refactor NH config? Or is it really to get around type names in strings?

    Please do not take this as being offensive. I just must be missing something... ;-)

    ReplyDelete
  2. Yes, you are
    Integrate.Using<MsSql2000Dialect>()
    Try to use something that is not a type compatible NH's Dialect and watch what happen when you try to copile your code (the same for other methods).

    ReplyDelete
  3. Personally, i think code is much better option than xml. There are many reasons to it, one being that you can write your code in loops and surround with if statements. That is a nice thing. With xml you woun't be able to achieve that easily.

    So i am very happy new options start to come! More options equals more flexibility, and more flexibility means more ways to adapt my software to clients needs. Everything boils down to how much flexibility you can present to your client via your software. And it is really importance when comes to questions "why should i buy this product, or another"

    ReplyDelete
  4. This is great stuff for modular applications. One can have a fully-functional general-purpose application and still customize it for different clients simply by providing replacement customized classes (for ex. with additional properties) and then mapping them programmatically when the customized assembly is loaded... Something similar is already possible with NHibernate but not too easy to do. Nice work!

    ReplyDelete
  5. Hello is there a fluent mapping feature built in to NHibernate? (of course there is fluent-nhibernate) but i just want to know if there is/would be a feature similar to fluent-nhibernate mapping.

    ReplyDelete
  6. Well its a mixed curiosity and confusion, i am newbie in NHibernate and i am kinda on the research stage right now. I want to know what would be the best approach when mapping my domain model? and i'm kinda attracted by the idea of fluent mapping.

    so i delve into fluent-nhibernate. we also know that they have this fluent configuration feature and NHibernate itself has its own.

    So now i'm kinda left in the crossroad. Which path should i take? Or Which is the best part to take?

    Thank you! :)

    ReplyDelete
  7. Can you help me configure Fluent NHibernate + NHibernate.MemCache. My configuration code fails. Please refer here for more details: http://stackoverflow.com/questions/23817330/configure-fluent-nhibernate-with-nhibernate-caches-memcached

    Please help. Thanks in advance

    ReplyDelete