• Selecting Aggregate Data With Castle.ActiveRecord

    Lets say your using Castle’s ActiveRecord and you have a complex query to aggregate some data, but you want that query to return the data in a typed class, just not one that NHibernate or ActiveRecord know about…

    Here’s what I ended up doing. Creating the SQL query then using the HqlBasedQuery class. There are two important details regarding this, first the query needs to name the columns using the SQL as keyword and setting the Scalar definition. Second, you need to set a transformer using the SetResultTransformer method. Luckily, NHibernate includes a class that will map named scalars to matching properties or fields. More about that here, in section 14.1.5.

    Here’s the code:

    var sql = @“
    select convert(varchar, published, 101) as [Date],
    count(state) as [Total],
    sum(case when state = 2 then 1 else 0 end) as [Responded]
    from notices n
    inner join sources s on n.sourceid = s.id and s.userid = :userid
    where published >= dateadd(day, -7, getdate())
    group by convert(varchar, published, 101)”;</p>

    HqlBasedQuery q = new HqlBasedQuery(typeof (Notice), QueryLanguage.Sql, sql); q.AddSqlScalarDefinition(NHibernateUtil.String, “Date”); q.AddSqlScalarDefinition(NHibernateUtil.Int32, “Total”); q.AddSqlScalarDefinition(NHibernateUtil.Int32, “Responded”); q.SetParameter(“userid”, Id); q.SetResultTransformer(Transformers.AliasToBean(typeof(NoticeCount)));

    var results = ActiveRecordMediator.ExecuteQuery(q) as ArrayList; if (results == null)

    <span class="kwrd">return</span> <span class="kwrd">new</span> NoticeCounts();

    var counts = results.ToArray(typeof (NoticeCount)) as NoticeCount[];
    return new NoticeCounts { Counts = counts };

  • IDictionary and the Brail View Engine

    I spent a lot of time trying to figure out that the brail engine assumes IList on indexed properties. If you have a IDictionary you need to use the get_Item method like:




  • Changes Needed for Castle.MonoRail to use jQuery

    To get jQuery as the JS library for monorail I need to do the following:

    1. Get the JS files from jQuery and jQuery.Validation, save those to /Content/Js/ (or where ever).

    2. Implement the IMonoRailContainerEvents and IMonoRailConfigurationEvents interfaces on my GlobalApplication.cs file.

    3. In the Initialized method add:

    IAjaxProxyGenerator ajaxProxyGenerator = new JQueryAjaxProxyGenerator();
    container.ServiceInitializer.Initialize(ajaxProxyGenerator, container);
    container.AjaxProxyGenerator = ajaxProxyGenerator;
    1. In the Configure method add:
    configuration.JSGeneratorConfiguration.AddLibrary("jquery-1.2.1", typeof(JQueryGenerator))
    1. In the layout file reference the JS files like:
    <script language="javascript" type="text/javascript" src="${siteRoot}/Content/Js/jquery-1.2.6.min.js"></script>
    <script language="javascript" type="text/javascript" src="${siteRoot}/Content/Js/jquery.validate.min.js"></script>


    1. Use the Castle trunk source.






  • Publishing video as FLV using a SWF Player

    This task turned out to be much more complicated then I would have expected. First just creating the video took a fair amount of time, trial, and error. I used CamStudio to capture the screen video and then the trial of Video Editor 8.

    After that was done I ended up using Any Video Converter to convert the file to FLV. Then I needed a SWF player to embed in the web page. I looked at a couple of options, but ended up with OS Flv. Once that was done and working, I ran into one last issue, resolved here. IIS doesn’t serve FLV files by default, so I needed to add the mime type.

  • Awesome Child Bike Seat – iBert [off topic]

    This is off topic. We recently started using an iBert child seat to take our son on bike rides with us. It’s a front mounted seat, which makes it really easy to control the bike, plus the child gets a better view. Our son loves it and even puts up with a helmet on it.  You need to make sure it will mount on your bike, mountain and road bikes may not work. It requires an inch or two of open stem space on the neck of the bike. I think most hybrid bikes will work, but make sure you double check. We bought it from amazon and so far we’re all loving it!

  • Get the selected radio button value using prototype

    Assuming you have a set of radio buttons with the same id, you can use the following to get the value of the selected/checked button.

    var value = null;
    $$(‘#plan_plan’).each( function(i) {</p>
    <span class="kwrd">if</span> ($F(i)) { value = $F(i); }

    } );

  • Not using Linq to it’s fullest can produce the worst of both worlds

    I’m parsing some XML and need to do one thing if there are a group of XML elements with the same name and another thing if just one element with a given name.

    Here my first attempt, yuck!

    XElement on = null;
    foreach (XElement element in elements.OrderBy(x => x.Name.LocalName))
    var current = element;
    <span class="kwrd">if</span> (elements.Count(x => current.Name.LocalName.Equals(x.Name.LocalName)) > 1)
        <span class="kwrd">if</span> (on == <span class="kwrd">null</span> || !current.Name.LocalName.Equals(on.Name.LocalName))
            <span class="kwrd">yield</span> <span class="kwrd">return</span> <span class="kwrd">new</span> JProperty(current.Name.LocalName,
                                       <span class="kwrd">new</span> JArray(
                                        elements.Where(x => current.Name.LocalName.Equals(x.Name.LocalName)).Select(
                                            x => <span class="kwrd">new</span> JValue(x.Value))));
            on = current;
    <span class="kwrd">else</span>
        <span class="kwrd">yield</span> <span class="kwrd">return</span> FromElement(element);


    That's really bad, Linq is probably making the code worse. After some refactoring...

    foreach (var group in from e in elements group e by e.Name.LocalName)
        if (group.Count() > 1)
            yield return new JProperty(group.First().Name.LocalName,
                new JArray(from x in @group select new JValue(x.Value)));
            yield return FromElement(group.First());

    The code here is easier to read shorter and clearer. Much better.