Home >

The moment of day – Fluent NHibernate

1. February 2009

I’ve been working with Paul Batum for a while on Fluent NHibernate. He’s working on semantic model while I am trying to make it work against NHibernate mapping meta. I’ve tried several times but i didn’t like the things I had, I was almost retiring from FNH. Below is the mapping that I first tried to make work.

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id);
        Map(x => x.Name)
            .WithLengthOf(30)
            .CanNotBeNull();
        Map(x => x.Age)
            .CanNotBeNull();
    }
}


And thankfully made it work.

This made my day, and you can’t guess how happy I am.

Comments

2/1/2009 9:59:20 AM #
Wow, this is actually pretty nice! Things like this and Linq to NHibernate really leverage a lot of possibilities.
I currently _have_ to work with PHP, and I'd love to be able to do this in my projects. I'm not sure how far your PHP knowledge stretches, but do you think something like this can be done in PHP too? That's one of my main issues with PHP vs. C#/Java: lack of (N)Hibernate Wink I know you're a NHibernate dev, so how far fetched would creating an NHibernate'ish library with fluent mappings be for PHP? I'd love to try to make something like that if possible.
2/1/2009 11:11:49 AM #
Hi Erik,
Agreed on Linq/Lambda point. I am really not familiar with PHP, I've just written school homework code consisting of "for loops" only Smile
I know that there are some ORM solutions for PHP including Propel and you can mimic the fluent mapping stuff probably.
        Map(x => x.Name)
            .WithLengthOf(30)
            .CanNotBeNull();
Map is a function returning PropertyMap which has some methods such as WithLengthOf etc. The only thing you won't have is expressions such as x=>x.Name because I don't think it is something that PHP has.
3/16/2009 6:35:23 PM #
I'm using Fluent NH for mapping and NH as ORM in my C# application.. The problem is with schemaupdate class.. if I create an instance of that.. using the configuration I got from my exposeconfiguration function.. It doesnot update the schema.. I need some better documentation on SchemaUpdate class.. or might be.. setting the configuration to accomodate the new schema in an old database.. for versioning purposes..

I have tried a lot of variations but no DDL command is reflected on the actual database using SchemaUpdate Frown

If I expose the configuration from :
  private static ISessionFactory CreateSessionFactory()  
        {
             return Fluently.Configure()
                .Database(SQLiteConfiguration.Standard
                    .UsingFile(basefile))
                .Mappings(m =>                    m.FluentMappings.AddFromAssemblyOf<Program>())
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

and use it here :
private static void BuildSchema(NHibernate.Cfg.Configuration cfg)
        {
            config = cfg;          
        }
private static void update_the_schema()
     {
       new SchemaUpdate(config).Execute(true, true);        
     }    
//Here config is that static configuration variable obtained from ExposeConfiguration method...  
is this thing supposed to update my old DB?
11/20/2010 3:43:51 AM #
Hello People! Your blog is awesome! I would like to visit it everyday. It's very informative and amazing
1/10/2011 4:13:15 AM #
very nice blog, love the info you give.
1/13/2011 11:05:20 AM #
I suggest this site to my friends so it could be useful & informative for them also. Great effort.
3/21/2011 10:52:20 PM #
I have been searching all over the internet for something to give me the correct advice.  This is not exactly what I was looking for but it helped a lot.  Thanks
3/22/2011 6:09:08 PM #
I am very impressed with the articles on your site. I get so many ideas to help me. I will be coming back to check if you have more articles in the future.
3/22/2011 7:17:20 PM #
What can I say.  This article is pretty good compared to all the junk out there that I have seen.  
3/22/2011 11:04:00 PM #
I personally love the particular information this excellent online site always offers. Top notch!
3/23/2011 4:46:37 AM #
Hello, I have been reading and enjoying your posts for some time now.  I appreciate your views on the topics.  I'm finally going to bookmark your page so I can keep returning to it.  I continuously happen upon it and keep thinking it's valuable but never saving it.  See you next time!  Keep providing us with these great writings!
3/23/2011 1:16:10 PM #
The great fresh content keep me coming back here very frequently. Thanks a lot.
3/23/2011 3:39:42 PM #
Of course, what a great site and informative posts, I will add backlink - bookmark this site? Regards, Reader.
Comments are closed