Fluent NHibernate And You

I finally got around to writing a little code with Fluent NHibernate and I like it. Of course I knew I probably would, but it does something I didn’t realize.. at least with SQL Lite, it creates the Schema for you from the mapped entities (might be an NHibernate feature). You can grab the source and see it in action with the sample project. Now this is both good and bad because I will have to come up with a versioning approach to deal with this. Basically you do these things:

Create your entities

   1:  public class WebLink
   2:  {
   3:    public virtual int Id { get; private set; }
   4:    public virtual string URL { get; set; }
   5:    public virtual string Name { get; set; }
   6:    public virtual bool OpenNewWindow { get; set; }
   7:   }

Create the entity maps

   1:  public class WebLinkMap : ClassMap<WebLink>
   2:  {
   3:    public WebLinkMap()
   4:    {
   5:      Id(x => x.Id);
   6:      Map(x => x.URL);
   7:      Map(x => x.Name);
   8:    }
   9:  }

Setup configuration and get an ISession object

   1:  public ISessionFactory CreateSessionFactory()
   2:  {
   3:    if (_sessionFactory == null)
   4:   
   5:    {
   6:      _sessionFactory = Fluently.Configure()
   7:        .Database(SQLiteConfiguration.Standard
   8:         .UsingFile(DbFile))
   9:        .Mappings(m =>
  10:        m.FluentMappings.AddFromAssemblyOf<BootStrap>())
  11:        .ExposeConfiguration(BuildSchema)
  12:        .BuildSessionFactory();
  13:    }
  14:   
  15:    return _sessionFactory;
  16:  }
  17:   
  18:  private void BuildSchema(Configuration config)
  19:  {
  20:    // delete the existing db on each run
  21:    if (File.Exists(DbFile))
  22:        File.Delete(DbFile);
  23:   
  24:    // this NHibernate tool takes a configuration (with mapping info in)
  25:    // and exports a database schema from it
  26:    new SchemaExport(config)
  27:        .Create(false, true);
  28:  }

Add your data

   1:  public void SetupData()
   2:  {
   3:    var sessionFactory = BootStrapper.CreateSessionFactory();
   4:    using (var session = sessionFactory.OpenSession())
   5:    {
   6:      // populate the database
   7:      using (var transaction = session.BeginTransaction())
   8:      {
   9:        //links
  10:        var link1 = new WebLink { URL = "http://semperfifund.org", Name = "semper fi fund", OpenNewWindow = true };
  11:        var link2 = new WebLink { URL = "http://www.woundedwarriorproject.org/", Name = "wounded warriors", OpenNewWindow = true };
  12:        var link3 = new WebLink { URL = "http://www.sunshineacres.org/", Name = "sunshine acres", OpenNewWindow = true };
  13:   
  14:        SaveLinks(session, link1, link2, link3);
  15:        transaction.Commit();
  16:      }
  17:    }
  18:  }
  19:   
  20:  private void SaveLinks(ISession session, params WebLink[] links)
  21:  {
  22:    foreach (var link in links)
  23:      session.SaveOrUpdate(link);
  24:  }

Query for data

   1:  [Test]
   2:  public void have_links()
   3:  {
   4:    var sessionFactory = BootStrapper.CreateSessionFactory();
   5:    using (var session = sessionFactory.OpenSession())
   6:    {
   7:      // retreive all stores and display them
   8:      using (session.BeginTransaction())
   9:      {
  10:        var links = session.CreateCriteria(typeof(WebLink)).List<WebLink>();
  11:   
  12:        //make sure there are links
  13:        Assert.GreaterThan(links.Count, 0);
  14:      }
  15:    }
  16:  }

 

As you can see, I am using a Unit Test (MBUnit) in that last snippet. I do most spike & throw away code like that these days. I used to use a console app but find that too crude anymore. (my opinion only)

Really, though, that is about all there is to it. I was planning on trying to get it working with Firebird db but SQL Lite will do the job. I might try to figure out hot to do it with Firebird  anyway just to help out the FNH project.

 
June 24, 2009 07:00 by josh
E-mail | Permalink
blog comments powered by Disqus

Comments



about josh

another programmer blogging about his misadventures in writing code.

Contact

contact us for website & software consulting

Decide

decide on pragmatic solutions

Develop

develop your product together

Succeed

achieve your goals with our services