Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

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.

Conclusion

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?

 

Advertisements

Written by Chorn Sokun

November 4, 2008 at 8:10 am

7 Responses

Subscribe to comments with RSS.

  1. I do agree on what you said MS has invested money in wrong time for EF. AR is a great tool that built on top of NHibernate, but I still using NHibernate for my choice because it provides transparent persistence for POCO, so I can use my domain object with my legacy application that uses iBatis.NET as persistence layer.

    Samnang

    November 4, 2008 at 3:46 pm

  2. great to hear, one question though how do you usually validate your domain object?

    Chorn Sokun

    November 4, 2008 at 3:51 pm

  3. Currently, we don’t do much for validation. We are done it at client side and service layer, but we do it by hand. I’m not sure which frameworks that am going to use, but I will try NHibernate.Validator.

    Samnang

    November 5, 2008 at 9:13 am

  4. If you take MR you can code happily on the expense of Castle.MonoRail.ActiveRecordSupport for server side, client side you got options prototype form validation, jQuery and so on

    It does help 80% where 20% we need to handle base on our complex business cases.

    Chorn Sokun

    November 5, 2008 at 2:24 pm

  5. When you’re working on AR, do you manage DB by hand or using SchemaExport? if SchemaExport, how do you keep versioning of your DB?

    Samnang

    November 7, 2008 at 10:05 am

  6. good catch :) I am not yet lucky to be able to run schema diff but for green field application I did use SchemaExport.

    The way I did it 1) SetupController with Install, Uninstall, Upgrade action. 2) setup some convention at least for myself to store *.sql script in a specific location with proper naming. 3) block direct access to *.sql script from web.config. 4) depend on where you launching the app + your permission -> you may or may not execute SetupController.

    So it pretty easy hack to get the functionality. Do you happen to have a better tools/solution to smooth this operations?

    Chorn Sokun

    November 7, 2008 at 10:16 am

  7. I use RedGate SQL Compare Pro to keep versioning my DB schema in script files and put in VCS. On release day, we just check out from VCS, then compare and synchronize to production DB. Sometimes we need some data synchronization, so SQL Data Compare could help.

    Samnang

    November 7, 2008 at 4:02 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: