Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Nhibernate .NET Providers

0.00/5 (No votes)
2 Apr 2009 1  
NHibernate based Profile, Membership, Role Providers written in C#

Introduction

Hibernate is a large scale project which covers 95 percent of common data persistence related programming tasks about SQL and database. We can call hibernate as one upper level of database(OULOD). I am kidding, we don't need any more terminologic and cryptic acronyms for making our documents hard to read as hell.

NHibernate is the port of Hibernate to .NET world. I don't know what developers think about ORMs after LINQ which makes these kind of things very very easy and neat. Well this article will introduce you to nice and dandy nhibernate and implements .NET membership, role, and profile providers.

About a week ago, I saw this project. It did not have ProfileProvider and I started digging and writing my implementation of ProfileProvider. It did not tested under heavy load. Just a starting point.

Background

You don't have to read it (because I don't believe reading documents takes you anywhere). But if you really wonder what this hibernate is, you can read it here.

Class Diagram

As you can see, it's a pretty simple and basic nhibernate project. The main provider inherited classes are:

  1. NHibernateMembershipProvider: handles the membership provider related tasks from abstracted level.
  2. NHibernateProfileProvider: I implemented this from real sqlprovider decompiled classes. I have not tested every aspect of this class. Please do that and send me bugs.
  3. NHibernateRoleProvider: Done the role provider implementation for .NET 2.0 class.

class diagram of the nhibernate providers

Using the Provider

Step 1: You need to make your database ready. I use mysql for this article and the code tested under mysql. Create a database named nhibernateprovidertest.

Step 2: Run the script which comes with the zip file named NHibernateProviderDataModel - mysql.ddl under NHibernateProviderDatabase\Create Scripts. I know this can be tricky. Even I get errors and swear the GUI tools. But it's fast and open source database and I like it.

Step 3: Open NHibernateProvider.sln with VS.NET 2008. and find web.config file and change the connection string at line:140 to point to your mysql server.

That is all. Now you have nhibernate which works on most famous databases (in theory). Click on the "Play" button. :)

Database Design

I must say that it was already designed for me. I just added one more table to store profile data. We have 7 table names of those:

  1. ApplicationUserRole: Connection table for user and roles. Holds just application id, user id and role id
  2. ApplicationUser: Contains which user belongs to which application. Holds application id and user id
  3. ApplicationRole: Contains which role belongs to which application. Holds application id and role id
  4. Role
  5. MembershipUser: Holds initial user data
  6. Application
  7. Profiles: columns:
    1. PropertyNames
    2. PropertyValuesString
    3. LastActivityDate
    4. PropertyValuesBinary

nhibernate providers

First, I was afraid for there was no connection between profiles and any other table. But then I thought that there are a lot of connections between other tables for more work. If I put any more connections between profile and any table, this means database has to check foreign keys when any delete or insert appears.

Points of Interest

If you develop any nhibernate project, don't forget to embed your mapping files into build. Otherwise you will read a lot of forum entry and solving this took longer then you would expect. Just select your mapping file "any.hbm.xml" and press F4 to open properties dialog and select "Embedded Resource" in "Build Action" line.

I have not used nhibernate in any of my projects. With this tool, I plan to support any database as much as I can. This article and code was a start. My next target is SQLite. Don't hesitate to join me. I am open to any suggestions/testing/debugging.

History

  • 2009-04-01 First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here