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?

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading