Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Archive for November 2008

MR, Ajax & JSGenerator

with one comment

A friend stabbed and asked me

How do I control ajax delete scenario where I want to remove table row if deleted is successful otherwise I will alert user that they can’t delete this particular record?

Sound very familiar right? The beauty of MR is it leverage your existing web development skills, and just believe me when I tell you so.

Now let take a look at how MR JSGenerator would solve the problem described above, let see our action code:

public void AjaxTest(int id){
if (id < 0) { PropertyBag["err"] = "Woop! what are you talking about?"; } // for some reason I had to flow the code down PropertyBag["id"] = id; } ... [/sourcecode] Now the view part (*.brailjs) [sourcecode language='javascript'] if IsDefined("err"): page.Alert(err) else: page.Alert("Id = " + id) end [/sourcecode] No hack no tweak but what about the result? well it safe to try that yourself at home. Is there any JSGenerator in ASP.NET MVC? I doubt that. Edit: I suppose to be able to do cooler code, but this bug prevented me

Written by Chorn Sokun

November 7, 2008 at 11:15 am

Posted in MonoRail, Tips & Tricks

Tagged with ,

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

MR, View & Controller

with 4 comments

Before I walk my fellower into the Castle I do a quick introduction on MVC design pattern everyone seem to get a pretty good idea how each part (Model-View-Controller) suppose to works separately and in group.

After get into Castle MonoRail everyone asked me why do MR always require a view (*.brail, *.vm) for each action? The answer was by convention you need to have a view for an action to land. However you don’t need to have 10 views for 10 actions, you can programmatically choice which view to land your action. 

But how? let take a look at CreateUser action

public void CreateUser([ARDataBind("u", Validate = true)] User user){
  if (HasValidationError(user)){
    // you might want to tell end-user what errors are
    Flash["u"] = GetSummaryError(user);

    // with ability to make correction without re-fill the entire form right?
    // here what the magic
    // happy day, user can safely create

Line #09: it tells MR to use add.brail view template instead of landing on its default view create.brail 
Line #15: again if you don’t want to to land just fly to somewhere else; no one gonna complain you about that ;)

isn’t that cool? Until next time …

Written by Chorn Sokun

November 3, 2008 at 11:07 am

Posted in MonoRail, Tips & Tricks

Tagged with