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:
- Part 1: Conventions, Opinions and Bootstrapping
- Part 2: Forms, Controllers, Views, and jQuery
- 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.

2 Comments
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.
@Ryan
Thanks for the catch. I’ve updated the tags with the binaries and I’ve also fixed the submission issue.
2 Trackbacks
[...] Part 3: Persistence [...]
[...] Part 3: Persistence [...]
Post a Comment