Tags: , , , , | Categories: development Posted by pieterg on 3/15/2010 8:14 AM | Comments (10)

It’s difficult to break away from old traditions, but sometimes it pays to think a differently. NHibernate is an awesome example of what I am talking about. It gives a developer the flexibility of concentrating on writing code instead of having to synchronize the database with the data layer/code.

Currently, what I see happening quite often is the following.

  1. Specification document gets drawn up.
  2. Database is slapped together by the developer or DB “dude”.
  3. Developer starts working on the code, only to realize that a column is missing due to either poor planning or requirements changing, the developer spends time having to change/add a column/s and tracking down all the changes in the stored procedure and hopefully he/she has tracked all of them down, otherwise….

    15154

I have 2 words, Fluent NHibernate…

How to setup Fluent NHibernate

  1. Download Fluent NHibernate from http://fluentnhibernate.org/downloads
  2. Add references to the DLLs
  3. Create your first Entity
    public class Person
    {
    	public virtual int ID { get; private set; }
    	public virtual string Name { get; set; }
    	public virtual string Lastname { get; set; }
    }
    
    public class PersonMap : ClassMap<Person>
    {
    	public PersonMap()
    	{
    		Schema("dbo");
    		Table("Person");
    		Id(x => x.ID);
    		Map(x => x.Name);
    		Map(x => x.Lastname);
    	}
    }
  4. Setup Fluent NHibernate and this is where fluent really shines. No more having to deal with the pain of the XML configuration.
    public static class SessionFactory
    {
    	public static ISessionFactory CreateSessionFactory()
    	{
    		return Fluently.Configure()
    			.Database(MsSqlConfiguration.MsSql2005
    			.ConnectionString(
    				c => c.FromConnectionStringWithKey("AthenaConnectionString")))
    			.Mappings(
    				m => m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()))
    			.ExposeConfiguration(BuildSchema)
    			.BuildSessionFactory();
    	}
    
    	private static void BuildSchema(Configuration config)
    	{
    		new SchemaUpdate(config).Execute(true, true);
    	}
    }
    
  5. How to use Fluent NHibernate in a simple example
var sessionFactory = SessionFactory.CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
	using (var transaction = session.BeginTransaction())
	{
		Person person = new Person { Name = "Pieter", Lastname = "Germishuys" };
		session.SaveOrUpdate(person);
		transaction.Commit();
	}
}

3JDQDM2DSFR8

Comments

Jada
Jada United States on 2/23/2010 3:29 AM Hey I just wanted to get the scoop on what is the difference between blogenenigne and wordpress blogs? Is it easier to use or more efficient? I amseeing a lot of blogs powered by this software popping up lately and wondering if it is better or not? Thanks...
guitar lessons for beginners
guitar lessons for beginners United States on 3/2/2010 11:08 AM i know this is not exactly on topic, but i have a blog using the blogengine platform as well and i'm having troubles with my comments displaying. is there a setting i am forgetting? maybe you could help me out? thank you.
learn to play guitar
learn to play guitar United States on 3/4/2010 2:12 AM i like the layout of your blog. hope you don't mind if i do something similar with mine.
wow leveling guide
wow leveling guide United States on 3/12/2010 3:40 AM Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.
fat loss 4 idiots review
fat loss 4 idiots review United States on 3/21/2010 2:47 PM i know this is not exactly on topic, but i run a blog using the blogengine platform as well and i'm having issues with my comments displaying. is there a setting i am missing? maybe you could help me out? thank you.
AVG Antivirus Review
AVG Antivirus Review United States on 4/1/2010 10:46 AM Thank you for this helpfull review. It really was helpfull. Keep up the good work.
Rapidshare
Rapidshare United States on 4/5/2010 6:38 AM I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!
Rapidshare Search engine
Rapidshare Search engine United States on 4/11/2010 9:11 PM Top Rapidshare Search engine
locksmith center
locksmith center Faroe Islands on 4/14/2010 9:15 PM Couldnt be written any better about asp.net . Reading this column reminds me of my old room mate! He always kept talking regarding this. I'll forward this informative article to him. Pretty sure he will have an excellent read. Gratitude for sharing!
Hershel Brugal
Hershel Brugal Principality of Monaco on 4/17/2010 6:22 AM

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading