Showing posts with label nhibernate. Show all posts
Showing posts with label nhibernate. Show all posts

NHibernate with csharp-sqlite embedded C# database

Following my [NH-2120] post on JIRA, I decided to make another post here, just in case I need it in the future. Maybe for instance my driver does not get accepted for whatever reason.

So what I have done is created a driver for the managed embedded SQLite C#/.NET re-implementation database called C#SQLite (see csharp-sqlite project) and have tested this with the latest versions of csharpsqlite and NHibernate (2.1.2.GA using the SQLiteDialect).

This database re-implementation is really bringing together a number of very attractive things, such as fully managed code, .NET 2.0 requirement only, multi-platform support via Mono, OK performance, embeddable, etc, etc. So download a copy of the latest database and ADO.NET provider first.

Then you need the NHibernate sources. Open them up and create a new driver file in NHibernate.Driver called CsharpSqlite.cs

Once the driver file (link 1, link 2) is included/pasted in the project, I had to recompile NHibernate and NHibernate.Bytecode.LinFu (you may want to recompile the bytecode provider of your own choice e.g. Castle). I'm using it with 2.1.2.GA currently and C#-SQLite version 3.6.22 (latest at the moment) without problems. EDIT: The very latest version (3.6.23) is broken, I'm investigating this and will submit a patch. In the mean time, use my 3.6.22 binary.

For your own project, the usual NHibernate libraries need to be referenced, consult NHibernate documentation for this. But make sure you include the assemblies "Community.CsharpSqlite" (SQLite managed implementation) and "Community.CsharpSqlite.SQLiteClient" (the ADO.NET provider), both available from noah.hart's csharp-sqlite webpage (EDIT: use my binary above!).

Some of these libraries (e.g. CSharp SQLite itself as well as the Client) appear to target .NET 3.5, but setting them all to .NET 2.0 caused no compilation errors and they worked just fine for me.

Enjoy!

NHibernate embedded managed C# database

I was looking for an embedded cross-platform managed database to use for a C# .NET 2.0 project of mine and I was surprised to find out how difficult it was to find one. After you read this post, I hope that you will have discovered a new and exciting choice.

In my search I encountered the following posts:

(sources: Embedded database for .NET SO question, C#/.NET single user database SO question, Good "mobile" database .NET database SO question, Observations on embedded databases by Ayende, as well as many others)

The major options available seemed not good enough for various reasons:

SQLite: It's basically a native library with a x86-only requirement.
SQL Server CE: It's not thread safe and hangs periodically.
VistaDB: Commercial database.
SharpHSQL: No NHibernate support as of yet.
Firebird: Windows only.

The list goes on. Basically all available options either lacked a NHibernate support, or were not cross platform, or required installation, etc.

So when I stumbled upon this little gem called C#Sqlite (csharp-sqlite) which is a C# re-implementation of the latest SQLite database, I thought that my search was over. Alas, this embedded, managed, cross-platform (.NET/Mono) database (which also has an OK performance) was not possible to use with NHibernate, due to the lack of a driver.

I have posted an NHibernate driver for CsharpSqlite [NH-2120], so there, now you can use NHibernate with this wonderfully compact database which fits the bill for all of the above requirements.

I hope you enjoy it!

NHibernate cheat sheet using Attributes

A few common mapping examples when using the NHibernate ORM with Attributes.

Class

This is a required attribute for all Entity classes:
[Class(NameType=typeof(Being),Table="Being",Lazy=false)]
public class Being ...

Id
All classes should specify a unique table identifier:
[Id(0, Name="Id", Column="Id")]
[Generator(1, Class="native")]
public long Id
{
get;
set;
}


Sub-classing (joined table per sub-class)
When extending a class you can use 3 types of subclassing that NHibernate recognises. For subclassing by creating a table per sub-class use:
[JoinedSubclass(NameType=typeof(BusinessContact),
ExtendsType=typeof(Being),
Table="BusinessContact", Lazy=false)]

public class BusinessContact : Being ...

In this case sub-classes must override the Id property:
[Key(Column = "Id")]
public override Id {
get { return base.Id; }
set { base.Id = value; }
}


One to One
Use this when a one-to-one association is required between two classes (in this case PrintingSettings is within a BulkPrintingJob class):
[Key(0, Column = "BulkPrintingJobId")]
[OneToOne(1,
ClassType = typeof(PrintingSettings),
Cascade = "save-update")]

public virtual PrintingSettings Settings
{
get;
set;
}


One to One v2
Similar to above but for same class types ('Address') e.g. linked lists. Set to unique and contained within another Address.
[ManyToOne(0, ClassType = typeof(Address),
Cascade = "none",
Unique = true)]

public Address PreviousAddress
{
get;
set;
}


One to Many
Typically used for holding multiple objects relating to an object. Example where a business contact has multiple email addresses:
[Bag(0, Cascade = "all-delete-orphan", Lazy = false)]
[Key(1, Column = "BusinessContactId")]
[OneToMany(2, ClassType = typeof(Email))]
public IList Emails { get; set; }

Many to Many (IList example)
An example where categories have accessors who can access these:
[Bag(0, Cascade = "none", Lazy = false)]
[Key(1, Column = "CategoryId")]
[ManyToMany(2, ClassType = typeof(Accessor))]
public IList Accessors
{
get;
set;
}


Many to Many (Array example)
Example where a group has group members (uses Arrays instead of ILists):
[Array(0, Cascade = "none")]
[Key(1, Column = "BeingGroupId")]
[Index(2)]
[ManyToMany(3, ClassType = typeof(Being),
Column = "_beingId")]

public Being[] GroupMembers
{
get;
set;
}


Many to One (& parent/child)
An example where a BeingGroup contains other BeingGroups.
[ManyToOne(0, ClassType = typeof(BeingGroup),
Cascade = "none")]

public BeingGroup Parent
{
get;
set;
}