Thursday, September 25, 2008

Easy database querying with dOOdads (C#, .NET)

Been some time since I posted about MyGeneration and dOOdads. Still using it regularly, even though it is a few years old now. A few sites I do still use .NET 1.1, so it is still in use.

A brief (re)introduction - MyGeneration is a tool that can generate code for you, through the use of templates (you can create your own, and there is a template library). It comes bundled with the dOOdads data abstraction library, which allows you to update and query your database without knowing the intricacies of interacting with it (i.e. .NET classes to use, SQL to write etc). Both MyGeneration and dOOdads support multiple databases (SQL Server, MySQL, Oracle etc) and regeneration of code, changing the connection string is often all that is needed to move to another system.

It was freeware initially, but is now hosted on SourceForge and no longer actively maintained by the main developers.

A basic example of a query would be:

Employees e = new Employees();
e.Where.EmployeeID.Value = 1;
if(e.Query.Load())
{
    SurnameTextBox.Text = e.Surname;
}

You can even select multiple employees with different EmployeeID's:

Employees e = new Employees();
e.Where.EmployeeID.Value = "1,3,7";
e.Where.EmployeeID.Operator = WhereParameter.Operand.In;
if(e.Query.Load())
{
    ....
}

For columns of type 'bit' (e.g. true, false), where you want to treat NULL as false, you use a TearOff:

Employees e = new Employees();
e.Query.OpenParenthesis();
e.Where.DrivingLicense.Value = false;
WhereParameter driving = e.Where.TearOff.DrivingLicense;
driving.Operator = WhereParameter.Operand.IsNull;
// add conjunction and close parenthesis
driving.Conjuction = WhereParameter.Conj.Or;
e.Query.CloseParenthesis();
if(e.Query.Load())
{
    ....
}

Note: driving.Conjuction is correct, it is a typo in dOOdads.

jQuery UI draggables - helper CSS class

While working with jQuery UI draggables, I have noticed that there does not seem to be a class associated with the draggable helper, and so it is styled in the same way as the draggable you selected. As a result you have to resort to the start function option to add the class dynamically:

$(".dragme").draggable({helper: "clone",
 start: function(e, ui)
 {
  $(ui.helper).addClass("ui-draggable-helper");
 }
});

Then just create a style for ui-draggable-helper, e.g.

.ui-draggable-helper {
 border: 1px dotted #000;
 padding: 6px;
 background: #fff;
 font-size: 1.2em;
}

Friday, September 12, 2008

Place JavaScript code at bottom of page

Recently I've been placing scripts at the bottom of the page, instead if the top. However, I don't put all of them there - the key ones required by other scripts (e.g. libraries like jQuery) are still in the header. The pages can appear to load faster, and in some cases may save some (albeit not always that much) code, e.g.

....
<script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
<script type="text/javascript">
$( function()
{
 $("a.foo").click(doSomething);
};
function doSomething()
{
 return confirm("About to visit " + this.href + ", continue?");
}
</script>
</head>
<body>
...
</body>
</html>

Can instead be:

....
<script src="js/jquery-1.2.6.min.js" type="text/javascript"></script>
</head>
<body>
...
<script type="text/javascript">
$("a.foo").click(doSomething);
function doSomething()
{
 return confirm("About to visit " + this.href + ", continue?");
}
</script>
</body>
</html>

In some circumstances, the script may query the CSS of an element and so the script may run before the associated stylesheet has downloaded (at least, I have experienced this in Google Chrome).

This does assume that you have not attached javascript functions inline (e.g. a onclick="doSomething()" as these would fail if the user interacts with elements before the function is available.

If you use 'DOM ready' events, then there is less benefit of doing this (as it should run when the page is ready anyway). As with any optimisations, it is always worth experimenting to reduce the time it takes for a page to be used and interacted with by visitors.