Amptools.Net

simplify your life.

getting the total count for query with group by

No Gravatar

I ran into the need of getting the total flattened count for pagination purposes when using a group by clause in a query.

A stripped down example of the structure of this query would look like the following.

SELECT COUNT(*)
 	FROM table as t1
 	INNER JOIN table2 AS t2 ON t1.id = t2.t1_id
       GROUP BY t1.id

As one would surmise, the results would be count of the records for grouping.

| count	|
--------
| 1		|
| 2		|
| 20	|
| 3		|
| 17	|

The conclusion was to do another select count wrapping the first query. After some trial and error and intensive googling, searching the mysql docs, and mysql books, I was still getting no where.

I found the answer only by searching stackoverflow.com. To which should be my default geek search engine. (There really is a market for niche search engines).

The answer looks like this, the first query being wrapped with a count selecting from the query.

SELECT COUNT(*)
 	FROM (SELECT COUNT(*)
 	 	 	FROM table as t1
 	 	 	INNER JOIN table2 AS t2 ON t1.id = t2.t1_id
        	 	GROUP BY t1.id)
 	tmp

Thus for ending my torment and suffering, I bumped up Marcus Adams’s answer on stackoverflow and must give mad props in this journal entry.

Tags: , , ,

The trouble with porting migrations and railties into other languages.

No Gravatar

I’ve been toying with API design for solid and simple with Ado.Net to build migrations on top of and possibly layer for building ORMs on. Ironically, Rob Conery, of Subsonic fame, just posted a page (or blog) about migrations in .net and subsonic in general.

The Problem(s).

Database agnostic scripting in C# and PHP and executing sql statements without having to use a heavy ORM in C#.

Obviously, Rails makes a good run at these problems with active record’s built in migrations that everyone and their mother (or at least their co-workers) tries to mimic.   However C#, PHP, and other C style languages lack that Ruby Meta Programming magic.

So why not just include active record into a project that isn’t rails?  This might be doable with Iron Ruby’s release on the horizon. Unfortunately the cost of adding another language to a project is not always justifiable (as some rubyist zealots tend to forget ;) .

So why not just port the awesome library?

After much time over the last year or so, I’ve come to the conclusion that porting libraries from other languages often comes with delusions of grandeur.  Especially when the languages differ on key features.

Ruby and in some ways Javascript, have a much better mechanism for the portability and mixing in methods. Ruby itself has the ability to create powerful readable Domain Specific Languages that other modern languages will not be able to copy.

You tend to see possible extra noise or reduced flexibility when you compare something like Migrator.Net

[Migration(20080805151231)]
public class AddCustomerTable : Migration
{
    public override void Up()
    {
         Database.AddTable("Customer",
             new Column("name", DbType.String, 50),
             new Column("address", DbType.String, 100),
             new Column("age", DbType.Int32)
        );
    }

    public override void Down()
    {
        Database.RemoveTable("Customer");
    }
}

to Rails Migrations.

class 20080805151231_AddCustomerTable > ActiveRecord::Migration
  def self.up
    create_table :customer do |t|
        t.string   :name,     :limit => 50
        t.string   :address,  :limit => 100
        t.integer  :age
   end
 end 

  def self.down
    drop_table :customer
  end
end

The biggest issue I see though is sometimes throwing out the baby with the bathwater by not using some of the same naming conventions because the languages do differ.

Possible Solutions Or Ideas

At least when it comes to C# 4.0 it shouldn’t be too hard to create a .Net library and then create a thin DSL layer in Ruby through Iron Ruby to make calls to the .Net lib.  That coupled with some form of code generation could give you a decent programing work flow.

Another option is to port key features from Rails, adapt to the language that your working in, but at least keep some kind of consistency in naming.

Or you could do a hybrid. That way you have the ability to create a readable DSL in projects that can allow for this or let you fall back to your native language if it doesn’t.

It is still ugly and verbose, but at least it follows in some proximity to its ported counter part.

[Version(20080805151231)]
public class AddCustomerTable : Migration
{
  public override void Up()
  {
    CreateTable("Customer", (t) => {
      t.String   ("name",     (c) => { c.Limit = 50; });
      t.String   ("address",  (c) => { c.Limit = 100; });
      t.Integer  ("age");
    });
 }

 public override void Down()
 {
    DropTable("Customer");
 }
}

And it shouldn’t be hard to build on an underlying API. If done right, you could use the DLR for Ruby or even Python for scripting access or creating that thin readable DSL layer.

It would have probably been cleaner to use optional parameters, but ‘default’ and ‘null’ are keywords in C# and adding @ to keywords for variables can distract from readability.

Another path is to create something new that adapts to your language but does the same thing in principle similar to what Rob partly did for Subsonic 3.0’s migrations.  Porting could be trying to put a square peg in a round hole. So its time to create that peg that fits.

I’ll have some follow up posts that cover ideas on the other areas of being able to script sql in C# with Ado.Net as well as experimental APIs and concepts.  If you got any comments, leave em.

Tags: , , , , , ,

Microformats in HTML 5

No Gravatar

Declaring microformat uri profiles in HTML 5 is different than it was for HTML 4. The profile attribute for the head element was dropped in HTML 5. Gone. Banished, despite the use of other W3 standards that might make use of it like GRDDL (Gleaning Resource Descriptions from Dialects of Languages). And I though Microsoft was bad at naming things..

This is the typical way that you would define a uri profile in HTML 4

<html profile="http://purl.org/uF/2008/03/">

In HTML 5, the microformat’s wiki suggests using a visible link, anchor a element, or a hidden link inside the head element. I suggest we use the hidden link inside the head element, despite the lack of example usage of this on the wiki.

<link rel="profile" href="http://purl.org/uF/2008/03/" />

Now you can go out in the world still use microformats for HTML 5 without generating invalid markup.

In Vegas, Tonight Only: Asp.Net Mvc vs *

No Gravatar

Notice: I’m always right. kidding. no seriously dawg, I am.

This post is not about how one Mvc framework is better than the rest, rather about my views of the current status of heated debates in the software community dealing with MVC.

Its feels like the Mac vs PC debate. The practical versus the trendy clique.

The Line Up: developer cat fights

There has been a ton of debate about Asp.Net Mvc on twitter, forums, blog posts and various other mediums. Asp.Net Mvc vs the world of Mvc: WebForms, Rails, and various Java Mvc frameworks (i.e. struts, spring, groovy, grails, etc).

I would add PHP Mvc frameworks to the list, but it seems the frameworks in PHP are always too busy duking it out with other PHP frameworks to have time for frameworks in other other laguages.

To get a glimpse of this, all you would need to do is a search in twitter for “mvc or php or framework”. You could also just scout Rob Connery’s blog, specifically the post on WebForms and the Mvp framework that wants to be Mvc.

Intervention Hotline: 1-800-dev-kill

Scott Gu wrote a blog post on the debacle that is Asp.Net Mvc vs WebForms and debating in general. Scott Gutherie, the guy behind Asp.Net and now much more, originally created WebForms and had much to do with the new Mvc framework in .Net.

This is the first time that I’ve seen one of the higher ups at Microsoft openly engage the whole community this openly in a blog. I do think the guys in Redmond know debate/discourse is important to a healthy community, yet realize a house totally divided can not stand.

I surmise that they also realize the community has nothing to gain by trying to bash other languages, platforms, or other solid slightly competing Microsoft technologies.

View From The Ring Seats: wiping off the blood spatter

If people were to visualize this debate it would look something along the lines of the stereotypical somewhat chunky guys with uber thick glasses, living in their mom’s basement, smacking and pulling each others hair out over a bunch of 1s and 0s.

Scary image, is it not?

This is barely a step up from the angry french guy wearing a sleeveless turtle neck selling a wicked cool book-scanner.

Developers, Developers, Developers: type this in youtube.

There are a vast array of developers: they can be green, new, old, seasoned, amazing, awful, inquisitive, humble, or arrogant. Sometimes it better to take a second to understand where they are coming from. New developers sometimes get overexcited easily.

I’m supportive that developers are passionate about code and their craft. It is a great motivator and one of the biggest reasons we are gifted with great under appreciated opensource projects that we do have.

However, I have to wonder how much of this debate is due to other reasons: The pressure in the job market? The heightened political debates reaching over into other arenas this past year (2009)? Is it jealously of attention or just being bombarded by the new kid on the block, Asp.Net Mvc?

Emotion does seem to block all blood moving to the rationale and logical parts of the brain. Maybe its just a lack of…. anyways.

I find it hypocritical when a Ruby developer gets upset because a .Net programmer is excited about the new Mvc option or vice versa. Or better yet, why are we debating why Asp.Net Mvc is better than WebForms? The Asp.Net framework is built on top of WebForms.

The whole Asp.Net Mvc vs Asp.Net WebForms debate is like this. A 3 year old jumps up and down ands says “Mom, I hate you” and the mom vehemently replies with immaturity, “I hate you too”. Except these are grown men and women, saying this about software. Does that put perspective on it?

Its laughable, arrogant, and ignorant that anyone can honestly say a certain platform, language, or framework is totally superior than all others.

If you want to be the Adolf Hilter of programming languages, frameworks, or applications, be my guest. Just don’t expect that to wow your clients or make you a ton of money.

The simple reason why that an argumentative attitude will not work is that it creates division and problems, it does not solve them. Clients don’t respect that. If you need to see this for yourself, look at Microsofts reversal efforts of its previous anti-opensource views and policies.

Don’t Be A Tool: be a craftsman.

Whether your choice of tooling comes from your preference or it might be the solution your client needs: there are various ways to solve problems. Some are more efficient than others. Over time those solutions change for better or worse.

A great developer can generally find interesting and new ways of solving a problem, even in a very constrained space with the framework, tool, or language is not up to par.

Clients and users generally don’t care what you use as long as you get the work done. Some clients do care and have constraints for a platform, language, framework that you must fill.

Do what is best for them with as much unbiased decisions that you can, not what is best for you. Network with developers in various spaces, trade work that fits your skill sets or preferences better.

Down for the count: rather breath and count backwards

In the meantime, if your passion leads you astray into heavy arguments that descends into insulting the person directly or indirectly. Then its probably time to hit alt+f4 before you make the comment.

If anything you could probably use a walk to shave of those extra developer pounds of fat. Maybe turn on the XBox or Wii, blow some things up. Unplug, read a book.

Those tweets and blog posts will still be there when you get back.

Do something other than waste bandwidth and space on a harddisk with irate and irrational debate like you were some kind of singles’ dating service advertisement.

At the very least, put that energy and passion into creating an opensource project or something usable for the community.

Feel free to comment, vent, or add some humor.

Saying goodbye to shared hosting, embracing the cloud.

No Gravatar

I really liked using reliablesite.net as a host provider. Alas, their lack of ability to reign in MySql was killing my ability to do anything with a wordpress blog.

First it started with the site timing out. Then I couldn’t even log into the backend to make a post. Even with caching, I was increasing my collection of grey hair prematurely.

Amptools.Net now resides in the cloud.

At first, I must say that I was a little skeptical of the notion of moving to the cloud. Though, months of issues, lost productivity, and really no lead way in resolving the issues; does have a way of influencing your decision making process.

Being the Granted Master Developer that I am (don’t feed the ego folks, it bites), I wanted options. I wanted access to a server with the ability to run .Net, Mono, Ruby, or PHP as needed.

I need to be able to install software that has been shown to work well without difficulty. Of course like any consumer, I need to do this on a fair budget.

My hosting options were to find another shared host, get a dedicated server, or jump into the cloud.  After searching, the cost of shared hosting with limited options excluded themselves.

It came down to finding a decent by inexpensive dedicated server or start digging into researching the options for hosting a site in the cloud.

What surprised me the most was the lack of explanation about the differences in the major cloud companies and what they actually offer.  I consider Amazon, Microsoft, Google, and Rackspace (formerly Mosso) as the biggest providers in the market.

Google’s App Engine

Probably the most self explanatory plan is Google’s App Engine. You are hosting single apps and charged per application.  You are limited to 10 applications. You are also limited to using either Java or Python at the moment.

Hmm, I like python. But right now I spend more time in other languages. Java. Well, it has no fight left in it. Its like it has given up in becoming a better language, so thats a fat no.  Plus I really want to be able to install a variety of opensource applications as needed that tend to be in other languages.

Microsoft’s Azure Platform

Microsoft’s Azure platform was the most tempting at first. Microsoft offers .Net, Java, PHP and Ruby on their platform. They generally have great developer tools and they even have an Azure plugin for eclipse.

Microsoft’s model is also one application per instance. Paying $60 or more a month per sandboxed application just wasn’t appealing to me. I need to be able to install more than one application per site.

Rackspace Cloud

Next I evaluated Rackspace. They seemed very promising. They offer an application instance model like Google and Microsoft, but they also offer cloud servers at a very compelling rate.

Yes! Wait!!! Their cloud servers only offered linux operating systems at the time.  Well I can use Mono, PHP, and Ruby, but I really want to be able to install .Net 4 when it comes out and I didn’t want move hosts a 3rd time.

Time to check out Amazon.

Amazon Web Services EC2

Amazon’s model does not offer an application instance model, but it does offer cloud servers (EC2). They’ve have been running windows for a while, but they even have Server 2008 and Server 2008 R2 with a preloaded stack of .net 3.5 and Sql Express.  I would need to install PHP and Ruby myself, but thats no big deal.

Even though the price seems steep compared to shared hosting, its cheaper than dedicated hosting and lets you run more than one application on the server per instance and to install what you need within reason.

Amazon EC2 was the one for me.

The cost is a bit more per month. Its totally worth the ability to blog once again and the freedom to set up the server to meet my needs. The cloud also comes with scalability and maintainability perks to build on. Now I just need to figure out how to make this site cover hosting costs, hiring a professional editor, and owning a cat. Then I’m golden.

Tags: , , , , , ,

Asp.Net Mvc 2 Areas Tip: Drop in a web.config

No Gravatar

I ran into an exception “Could not load type ‘System.Web.Mvc.ViewPage” or something along those lines while trying to implement “Areas” for Asp.Net MVC 2.

So if you are coming up against this issue, you might be having that same issue.

The solution, drop in a copy of the web.config from the views folder into the folder for your areas folder (i.e. Areas/AreaName or Areas/AreaName/Views). Refresh. That should solve that exception. Hopefully.

Tags: , , ,

Switching between Ioc libraries in Net.

No Gravatar

Lets face it. Some developers are religiously attached to their beloved languages, frameworks, and libraries.

This becomes a problem when building software with hopes of gaining a developer base. If it is not built on Unity, Castle Windsor, Structure Map or [insert beloved Ioc library here], they refuse to work/use the software.

Evidently others have had this issue and have created a solution, the common service locator.  I ran across while searching for asp.net  mvc and DI (dependency injection) for models. More on DI with models inside controllers in a future post.

How to use

Download the library from codeplex.com/CommonServiceLocator

Its pretty straight forward.  Download the service locator adapter for the Ioc library of your code on the codeplex.com/CommonServiceLocator

choose the adapter for your Ioc library.

choose the adapter for your Ioc library.

Configure your Ioc library in the usual manner. Then pass the container into the adapter.

        private IServiceLocator CreateLocator()
        {
            var container = new UnityContainer();
            container.RegisterType<IUser, User>();
            container.RegisterType<IMembership, Membership>();
            container.RegisterType<IUser, Loser>("loser");

            return new UnityServiceLocatorAdapter(container);
        }

Then tell the service locator which adapter you are using.

        private void SetupService()
        {
            // CreateLocator returns IServiceLocator in the above method.
            ServiceLocator.SetLocatorProvider(() => CreateLocator());
        }

Then use the locator to return the adapter, which will allow you to use a common interface to resolve your dependencies.

        [It, Should("resolve the type to the default")]
        public void ResolveType()
        {
            this.SetupService();
                         // gets the current adapter for the Ioc Library.
            var resolver = ServiceLocator.Current;             

            // resolves the type.
            var result = resolver.GetInstance(typeof(IUser));

            // verify the result is the right type for the contract.
            result.IsInstanceOf(typeof(User));

            // this user should not have a membership.
            var user = (User)result;
            user.Membership.ShouldBeNull();
        }

Some Caveats.

Someone named the base service locator class in a java like manner using Impl.

The assemblies for the Unity Ioc are not strong named assemblies. I had to create a key, sign the assign the assemblies for Unity. You can download the midori-project which has the signed assemblies for Unity under the vendor folder.

Further Usage and Reading

If you would like to check out the spec and see how it might be used, check out the spec in the midori core. This will give you some context.

You can also read more about it on codebetter.com as well as the initial post from Jeremy Miller that spawned someone to write this.

Tags: , , , , , , ,

Asp.Net Mvc Add Css Classes To The Body Element

No Gravatar

Using the css classes on the body tag/element, you can open a door to having finer control over the layout of your site. Its one of the few decent tricks that bigger Content/Course Management Software like drupal or moodle provides.

In Asp.Net Mvc this can be done in the master page, outside of the head tags. What makes this nice is using the controller and action name to isolate the page and section of the site you are int.

</head>
<%
        var values = Html.ViewContext.RouteData.Values;
        var bodyClasses = values["controller"] + " " +
                        values["action"];
%>
// in the master page
<body class="<%= bodyClasses %>"> 

// what should be rendered for the url: /blog/edit/5
<body class="blog edit">

As you can see above, it would pay off to stick to using the controller and action keywords in your routing. You could adjust the above for adding the modules/areas name.

.blog aside.column {
        display: none;
}

.blog.edit  footer {
        margin-top: 20px;
        background: transparent(images/egg.png) no-repeat;
}

You can now target certain aspects of the layout and change it depending what section of the site you are in.

Tags: , , , , , , , ,