Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘NHibernate

Ubuntu (NHibernate Mono MySQL) Take 2

with one comment

In my previous post I am talking about the setting mono, Nhibernate and MySQL stack on Ubuntu, it was a fun experience. However I made a mistake since I didn’t intend seriously use mono at the time I just wipe the whole os off my laptop and boom all the sample code and assembly gone too. But guess what this time I did it again with the combination of

  • Nhibernate 3 (beta build from trunk)
  • ConfORM (just so I don’t have to worry about .hbm.xml files)
  • MySQL driver install via Synaptic Package Manager (cuz I’m a bit lazy this time)

Now see what I got:

SchemaExport in Action ;)

And of cause I do make the sample code & assembly accessible at .

Written by Chorn Sokun

October 19, 2010 at 9:43 pm

Posted in Mono 2.6, NHibernate, OSS, Ubuntu

Tagged with ,

NHibernate + Mono + MySQL Fly by

with 7 comments

I thought about it and I want to see it works for me and there it goes ;)

  • Ubuntu Karmic
  • Mono 2.4.3
  • NHibernate (trunk)
  • MySql connector-net (trunk)

rebuild the stack for new home ;)

Sweet :) next rediscover Castle stack on Mono that should be fun let see how much I stress MonoDevelop 2.2

Written by Chorn Sokun

November 5, 2009 at 5:11 pm

Posted in Brainstorm, OSS, Ubuntu

Tagged with , ,


with 3 comments

Ayende’s Rhino-Tools become the most wanted library for my next project, consuming open source library with lack of walkthrough sample/enough test suite is always a painful experience “at the beginning”. It makes me stress for sometime as I can’t find any peer near by to share the experience with and yeah I tell you if you’ve got weak heart and usually depend on external person to walk you through the percentage of success is very low.

“No Pain, No Gain”, although the OSS’s troop are miles aways I’ll keep standing still until my last breath and hopefully, hopefully the support troop would come along.

So on the next day(s) I found the following post appear on the net which help me stand again.

Now I know how to spend my next weekend, thanks to all the good works and the sharing.

Written by Chorn Sokun

June 3, 2008 at 10:47 am

Convert IList to List<T>

with 7 comments

Follow up with my NHibernate Series today I have another post talking about one of my favorite Util’s method.

This come from my heavy use of NHibernate classes such as DetachedCriteria, IMultiCriteria and I love to call on it .SetResultTransformer method to transform result into strong type; however its .List() method return an IList which doesn’t uncomforted me much because I addicted to generic & strong type object.

public static IList<T> ConvertToListOf<T>(IList iList)
  IList<T> result = new List<T>();
  foreach (T value in iList)

  return result;

With this little method it allow me to work with generic type again and that is sweet ;)

EDIT: hmm, I just discovered something interesting actually I don’t really need this at all I can just manipulate iList element through foreach().

namespace ConsoleApplication1
  using System;
  using System.Collections;
  using System.Collections.Generic;
  using System.Diagnostics;

  internal class Program
    private static void Main(string[] args)
      IList x = new ArrayList();

      for(int i = 0; i < 100000; i++)
        x.Add(new Person("First #" + i, "Last #" + i));

      Stopwatch watch1 = new Stopwatch();
      foreach(Person person in x)

      Stopwatch watch2 = new Stopwatch();
      IList<Person> persons = ConvertToListOf<Person>(x);
      foreach(Person person in persons)


      Console.WriteLine("Watch #1: {0}\r\nWatch #2: {1}", watch1.Elapsed, watch2.Elapsed);

    public static IList<T> ConvertToListOf<T>(IList iList)
      IList<T> result = new List<T>();
      foreach(T value in iList)

      return result;

  public class Person
    public string FirstName;
    public string LastName;

    public Person(string firstName, string lastName)
      FirstName = firstName;
      LastName = lastName;

However it come down to an interesting measurement issue
* Pass through *
Watch #1: 00:00:00.0075761
Watch #2: 00:00:00.0131754

* Pass through & read field *
Watch #1: 00:00:06.2585406 (process direct)
Watch #2: 00:00:06.3542894 (convert first)

Written by Chorn Sokun

May 27, 2008 at 10:47 am

Many to many lazzziii

leave a comment »

I scratched my head for a few hours reading between the line of the following code

DetachedCriteria filter = DetachedCriteria.For(typeof(Project))
  .Add(Expression.Eq("ID", pid))

DetachedCriteria q = DetachedCriteria.For(typeof(List_Output), "op")

ISession s = holder.CreateSession(typeof(List_Output));
IList result;

  result = q.GetExecutableCriteria(s).List();

Wonder why It generate so many sql call to the server? the domain objects in discussion are Project which belong to a Type and base on selected Type a project can choices a bunch of Outputs.
So Project is belong to a Type [One-to-Many], and Type has many Outputs where Output belong to Many Types [Many-to-Many].

After a while I found the issue and got it fixed “it is the Lazzzzzzzziiiiiiiiiiiiiiiines” configuration :))
I forgot to tell Type to load Outputs in a Lazy mode and vise versa, once configured I got only one YEAH ! I mean ONE (a single) sql call to the server instead of 2 + N (where N is number of relevant Outputs in a Type)

Thanks to DetachedCriteria & the Laziness until next time don’t forget the magic word is “Lazy = true”

Written by Chorn Sokun

April 23, 2008 at 3:22 pm

Let do Projection

leave a comment »

This would be a continue part from my previous post, what I tried to do is find out how to use ActiveRecord “NHiberate to be accurate” in my reporting scenario (not a CRUD) after a quick search, It came up with a few blog posts

It will take another post for me to wrap up how I come up with …. stay tune.

Edit: I shouldn’t have missed, exploring test case

Written by Chorn Sokun

January 17, 2008 at 3:55 pm

IList you got me?

with 2 comments

As much as I hate to deal with numerical index I had to cross it one way or the other so be it.
Here is the fact, I am enjoy using Castle.ActiveRecord to wrap my DAL code cuz it simplify thing a lot for me > 80% somehow when it came to not so complex scenario like this one:

I want to allow my customer to list all the products and set the qty they want to order.
If they already order I would like to give them a chance to update qty for the product that they miss in the previous hit.

public IList LoadProductOrderList(int customer_id)
  IList result;

  ISession session = holder.CreateSession(typeof(AnnualTarget));
    // note: code wrapped for view
    string sql =  @"SELECT,, o.qty, as order_id
        FROM Products p JOIN Orders o
        ON = o.product_id  WHERE o.customer_id = :c1
        SELCT pid, name, 0 as qty, 0 order_id FROM Products";

    result = session.CreateSQLQuery(sql)
        .AddScalar("ID", NHibernateUtil.Int32)
        .AddScalar("NAME", NHibernateUtil.String)
        .AddScalar("QTY", NHibernateUtil.Int32)
        .AddScalar("ORDER_ID", NHibernateUtil.Int32)
        .SetInt32("c1", customer_id)

    } catch (Exception ex) {
    } finally {
    return result; // this is an IList ?!!! I don't like to work with it

As much as I hate, it is a bad idea to pass IList back to the caller object. What is interesting about this code session.CreateSQLQuery(sql) you can tell it to map the result to an Entity by call like this


But anyway I have got time to dig what AddEntity would do with the object pass in to me the road a head is a bit dark. Can I have something more simple?

The simple should be:

public IList LoadProductOrderList(int customer_id)
    IList betterResult = new List();
    for(int i = 0; i

Now it much better to work with

Written by Chorn Sokun

December 5, 2007 at 3:40 pm

Posted in Castle.ActiveRecord

Tagged with