Register   Reset password

Get Cuyahoga at SourceForge.net. Fast, secure and Free Open Source software downloads


Welcome Guest Search | Active Topics | Members

Fluent NHibernate and T4 Templates Options
Posted: Saturday, February 28, 2009 9:20:18 AM
Rank: Administration
Groups: Administration , Member

Joined: 10/7/2008
Posts: 505
Points: 1,515
Today I have been looking at using T4 (Text Template Transformation Toolkit) in VS2008 for the generation of NHibernate mapping. It is simple, powerfull and fast.

There seem to be some real benefits including the immediate reflection of changes in the domain on mapping files and of course not having to deal with repetitive verbose xml.

Download FluentNHibernate

Here is some info on:

Getting started guide
Fluent mapping
Auto Mapping


I am now going to look at adding FluentNHibernate to my Cuyahoga.

If anyone has any information on how best to approach this or has any pointers/things to be aware of, I would be greatful!

Posted: Saturday, March 7, 2009 2:57:19 PM
Rank: Administration
Groups: Administration , Member

Joined: 10/7/2008
Posts: 505
Points: 1,515
I have has a look at this and it should work without any changes to Cuyahoga. You should be able to use it within the context of a module.

The mappings are generated using T4 templates in a separate project within the same solution. This process is instant. Drag and drop the tables into a LINQ to SQL class and the T4 generates the mapping. Any changes to the database are reflected immediately (you just delete the table from the LINQ to SQL class and add the altered table). At the moment there is still a cut and paste into the web project domain. This step can be removed with some polish and a build event to copy/rename them.

The good thing is that if the mapping is incorrect then it will not build. They are also a lot less noisy.

I managed to dump some database info into a gridview really quick like this ( this is only a test happy ).


        private ISessionFactory _SessionFactory;
        private ISession _Session;

       private static ISessionFactory CreateSessionFactory()
            return Fluently.Configure()
                .ConnectionString( c =>
                    c.FromConnectionStringWithKey( "connectionString" ))
                .Mappings( m =>
                    m.FluentMappings.AddFromAssemblyOf (nc_Catalog) ()

        protected void Page_Load(object sender, EventArgs e)
            this._SessionFactory = CreateSessionFactory();
            this._Session = _SessionFactory.OpenSession();

            string hql = "select c from NHTests.Domain.nc_Catalog c";
            IQuery q = _Session.CreateQuery(hql);

            GridView1.DataSource = q.List();
Posted: Thursday, May 14, 2009 1:07:04 AM
Rank: Advanced Member
Groups: Member

Joined: 4/3/2006
Posts: 243
Points: 450
Location: the moon :)

i do think that strongly typed compile time errors stuff IS great.
But i think the code is not ready to be used in anger yet.
i read the team blog, and they are having to do a major major redesign to allow it to work with some corner cases of nhibernate it seems.
sorry don't have the URL right now.

can you mix and match Fluent with hbml in the same project i wonder ??

hence, although a great idea i think that it is not ready for the Cuyahoga Framework just yet.
This is my 2 cents though.

if your doing it hard your probably doing it the wrong way....
Posted: Thursday, May 14, 2009 2:49:40 AM
Rank: Administration
Groups: Administration , Member

Joined: 10/7/2008
Posts: 505
Points: 1,515
I agree. I had a look at this a while back and found it to be very exciting. Removing a lot of the noise from the process of using NHibernate. I have not as yet used this in any 'real' sense for the reasons you state.

I used a test module for Fluent and although it was basic it worked fine. Within the context of that module I did not use any standard hbml mapping. I can not see what would prevent this. The question would be, why use both though?

The ideal would be the ability to change your model and have these changes reflected in the domain objects and mapping instantly and consistently. HOW information is persisted should not really be a question that needs to be asked. It should just happen. IMO.
Users browsing this topic

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

Yet Another Forum.net version 1.9.0 running under Cuyahoga.
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.