Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Archive for the ‘Castle.ActiveRecord’ Category

Storm the Castle in Action

with 2 comments

felt bore today and decided to tune into studio and record some stuff :D hope you enjoy it.

I know, I know the quality is bad but I don’t know I am not do much with youtube so it the luck I get :) BUT as always orginal video can be download here.

Source code:

Written by Chorn Sokun

October 23, 2009 at 5:50 pm

(N)How simple could that be?

with 2 comments


Wonder how this report get produced from model above it? If you guess was

  • NHibernate
  • MR (did you say <table>?)

You right and I must say I finally master the NH Projection skill or in another word (stealling from JXII) level up!.

What I did by taking AreaCode as primitive data connect it through and transform set of progress records into columns pretty cool trick, no NH hack is required it all natural.

Happy Projections day

Written by Chorn Sokun

May 29, 2009 at 12:19 pm

Storm The Castle

with one comment

I hope Ayende won’t mind if I steal the name :D well, he did used that name for one of his sample project. Anyway let me throw some good news out to all of my blog trackers “who loves coding :D” I just check-in a sample project using Castle minutes ago.

As I am not sure how many of you understand what I am blogging about or even had trouble try to replicate my trip. Now I make it simpler if you want to catch-up or debate with me on my future Castle posts you got a chance to start it  now just go grab a stick and stone from

If you have any question/suggestion/request you can post it on the project site I am happy to help it out as time allow ;)

Oh by the way this is my 100 posts :))

Happy Chinese New Year everyone.

Written by Chorn Sokun

January 24, 2009 at 11:59 am

Missing AR

with 5 comments

Part of my new year resolution was to get rid off AR from my application architecture design replace it with pure NHibernate. But since AR had been my tool of choice for almost a year or so it just keep haunting me every now and then.

I believe the feature of AR that I am going to talk about is pretty old but somehow useful for certain situation; so let see.

Say you have to deal with User & User’s Roles scenario. A user can play many roles isn’t she? Now pretend you are a lazy developer / no because your DBA was lazy to create a Role, UserRoles table for you :) I know, I know I don’t think this will happen in the real world, your DBA can’t be that naughty :D but anyway one in a while if you were like me learn and love IPrincipal interface everytime I setup a User class you know that the IsInRole(string) will smile at you if it had to deal with Role stored as array of string.

public class User {

   public virtual int Id { get; set; }

   public virtual string Name { get; set; }

   public virtual string[] Roles { get; set; }


No hack, No tweak simply sweet how can’t I miss the AR ;)

Written by Chorn Sokun

January 6, 2009 at 5:58 pm

AR, Wrap-up

with 7 comments

Castle ActiveRecord (AR) – is a .NET implementation of ActiveRecord pattern. The ActiveRecord pattern consists on instance methods and properties representing rows in the database, and static methods acting on all rows.

Knew how AR is capable of and read through available document I say it far more effort need to put on making the features more accessible; but still it present form (document) are just good enough to attract new comer with some basic scenario. Saying all thses mean I am not gonna repeat what is already there. What I am trying to do now is to give you a quick overview on what AR can do for you and why you should love it.

How to setup an AR class?

  1. Setting class attributes: [ActiveRecord] on class, at less on [PrimaryKey] attribute apply on class property and the rest simply [Property]
  2. Derive your class from one of these AR base classes: ActiveRecordBase<T>, ActiveRecordValidationBase<T>

Notice that non generic version is also available but I simply ignore it, or pretend it doesn’t exist :P

How to perform Create, Read, Update & Delete with AR?

Alright, depend on how your PrimaryKeyType is configured you may or may not call instance.Save() method. However once you set value for all required properties you can call instance.Create(). To read/retrieve data back you can call a static method of the class ARClass.Get(id) which will return an instance of ARClass. Once you got an instance you can make change to the properties value and then call instance.Update() to persist the change; or you can call instance.Delete() to delete the record off your table.

** Note that you might need to call SessionScope.Flush() in some scenario, but again it depend on how your session was configure as well.

How to filtering result?

Beyond basic data retrieval via static method such as Find, FindAll, FindOne. AR came with a varieties of query API namely

  • SimpleQuery<T>
  • ScalarQuery<T>
  • CountQuery
  • ScalarProjectionQuery<ARType, TResult>
  • ProjectionQuery<ARType>
  • ProjectionQuery<ARType, TResultItem>
  • or you can say NHibernate is the limited

These API are very handing when it comes to extracting data for reporting purposes. Last but not least, MultiCriteria is your best train for moving data back & forth between AppDomain to DBMS.


With such a great tool available off the shelf it shame for Microsoft not acquiring this stuff or even promote it but keep invest their money on new tool set which happen fail time & time again; but that is a political issue which I am not in a position to discuss :D How about you? what are your favorite AR query APIs?


Written by Chorn Sokun

November 4, 2008 at 8:10 am

Raising the Dark Army

with 5 comments

I never regret to step my feet into programming world back in 1997. My first experience using notepad to script out (HTML) website. I don’t remember how many times I skew up my brother’s computer just to figure out a thing no matter how hard he try to avoid me. 

Rewind for the last 10 years I keep pushing myself up step by step no matter how hard, stressful I got I never give up. 10 years? Yeah I know my brain deserved a major upgrade perhap replacement of RAM, CPU Squad Core or something :) But see it took me so long to get to this stage where I legendary programmers abroad take much shorter time to advance their skills.

You may compare my learning experience to a single pig raising in a big farm, the lack of physical interaction, challenge and guiding is the main source preventing it from fast growth. But that are all history today I am so happy, because I am no fall into those mind set and I am not alone, ladies & gentlemen the dark army is rising. 

Dark Army

Dark Army, taking on TDD & DDD

This is the 5th session that they all come together and learn .NET programming C# and I glad to become their instructor as much as I enjoy walking them through the learning curve. Although it is a programming course we don’t talk much about syntax sugar or debate about how it was designed because it doesn’t give much value in real world once it’s time to get down to business so what they need to learn are concepts and tool set for rapid application development.

I want to see some new face as well but it doesn’t seem to work out just yet :) but again we please to invite you to join our journey anytime just keep you eyes on our public calendar.

Let the spirit of sharing growth among Cambodian, let us all create an opportunity for our young generation to extend what we had discovered.

Written by Chorn Sokun

September 27, 2008 at 10:34 pm


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