Hi Folks! In this post I am going to talk about the NHibernate providers for the .NET BlogEngine I built a few days ago. My goal is to publish a stable version of it as a NuGet Package soon.
The code base comes from the official 126.96.36.199 version
What is inside?
First thing you should know is that there's a separate assembly called BlogEngine.Core.NHibernate. So whenever the official source-code changes, I can easily merge and keep everything consistent. The new assembly then uses Entity Framework as the ORM.
Next thing is the POCO classes under BlogEngine.Core.NHibernate.Models namespace: although the official version contains models designed for the Repository feature, they were not specifically designed for ORM. So I had to create them representing each of the database tables.
The third thing is the Hub class and the Fluent NHibernate Mapping classes. Because the new models act as a separate layer between the database tables and the business classes (BlogEngine.Core.Post, BlogEngine.Core.Page, BlogEngine.Core.Blog, etc.), the Hub class must convert them back and forth, whenever it needs to save or retrieve information.
Some classes contain internal objects, so I used reflection to access them.
I needed to add the FluentNHibernate, instead of using the HBM files and created a separate Mapping namespace with the classes and their database definition.
Last, but not least, the providers themselves under the BlogEngine.Core.NHibernate.Providers namespace:
- the NhBlogProvider, saving/deleting/retrieving blog information
- the NhMembershipProvider, taking care of user information and authentication
- and the NhRoleProvider, taking care of user authorization
How to install the files manually
The NHibernate ORM was originally designed to work with Java and was later ported to the .NET world, so there's no need to download separate ADO.NET providers from vendors, since the ORM is self-suficient in that matter.
Make sure you are running at least the 188.8.131.52 version of the .NET BlogEngine (otherwise the providers might not work)
Make sure you have the right database tables in your server
- Compile the source-code and copy the BlogEngine.Core.NHibernate and the NHibernate files (NHibernate, FluentNhibernate and Iesi collections) into the bin directory of your website running the .NET BlogEngine
- Edit the Web.config and create a connectionstring for your database called BlogEngine, but don't forget to specify the provider name or EF won't be able to connect with the database
- Edit the Web.config, adding the 3 providers and setting them as default (or use only the Nhibernate blog provider, if you prefer)
The easiest way to understand how the providers work is to download the source code and run the website. First, you need to create a MySQL database and configure the connectionstring (there are database scripts inside the setup website dir). Well, and this is pretty much it!
Hope you guys enjoyed it. See ya!