Thursday, December 4, 2008

Single Table Inheritance With LINQ

For a while now I've been using LINQ in my ASP.NET applications and it has been a huge time saver. Having used nHibernate prior to this, the prospect of not having to deal with config files was a double bonus. One thing I do miss from nHibernate, however is the ability to add a simple Where clause in the class definition. For example, I regularly use a deleted flag in tables so as to avoid doing 'hard' deletes. In the nHibernate world, in order to exclude deleted records the config file would use something like this:
<class name="Core.ELSField, Core" table="dbo.tblFields" Where="fDeleted=0">

I've yet to find anything in the LINQ world that equates to this so I'm left including 'where !deleted' in all my queries. Oh well.

Inheritance in nHibernate is handled through discriminator columns and looks like this:

<class name="IPayment" table="PAYMENT">
<id name="Id" type="Int64" column="PAYMENT_ID">
<generator class="native"/>
<discriminator column="PAYMENT_TYPE" type="String"/>
<property name="Amount" column="AMOUNT"/>
<subclass name="CreditCardPayment" discriminator-value="CREDIT">


For the longest time, I was coding the equivalent LINQ version by hand. Little did I know that the O/R designer in Visual Studio handles this. MSDN provides a very nice example which you should be able to apply to your own situation. Hopefully you will find it as useful as I did.

Monday, November 17, 2008

Using Grails to Store Generic Data Sets

TASK: Build a generic data structure to represent grid data, then use the Grails framework to implement it.

At a high level, what is needed is a set of tables that can hold sets of varying numbered rows and columns. This means we have collections of grids representing the individual data sets. Each grid has a collection of rows (we’ll call them results for this discussion) and data columns. Each result has a collection of data points. And finally, each datapoint is associated with a data column.

I’ve been using Grails now for about a month and I’ve found the documentation to be lacking in comparison with other frameworks that I’ve used. So hopefully this example of representing result sets will be usefull to others who, like myself, learn best from checking out example code and applying it to the task at hand. For the purposes of this example, I am using Grails 1.0.3, MySQL 5.0, NetBeans 6.5, and Navicat 8 Lite for MySQL (very helpful for setting up MySQL for those who aren't keen on command-line stuff). I won't get into the whole setup of the Grails plugin for NetBeans and I'll assume that you already have your MySQL database created. The tables will be automagically generated by Grails so all you need is an empty database.

First, the domain classes:

class DataRow {
int Id
int DataSetId
static hasMany = [results:DataPoint]

class DataPoint {
int Id
DataPointColumn Column
String Val


class DataPointColumn {
int Id
String ColumnName

And now the code to save your data. For this example, you will have some java structure that stores results rows and columns. I won't muddy the waters here with my implementation of that since this is supposed to be about Grails. Suffice it to say that you will need to have some mechanism of retrieving a collection of column names, and each data item should be associated with a column.
IResults res = <execute some query>
String[] column_names = res.getColumnNames()

for (Result r : res.getResults())
def savedResults = new DataRow()
savedResults.DataSetId = res.getId()

for (int i=0; i< column_names.length; i++)
def col = DataPointColumn.find("from DataPointColumn as dp where dp.columnName=?",[column_names[i]])
if (col==null){
col = new DataPointColumn()
col.ColumnName = column_names[i]
String val = r.getDatapointValue(column_names[i]);

def dp = new DataPoint()
dp.Val = val
dp.Column = col


} // Save the row

This is actually a slight modification to the actual code that I implemented so that my business-specific names and code are left out. The most interesting piece of the code (IMHO) is the addTo* function. There's actually a nice explanation of how that works on the Grails site (Grails: addTo*). If you absolutely must use java (I'm trying to hide my .NET bias here, really) Grails is a good MVC framework to use. Any changes to the domain model are automatically implemented in the database. One thing you will find usefull in tracking down errors is this code snippet:
(b is some domain object)

if( ! ) {
b.errors.each {
println it

As of right now, Grails does not have any built-in debugging support, which means that you are limmited to spitting out error descriptions. Hopefully someday it will be possible to step through a Grails app, but for the same price as this short example, you get a pretty slick development environment and framework.

Wednesday, November 12, 2008

PADR, PADL and PADC for SQL Server

Thanks to Igor Nikiforov for these very helpful UDFs. One of the biggest things I have on my wish list for SQL Server is more support for string functions. Here's the link...


Thursday, November 6, 2008

Can Anybody Learn To Sing?

In a prior life, before I discovered how cool it is to play with computers, I was a music teacher - arguably a pretty darn good one. Specifically, I was a voice teacher with a masters degree in Vocal Pedagogy from New England Conservatory of Music and an undergrad in Music Education. Fresh out of school I was convinced that I could teach *anybody* to sing. I had the theory, the technique and the practical experience. That, combined with a lack of money, fueled my desire to take on any and all who would fancy themselves a singer.

Yeah, about that...

To this day, I still cannot explain why there are some people who, knowing that they can't carry a tune in a handbasket, cling to the hope that they can learn to sing as easily as learning the fingerings on a piano. To borrow a phrase, "To those that have will be given more, and those that don't have - try gardening". Ok, I'm paraphrasing.

Here are my supporting arguments:

The voice is deeply connected to a person's identity.
For a person to even stand a chance at changing the way their voice sounds, they have to be able to acknowledge that there is something that needs to be changed. Right off the bat, that eliminates most who would endeavor through voice lessons. The simple reason is the diva/divo complex that brought them there in the first place.

There is no way to directly affect the mechanism by which we sing
Let me say that again: The musculature involved in making vocal sounds, for the most part, is beyond the level of conscious control. The task of the voice teacher is to provide an environment to which the vocal apparatus will reflexively respond. Because we are talking about muscle memorization, the process can take a very long time. It has been my experience that most would-be singers don't have the patience to stick it out. This is assuming that there are no external forces that would get in their way such as smoking, drinking, pregnancy, old age. And yes, I had at least a couple of students from each of these groups. Lovely people. No chance of singing.

The human lifespan is at best 120 years.
The length of time it would take a person with no ear to learn to sing is far longer than most would stick to it. Nuff said.

Damn it Jim, I'm a voice teacher, not a doctor...
Or something like that. There are some people who have physical characteristics which make it impossible for them to produce the kind of vocal sounds that they would hope to achieve. For example, I had one gentleman who actually had a lot of musical ability being a fairly accomplished guitarist. The problem was that he was also heavily into weight lifting and, being a rather short and stocky individual, had the sort of build that made it impossible to put his arms by his side. That is to say, his arms, when at rest, were always in a position that made him look like he was getting ready for an old-style western quick-draw shoot out. Because of that, he could never seem to relax enough to allow the vocal apparatus to operate freely.

That was just one case. Others either smoked so much that they could neither breathe properly nor overcome the swelling and irritation of the vocal folds to make a focused sound. Then there was one poor woman who was pregnant. Now let me be clear. Being a man, I have no direct insight as to what she was going through. But I can only imagine the discomfort she felt when I asked her to take a deep breath, knowing full well that the tiny human in her abdomen had no intention of allowing her mom's diaphragm to descend more than a couple of inches.

I welcome any and all arguments. Even though I've moved on professionally to other things, I still find the science of vocal pedagogy fascinating and enjoy discussing it.

Avoid Inserting Duplicate Records

Here's a quick solution to the problem where you don't want to insert duplicate records into a SQL Server table. Of course, the easier way to do this is to set up a constraint, but there are times when you can't do that. For example, if you have a table where you are using a deleted flag as opposed to doing hard deletes. If you have foreign keys to deal with, I would suggest creating a table variable (again, assuming you are using SQL Server) and inserting the denormalized records there, and then doing the insert as listed below. You could probably do it in one statement, but just because you *can* do something doesn't necessarily mean you should. The two step process would be easier to read and more clearly indicates the intent of the code. Comments are great, but code should be self-documenting.

SELECT field_1, field_2, field_3
SELECT DISTINCT B.comp_1, B.comp_2, B.comp_3
WHERE B.comp_1 = LU.comp_1 And
B.comp_2 = LU.comp_2 And
B.comp_3 = LU.comp_3

Hope this helps.

Saturday, October 25, 2008

So Long and Thanks for All the Sugar Water

If you are a bee keeper reading this then I'm sure you know where this post is headed. If not, let me briefly explain what happens when a bee colony decides it has outgrown its current facilities and wants to look elsewhere for other arrangements. 

When starting a hive, as my wife and I did this past summer, it is very important to check on the little critters at least once per week - which unfortunately my wife and I did *not* do this past summer. In our defense, we were very well intentioned but we just had way too much going on. Even though we had the benefit of attending 'bee school' provided by the local Bee Keepers Association (which I would highly recommend to anyone interested in getting started with their own apiary), I figured that even though they were a little neglected, they were being fed and seemed active so all was good. Wrong. I had only installed the lower hive with frames and had just recently finished assembling the next 10 frames to put in the second hive body but had not put it out yet. 

Well, one day in early June I came home from work and saw a frightening sight. Now, I knew there were a lot of bees in the hive - some 12 or 13,000 - but it wasn't until I saw most of them in a huge clump on the side of the hive that I really got the full effect. I was witnessing a swarm. Now, a swarm is not what the name has come to imply from horror movies. Rather, it just means that they had outgrown their current space and were ready to pack up and move. 

When bees swarm, they send out a small bunch of explorers to find a new location. When they find it, they signal back to the rest of the colony which is waiting patiently in a big ball and they relocate taking their queen with them. When this happens with bees in the woods or elsewhere, you might find them dangling from a tree limb. 

So now here I am faced with this swarm of bees. I had to work quickly, so I ran in the house to tell my wife and we jumped into the bee suits, opened up the hive and literally dumped the bees back into the hive. You would think that they would have just flown off at that point, but it seemed at the time that we had caught it just in time. 

Unfortunately there's not a happy ending to this story. Our hive did not survive the summer. While it seemed that through July and August the bees were reproducing and staying active, we never did find our queen. The only thing that we could think of is that either the queen had in fact flown off with a good portion of the colony, or somewhere along the way we (or the other bees) had killed her. We'll never know what really happened, but it turns out the bees were reproducing, but without a queen. We had a condition wherein the worker bees start laying eggs instead of the queen. Eventually the colony will die off when this occurs.

The upside to this whole thing (if one can be found) is that my wife and I learned the hard way some very useful stuff about bee keeping including how to check for a queen cell, the right way (and wrong way) to remove frames from the hive without squishing anybody along with the simple hands-on inspection of the hive. We plan on attending the bee school sessions again this winter and trying again in the spring with a new colony. At least now we've got all the equipment ready to go this time around. Here are some pictures of the hive taken prior to the mass exodus...

Thursday, October 23, 2008

Mvc Framework Model Binders

In the preview 5 version of the mvc framework they introduced the ModelBinder. The team at my company actually started a very similar implementation for use with nHibernate. Unfortunately, it - like the project - never really took off. So along comes the mvc framework and the ModelBinder. Preview 5 supports built in support for basic binding, but allows you to build your own custom binding. 

As I mentioned in a previous post, I'll be a little different and post this example using code. Truth to be told, I prefer C#, but there seems to be a real lack of examples so far. 

To add custom binding/serialization support for your custom class it needs to implement IModelBinder. There's only one required method to implement which is GetValue() in which you take the form from the controllerContext.HttpContext.Request object and map the fields to your class. Here's a sample:

<modelbinder(GetType(fedloanrule))> _

Partial Public Class FedLoanRule

  Implements IModelBinder

    Public Function GetValue(ByVal controllerContext As System.Web.Mvc.ControllerContext, ByVal modelName As String, ByVal modelType As System.Type, ByVal modelState As System.Web.Mvc.ModelStateDictionary) As Object
  Implements System.Web.Mvc.IModelBinder.GetValue

        Dim request As HttpRequestBase = controllerContext.HttpContext.Request
        Dim rule As FedLoanRule = New FedLoanRule()

        rule.LoanType = request("LoanType")
        rule.LateDisbInterval = Integer.Parse(request("LateDisbInterval"))
        rule.MaxCombinedDepLoan = Integer.Parse(request("MaxCombinedDepLoan"))
        rule.MaxCombinedGradLoan = Integer.Parse(request("MaxCombinedGradLoan"))
        Return rule

    End Function
End Class

When an instance of a FedLoanRule is passed into my controller action, GetValue is called first. This would be useful if you had some special mapping that had to be done since it takes the mapping code out of your save/update code. In this simple case it would seem that it would have been easier just to call TryUpdateModel(). But this is good for an example. 

When the code gets into the controller action, the updated object instance is there and ready to use. Here's the catch. As of the Preview 5 version (I say that because the beta was just released),  this only seems to be useful for creating new records. If you want to do an update, even though your object is bound to the form prior to reaching your controller action, once in the controller action the Model does not see that anything has been changed. You have to retrieve the object from the model, make your change and then you can do the update. If you just call SubmitChanges()  on the object that is passed in from GetValue(), nothing is submitted. 

While it's cool functionality, it doesn't seem to make a whole lot of sense to have separate methods for inserting new records - a scenerio in which the model binder does work - and for updating existing records. 

Hey, this is still preview code and I've yet to try out the beta. Somebody else brought up this point on ScottGu's blog. And he mentioned that it might be addressed in the beta. 

ASP.Net MVC Framework

We've been using the new mvc framework at my company since the Preview 2 version. For those who are keeping score the beta was released about a week ago. I've been fortunate to have been able to work on projects that have spanned the evolution of this framework and had to upgrade a couple so I thought I'd share some of the highlights.

Let me state for the purpose of full disclosure that I've only been doing web development for about a year and a half though I've been an application developer for over 10 years now. But it's been a very full year and a half as I've been very fortunate to work for a company that embraces new technology. 

First of all, the mvc framework, certainly not a new idea having been around for some time, is a relief from having to deal with web forms. One of the biggest headaches of trying to incorporate javascript, for example, in a web forms app was having to use the ClientId property of html elements and pass that id back to the server rather than just using the id that I assign. This illustrates what seems to be the key feature that one would look to when considering switching development frameworks. With web forms the focus is on the server processing. Anytime you want to do client side processing you'll more than likely have to do some work around / hack to get it to work. It's not so much that the mvc framework gives you new functionality that web forms doesn't. It's the fact that you don't have to fight tooth and nail to get simple things to work. 

For example, take any web forms application and view the source of a page. You'll find all the nasty looking element ids that are auto generated along with html that, if you were the developer, would leave you scratching your head wondering where it came from. Now take that same page written using the mvc framework and you'll see ... wait for it ... yep, pretty much the same html that the developer wrote. You have control of every single tag that is written. 

Here's another gem. You have a datagrid control and you want to include a checkbox column. And to each checkbox you want to assign a custom attribute. Go ahead and try it. I'll wait. Hopefully when you get back here you won't have pulled out all your hair after you notice that each checkbox is enclosed in a span tag and your custom attribute is not assigned to the checkbox, BUT TO THE SPAN! Somebody with more experience please explain that to me. That same exercise using the mvc framework involves either using the Html.Checkbox helper function or just simply writing the input tag yourself and assigning whatever attribute you want. I'd call it What You Write Is What You Get. And while the world certainly doesn't need another acronym, if you try saying WYWIWYG a few times you'll soon be reverting to an Elmer Fud voice and rolling on the floor as I am right now. 

Excuse me.

Ok, I'm back.  If you want further information regarding the mvc framework, I'd check out Scott Guthrie's blog. One thing that does seem to be lacking, at least for now, is a good set of VB.Net examples. I say this because I'm currently working on an application for a client that requires and having heavily relied on example code to learn this new framework, I spent a lot of time trying to convert some of the features. Admittedly, though, much of the difficulty in conversion was dealing with LINQ and not specifically the mvc framework. I'll post some of the more interesting examples another time. 

Wednesday, October 22, 2008

Getting Started in Beekeeping

My wife and I took a shot at beekeeping this past summer and had some mixed results. We started by attending 'bee school'. The RI Beekeepers Association sponsored the session that we attended and I have to say it was very interesting. The class met once per week for about 10 weeks and by the end we had the knowledge (and contacts) necessary to get started. 

We hooked up with someone who regularly goes down south to get what are called installation packages. This is basically just a small box with about 10,000 bees in it and 1 queen. The queen is kept in a small wooden box with a couple of attendants in there with her. More on that later. We got our bees around the end of April. I have to admit I was a little freaked out driving home with 10,000 bees in the back of my truck. Fortunately, it was an uneventful ride home.

When we got them home, my wife and I put on the bee suits (which look strikingly similar to hazmat suits), opened up the empty hive and dumped the bees into it. There were still some left in the box, but we just left it opened in front of the hive. They found their way in eventually. Next, we took the queen box and gently placed it in the hive. What is supposed to happen is that the queen is protected inside the box. If she wasn't in there, the other bees would kill her. The purpose of the box is to allow the queen to gradually get introduced to the rest of the colony. The queen box has a little hole in the top which is plugged up with a piece of hard sugar. Over the course of about 3 days, the queen's attendants eat through the sugar so that by the time they are free, the queen should be acclimated to the rest of the colony. 

That's it. Pretty simple. I'll post some more regarding some of the more interesting things that we encountered in our little experiment.