Fluent Nhibernate and ASP.NET MVC

For those of you who follow our personal tweets (yeah we have to tweet more with our @itsletnl account), you probably noticed how we’re currently involved (or obsessed) by Fluent Nhibernate. Fluent Nhibernate FTW!

Itslet Fluent Nhibernate

Fluent Nhibernate uses poco mappings instead of XML mapping, like its big brother Nhibernate. There is no strong argument to use Fluent Nhibernate instead of Nhibernate, it is just a matter of taste to me. Although, Fluent Nhibernate can automap and autopersist your entities, based on convention, and Nhibernate cannot.

Getting started with Fluent Nhibernate is very simple. Just read their excellent getting started guide. It doesn’t make much sense to repeat that on this website with different entities.

But how do we get started with ASP.NET MVC? How do I connect with the Fluent Nhibernate session gracefully?

Handling sessions

I used the book of Jason Dentler to find out more. The most common pattern in web application is the session-per-request. We open the session when the request begins and close it when it ends.

Nhibernate has a contextual session feature which allows a session to be associated with some application specific scope (unit of work). You need to set the current_session_context_class property in the hibernate-configuration section of web.config. But as we use Fluent Nhibernate, we have to use the ‘ExposeConfiguration’ method.

Here is my Global.asax.cs. Check out line 45: .ExposeConfiguration(c => c.SetProperty(“current_session_context_class”, “thread_static”))’.

Now we can refer to that session in our controller, like this:

In the example the person is fetched from the database. But to make the app more production worthy, we would use dependency injection rather than directly access the current session. Also, we would not have exposed our connectionstring like that.
But that is a completely new blogpost.

Github quickstart

Here are some quick guidelines to get up and running with Github quickly.
This article assumes you are on Windows and use Visual Studio for coding, but it could very well be another OS or IDE.
Create an account first at Github.
Create a new repository.

Download and install Git here and install according to this guidelines.

Now, rightclick on your Visual Studio project folder and choose ‘Git Bash here’. We are going to generate SSH keys first.
$ ssh-keygen –C ‘barbarabarend@gmail.com’ –t rsa”
Accept all the defaults and don’t enter a passphrase.

now type:
$ cd ~/.ssh
$ vim id_rsa.pub

now copy all the contents of this file and paste it in the Github website under Account:

Now, navigate to your Visual Studio project folder. And type:

$ git config –global user.name "Barbara Barend"
$ git config –global user.email barbarabarend@gmail.com

Or whatever your name is.

$ git init
$ touch README
$ git add README
$ git commit -m ‘first commit’
$ git remote add MyRemoteRepo git@github.com:YOURGITHUBNAME/test.git
$ git push MyRemoteRepo master

Line 5 is one line!

Now the Readme file is placed in your Github repository.

So, at this point you verified that you can actually upload your codefiles to Github and it’s time to upload the whole project. But you probably want to ignore a few files and folders (e.g debug and bin and so on).
To exclude files from being posted to Github, you need to create a .gitignore file. Like this:

$ touch .gitignore

Now, edit this file with your favorite text editor and make it look like this:

# Other useful stuff

Just basically add what you don’t want to be included. Upload the .gitignore as well:

$ git add .gitignore
$ git commit -m ‘my ignored files’
$ git push MyRemoteRepo master

Okay, now push your Visual Studio Solution to Github:

$ git add .
$ git commit -m ‘the whole she-bang’
$ git push MyRemoteRepo master

Now what if you want to add existing code? Like the fabulous ShoeShoe app as discussed in the former blogpost?
Create a new folder for these files and start the Git bash.

$ git init
$ git remote add MyRemoteShoeRepo git@github.com:YOURGITHUBNAME/MyShoeShoes.git
$ git clone git@github.com:YOURGITHUBNAME/MyShoeShoes.git

So this was quick and dirty, but it does the trick.