Archive for November 2008

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 http://support.castleproject.org/projects/MR/issues/view/MR-ISSUE-504

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?

Setting class attributes: [ActiveRecord] on class, at less on [PrimaryKey] attribute apply on class property and the rest simply [Property]

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.

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?

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.

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