NHibernate Video Tutorial



So I finally got round to doing a simple Fluent NHibernate Tutorial screencast. I've been playing around with screencasts recently so thought what better quickstart to do than one on NHibernate. It roughly follows my previous NHibernate Tutorial but it's simplified to fit into YouTube's 15min limit. I hope you enjoy it, and please have a look at the recommended reading if you're interested in learning NHibernate. Be sure to checkout the Github repository here or just download the code from github as a zip

NHibernate Screencast

NHibernate Tutorial source code

So here's the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Mapping;


namespace FluentNHibernate.Example
{
    internal class Program
    {
        private static void Main(string[] args)
        {
          
            using(var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    var fordMake = new Make
                                       {
                                           Name = "Ford"
                                       };
                    var fiestaModel = new Model
                                          {
                                              Name = "Fiesta",
                                              Make = fordMake
                                          };

                    var car = new Car
                                  {
                                      Make = fordMake,
                                      Model = fiestaModel,
                                      Title = "Dans Car"
                                  };

                    session.Save(car);

                    transaction.Commit();

                    Console.WriteLine("Created Car " + car.Title);
                }
            }
            Console.ReadLine();
        }
    }


    public class MakeMap : ClassMap
    {
        public MakeMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);

        }
    }

    public class ModelMap : ClassMap
    {
        public ModelMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            References(x => x.Make).Cascade.All();

        }
    }

    public class CarMap : ClassMap
    {
        public CarMap()
        {
            Id(x => x.Id);
            Map(x => x.Title);
            References(x => x.Make).Cascade.All();
            References(x => x.Model).Cascade.All();

        }
    }

    public class Make
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
    }

    public class Model
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual Make Make { get; set; }
    }

    public class Car
    {
        public virtual int Id { get; set; }
        public virtual string Title { get; set; }
        public virtual Make Make { get; set; }
        public virtual Model Model { get; set; }
    }
   
}

And here's the NHibernateHelper class

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;

namespace FluentNHibernate.Example
{
    public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        private static ISessionFactory SessionFactory
        {
            get
            {
                if (_sessionFactory == null)
                    InitializeSessionFactory();

                return _sessionFactory;
            }
        }

        private static void InitializeSessionFactory()
        {
            _sessionFactory = Fluently.Configure()
                .Database(MsSqlConfiguration.MsSql2008
                              .ConnectionString(
                                  @"Server=localhost\SQLExpress;Database=SimpleNHibernate;Trusted_Connection=True;")
                              .ShowSql()
                )
                .Mappings(m =>
                          m.FluentMappings
                              .AddFromAssemblyOf<Car>())
                .ExposeConfiguration(cfg => new SchemaExport(cfg)
                                                .Create(true, true))
                .BuildSessionFactory();            
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }
    }
}

NHibernate Recommended Reading

If you're getting into NHibernate I'd recommend these books provding a good introduction and then a deeper dive. NHibernate in Action is a bit out of date but's still a great book.


blog comments powered by Disqus

Search

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014