Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Posts Tagged ‘AR Series

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