FubuToDo – Part 3: Persistence

This is the third post in a series of posts I am writing on creating a simple “To-Do” application using FubuMVC. I’ve broken down this series into the following:

  1. Part 1: Conventions, Opinions and Bootstrapping
  2. Part 2: Forms, Controllers, Views, and jQuery
  3. Part 3: Persistence

Overview

I wanted to create a simple “To Do” application using FubuMVC. In my previous post, I showed the basics for creating forms, actions on controllers to respond to those forms, and how to work in some jQuery. I also explained how to render ViewModels as JSON output. This time I’m going to talk about the last piece of the puzzle: persistence.

Note:
I had originally planned on discussing validation in this post. After thinking about it a little more, I decided that due to the simple nature of the sample application I would defer that topic to a later post. I plan on creating some posts on more advanced topics like validation and behaviors whenever time allows.

Persistence

Up until now, our UI had been operating off of the FakeItemRepository and FakeUnitOfWork classes. The switch over to NHibernate wasn’t hard since everything was written against our interfaces. If you’re reading this, I’m assuming you know the basics for unit of work, repositories, NHibernate, and all of that. I just want to talk about the specifics on how to get everything to work with FubuMVC.

After I implemented our interfaces in an NHibernate-specific way, I created a registry to properly register everything with StructureMap:

NHibernateRegistry

/// <summary>/// Provides a registry mechanism for initializing all NHibernate implementations./// </summary>public class NHibernateRegistry : Registry{    /// <summary>    /// Initializes a new instance of the <see cref="NHibernateRegistry"/> class.    /// </summary>    public NHibernateRegistry()    {        var sessionFactory = Fluently.Configure()            .Database(MsSqlConfiguration.MsSql2005.ConnectionString(c => c.FromConnectionStringWithKey("FubuToDo")))            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ToDoItemMap>())            .BuildSessionFactory();

        For<IToDoItemRepository>().Use<ToDoItemRepository>();        ForSingletonOf<ISessionFactory>().Use(sessionFactory);        For<ISession>().Use(ctx => ctx.GetInstance<ISessionFactory>().OpenSession());        For<IUnitOfWork>().Use<UnitOfWork>();    }}


Then I modified our FubuStructureMapBootstrapper.BootstrapStructureMap() method:

BootstrapStructureMap()

/// <summary>/// Initializes the bootstrapping process./// </summary>public void BootstrapStructureMap(){    UrlContext.Reset();

    ObjectFactory.Initialize(x => x.AddRegistry(new NHibernateRegistry()));

    Bootstrap(ObjectFactory.Container, _routes);}


We add our connection string and we’re done!

Source Code

You can download the source for this part at: http://svn.joshua-arnold.com/fubutodo/tags/part3

Note: You will find a “db” folder with a batch file to create the database for you.

  • Share/Bookmark
This entry was posted in Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

2 Comments

  1. Posted February 5, 2010 at 7:33 pm | Permalink

    Hey Josh,

    Just so you know, you did not include the FluentNH or NH binaries in your svn trunk for part 3.
    I doesn’t build when you pull it down.

  2. Posted February 10, 2010 at 5:10 pm | Permalink

    @Ryan

    Thanks for the catch. I’ve updated the tags with the binaries and I’ve also fixed the submission issue.

2 Trackbacks

  1. [...] Part 3: Persistence [...]

  2. [...] Part 3: Persistence [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

You may use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>