views:

117

answers:

5

I was going through Fluent nhibernate wiki and i know that Fluent nhibernate is built on top of nHibernate... Should i care/have knowledge about nHibernate before choosing Fluent nHibernate? Any suggestion...

+1  A: 

of course, fluent nhibernate is mainly there to make mapping simpler (and type safe)

Jaguar
@jaguar can you give me an example how to setup fluent nhibernate with asp.net mvc...
Pandiya Chendur
A: 

I say yes. If you know NHibernate's XML based mapping format, it's much easier to track down errors via fluent NH's [FluentMappingsContainer].ExportTo([e.g. Environment.CurrentDirectory]).

Edit: ASP.NET MVC example w/ StructureMap

StructureMap:

 private static void ConfigureSQLiteInMemoryTest(IInitializationExpression init)
        {
            init.For<ISessionFactory>()
                .Singleton()
                .Use( Fluently.Configure()
                          .Database( SQLiteConfiguration.Standard.InMemory().AdoNetBatchSize( 100 ).ShowSql )
                          .Mappings( m => m.FluentMappings.AddFromAssemblyOf<MyEntity>() )
                          .ExposeConfiguration( config =>
                                                    {
                                                        config.SetProperty( NHEnvironment.ProxyFactoryFactoryClass,
                                                                            typeof( ProxyFactoryFactory ).AssemblyQualifiedName );   

                                                    } )
                          .BuildSessionFactory() );

            init.For<ISession>()
                .LifecycleIs( GetLifecycle() )
                .Use( context =>
                          {
                              var session = context.GetInstance<ISessionFactory>().OpenSession();

                              new TestData( session, _nhConfig ).Create();

                              return session;
                          } );
        }

Tell MVC to use a StructureMap based controller factory:

Global.asax.cs:

protected void Application_Start()
        {
            [...]

            var controllerFactory = new StructureMapControllerFactory( ObjectFactory.Container );

            ControllerBuilder.Current.SetControllerFactory( controllerFactory );

            [...]

        }

public class StructureMapControllerFactory : DefaultControllerFactory
    {
        private readonly IContainer _container;

        public StructureMapControllerFactory( IContainer container )
        {
            _container = container;
        }

        protected override IController GetControllerInstance( RequestContext requestContext, Type controllerType )
        {
            if (controllerType == null)
                return null;

            return (IController)_container.GetInstance( controllerType );
        }
    }
Martin R-L
@Martin can you give me an example how to setup fluent nhibernate with asp.net mvc...
Pandiya Chendur
@Martin can i use this for mysql Database instead of sqllite.. What should i do?
Pandiya Chendur
I guess you can. I've only used NH w/ SQLite, SQLS, and Oracle myself.Try changing the param of the Database(...) method to one that defines MySql.
Martin R-L
A: 

Try the answer for this question for tutorials

http://stackoverflow.com/questions/596357/nhibernate-fluent-tutorials

It makes sense to have a grasp of NHibernate before you learn fluent nhibernate. As @Jaguar says it sits on top of nhibernate.

It might be worth looking at nhlambdaextensions.googlecode.com - although this is going to be included in the next version!

For Nhibernate tutorials check out dimecasts or tekpub - or nhforge - see question

http://stackoverflow.com/questions/1009110/learning-nhibernate

NHibernate is database agnostic. :)

Aim Kai
+2  A: 

You absolutely need to learn NHibernate. Fluent NHibernate is only a wrapper over NHibernate's mapping API, and mapping is only a small part of working with NHibernate.

Queries (Criteria/HQL/LINQ), sessions, locking, lazy/eager loading, etc, are concepts that you must know when working with NHibernate and have nothing to do with Fluent NHibernate.

Mauricio Scheffer
+1  A: 
UpTheCreek