(N)How simple could that be?

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
Myth of Cambodia Geography Identification Code
How many of you are familiar with Cambodia Geography Identification code. Do you know the myth behind it code assignment? Well, it’s all started like this:
- There are 4 levels of administration
- Province
- District
- Commune
- Village
- Commune
- District
- Province
- Each level add up 2 digits begin with Province 2 digits
- Identification code are stored in numberic form (Integer)
That mean valid village code length bewteen 7 and 8 for example code 24020406 would identified as:
- Village Name = Ou Ressey Loeu (24020406)
- In Commune = Ou Andoung (240204)
- In District = Sala Krau (2402)
- In Province = Pailin (24)
Now suppose these Geography represent in 4 separate tables (SQL Server for ghost shack!)

By knowing a village code (eg: 24020406) I want to extract province code and I want the quickest way possible any thought? Oh ! there will be a reward for best answer.
Rhino-Tools a smarter Guard
When Ayende typed Guard.Against(condition, error_message) it clicked in my brain. I thought wow that was nice and easy to use what it does was check if the condition is true it will throw and exception with error message specify. I had an idea using this litle beast for business validation, since it doesn’t required any special configuration all you have to do is adding reference to Rhino.Common.Clr.dll.
However the original Guard API only has only two static method Guard.Against() it was limited for web application. In web application we might want to report back all problems so user can make the correction before they re-submit the form again, but using Guard.Against() we only catch one problem at a time, if we have multiple codition to check you can imagine how annoy for web experience.
Ayende is kindly enough accepting my patch for two additional method for the Guard bellow:
- Guard.Check(condition, error_message) – same behavior as Guard.Against() but it won’t throw and exception instead keeping records of problems
- Guard.Report() – until .Report() get call and exception known as GuardSpotException will be throw.
Let see how I can improve my usage experience with these methods
try{
new Guard()
.Check(pay.Date.Equals(DateTime.MinValue), "Invalid payment date.")
.Check(pay.Amount == 0, "Invalid amount, should it be something other than zero?")
.Report();
// persist pay instance to the database
}
catch(GuardSpotException ex)
{
// list of errors message
PropertyBag["errors"] = ex.GetErrorSummary();
}
First I instantiate a new Guard object and start checking conditions (business rules) finally I call Guard.Report() if there is any true condition (= business rule voilated) and exception known as GuardSpotException will be throw you then can catch and examin all problems.
Isn’t that cool?
MonoRail & jQuery
Last year I blog about using jQuery.ajaxForm() to send http post request to MonoRail action and finally it depend on JSGenerator to produce javascript and send it back to client.
But overtime I find it pretty scary using that approach:
- It give too much control to the server – the server can generate a bunch of javascript and send off to the client sort of invade client responsibility.
- In some scenario I even specify from the server which div element I want to render my data table in
page.ReplaceHtml('#divId', "content_to_be_replace_from_server")
- using approach in previous post I was not be able to ensure 100% that if I send piece of jquery script that it combine into the main DOM properly for instance I did have a form & a jQuery to confirm the form into ajaxForm but that behavior is not always reliable.
So what the deal now? well, I happen to change the strategy a while ago just haven’t got time to blog about it the solution is pretty simple “Don’t make JSGenerator your new hammer.”, Let see my simplify approach:
<div id='dlist'> <!-- I am going to render list of data in this div --></div> <input type="button" value="Show data" onclick="showData();" /> <input type="button" value="Add more" onclick="addData();" /> <div id='dform' style='display: none;'> <!-- I am going to load form into this div --></div>
That pretty normal HTML markup right? 2 div one for display list of data second one for display form for adding more data and I also have 2 buttons to invoke client script (jQuery).
<script>
function showData(){
$('#dlist').load( '/task/list.rails', {'status': 'incomplete'} );
}
function addData(){
$('#dform').load( '/task/add.rails', {}, function(){
$('#dform').show();
});
}
</script>
Now that instead of using $.ajax I use $(‘#elementId’).load() instead because load() will inject html render by MonoRail action in the DOM and it just as native element.
Note : line 07 dform had style apply so that without having form inside it don’t occupy space in the page however one content loaded we need to set visibility for it.
For your reference the final piece of the puzzle is purely view code like this (View\task\add.brail)
<form method='post' id='taskForm' action='/task/create.rail'>
Name: <input type='text' name='t.Name' />
<input type='submit' value='Create' />
</form>
<!-- not inject some jQuery script along make the form submit via ajax -->
<script>
$(function(){
$('#taskForm').ajaxForm ({
success: function(response){
$('#dlist').html(response);
},
error: function (xhr, status, error) {} // handle error if you wish
});
});
</script>
Notice on line #09 : need the form to submit using ajax and line #11 the client take control where it want to place the result (list of task).
It seem like we have to write a bit more code (jQuery) but after all that the fun part of being a programmer write code that :) Hope this would help.
MonoRail vs ASP.NET MVC Take #1 HTTP verbs
Along came ASP.NET MVC although I am not the fan of ASP.NET MVC but it doens’t heard reading ASP.NET free chapter (nerddinner) and I did learn a few tricks, I will try my best to map concept between the two framework now let see
Why differentiate via HTTP verbs? read the book and find out, but here the deal
- ASP.NET MVC [AcceptVerbs(HttpVerbs.Post)]
- and [AccessibleThrough(Verb.Post)] in MonoRail
// GET: /Payroll/Delete/2
public void Delete([ARFetch("id"}] Payroll payroll){
if (payroll == null)
{
RenderView("InvalidRequest");
}
else
{
PropertyBag["payroll"] = payroll;
RenderView("DeleteConfirm");
}
}
// POST: /Payroll/Delete/2
[AccessibleThrough(Verb.Post)]
public void Delete([ARFetch("id"}] Payroll payroll, bool confirm){
if (payroll == null)
{
RenderView("InvalidRequest");
}
else
{
if(confirm){
..... // drop the record off the database
..... // do whatever you want.
}
else
{
RedirectToAction("Delete", "id" + payroll.Id);
}
}
}
MonoRail Rock!
4a friend in d’hospital
Vithou this is for you man I hope you recovered and return home safely soon; we miss you and for the record bellow there is an award waiting for you well enough a drink and #$@ as usual ;)

Ah! this app had Pheap’s finger print on it although he pretty young but he did a good job on this don’t worry you will not get suite for develop a lottery system :)) beware man.
Brail Daily Use Macros #2
Imagine you had to associate multiple skill to your “Profile”

Suppose we have the following Profile & Skill class
class Profile {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
//.... other properties as necessary
public virtual IList<Skill> Skills { get;set; }
}
// a lookup class per say
class Skill {
public virtual int Id { get;set; }
public virtual Profile { get; set; }
public virtual string Title { get; set; }
}
Assume that we know how to apply AR attribute on these two class make it persist able to a database. The important thing is Profile has many Skills that all you have to concentrate on right now.
Ok ! today recipe is to generate UI with checkbox which allow user to tick and save the choices without any hassle. With the help of FormHelper method YES, WE CAN
<%
list = Form.CreateCheckboxFieldList("p.Skills", skills, {@text:'Title', @value:'Id'})
// we need a loop
for item in list:
%>
${list.Item()} ${item.ToString()}
<%
end
%>
Don’t panic let me explain a bit ${list.Item()} will generate individual html checkbox where ${item.ToString()} will generate a label for each one.



