2009-01-22

NHibernate Provider for Dynamic Data - Part 1

Dynamic Data supports LINQ To SQL and Entity Framework. There is also a provider for LLBLGenLLBLGen.

As a fan of it, I plan to plug NHibernate and Dynamic Data.

Is it possible ?
A read a lot of articles on how to plug an ORM tool and Dynamic Data. There is two points that your ORM tool must provide :
- A DataContext (Or ObjectContext) to provide a working session
- Linq support
- A Datasource control which has SelectParameters support (FransBouma)

Since last week, NHibernate.Linq Alpha is released. So we've got Linq Support and a new object NHibernateContext which could be our DataContext Object.
So I need two components :
- The Model Provider which will provide NHibernate methods to Dynamic Data
- The Datasource control

I've created a basic NHibernate solution to test my provider :



Here is a short description :
- Entities : The entity class, with NHibernate mapping files. I've generated it with Mygeneration soft. Basing on the NHibernate.Linq examples, I've build my context class (with only one entity in it for the moment) :
public class WineNotesContext : NHibernateContext
{
public WineNotesContext(ISession oSession) : base(oSession)
{
}


public IOrderedQueryable<DtCountry> Countries
{
get { return Session.Linq<DtCountry>(); }
}

}

- NHibDynamicData.Provider : This is the project where I wrote my NHibernate Model Provider.
- NHibDynamicData.Controls : This is the project where I wrote my NHibernate DataSource.
- TestLinqDD : A standard Linq To SQL Dynamic Data Website.

The Model Provider
Based on the SimpleModelProvider from dynamic data release. You just have to include it on your global.asax on your dynamic data web site :
model.RegisterContext(
new NHibModelProvider(typeof(WineNotesContext)), new ContextConfiguration() { ScaffoldAllTables = false });


The DataSource Control
Based on the LinqDataSource control. You have to replace standard LinqDataSource on your dynamic data pages by this control.

Customizing your entities
You just have to use attributes. Here is the example of country entity :

[ScaffoldTable(true)]
[
DisplayName("Country")]
[
DisplayColumn("CtrName")]
public class DtCountry
{

The entity will be used by the model (scaffold attribute) and will be shown by its display name. The same attributes are existing for each columns.

There are problems !!!
...but I will fix it on next part of this article :
- Deleting problems
- Showing and updating foreign keys
- Primary key detection problem, if it is not detected, MetaTable will be still on read-only mode !

You can checkout this idea here : http://speezo.unfuddle.com/svn/speezo_nhibdynamicdata/
kick it on DotNetKicks.com

2 comments:

  1. Hi, Guillaume!
    Any luck on this issue? I'd love to hear more.
    Thanks,
    Ricardo Peres

    ReplyDelete
  2. I have implemented my own provider. Still in beta, but you can get it from http://weblogs.asp.net/ricardoperes/archive/2010/01/03/asp-net-dynamic-data-nhibernate-provider.aspx

    ReplyDelete