• tel: 0845 475 2487 (UK)

Ruby on Rails from the perspective of a .Net Developer

Developed by Jon

My blog entries have been a little infrequent of late as I have had my nose deep in the excellent Agile Web Development with Rails (Pragmatic Programmers) (Pragmatic Programmers) and also the fantastic Ruby on Rails Essential Training (Lynda.com).

For those of you that don’t know Ruby is a dynamic open source language created by Yukihiro “matz” Matsumoto in 1995. Rails is an open source framework created by David Heinemeier Hansson in 2004 to support the development of 37 Signals suite of web applications (Basecamp, Packpack etc.)

So two months later what have I learnt ..Well enough to be a competent rails coder, but more importantly, I feel the the process has been nothing more than an awakening to a new approach in Software Development. I have to admit when I first heard about Ruby on Rails I was skeptical and it was easy to state through away comments such as rails is slow, it’s not scalable, it’s difficult to deploy, you can do the same thing on .Net blah blah blah. And to some extent these are all true, but back to that in a bit.

So have I given up on .Net well no, of course not, however there is a time and place for .Net just as there is a time and place for Ruby on Rails. Before I spent time with Rails I easily got caught up in the .Net vs Java vs PHP arguments but at the end of the day all these languages have their place and software development is an art that is transferable to any language. I really can\’92t understand why some people are so defensive about the programming languages they use, I have programmed Basic, Visual Basic, Pascal, C, C#, Java, PHP, Javascript etc in my past however Rails is different but not to the extent that it makes any skills I have learnt obsolete, it just teaches an approach that I can take back with me and use with any other language I use and made me a better software developer.

And this is why I am so excited about Rails, I have been programming since the age of 6 (24 years) and discovering Ruby on Rails has been like rediscovering programming again for the first time. It is taking a disciplined approach to development which then allows a flexibility and freedom to really allow creativity.

As I said earlier I was not convinced with Rails at first so I set myself a challenge. I had a personal project that I had been working on for just over a month, using .Net and Subsonic. So if in one month I could not only learn Ruby on Rails but get to the same point in a RoR version of my project I would be convinced.

Did I achieve this. Of course I did or I wouldn’t be raving about it now. I picked Ruby up enough to get started in two weeks (evenings and weekends) and it took me two weeks to get to the same point I had done with the .Net equivalent, not only that I had produced a better product as I had not had to implement workarounds for various technical barriers I had previously had with .Net.

So of these bad points well yes Rails is slower than compiled languages but it can still handle huge amounts of traffic, Basecamp is a good example. Hosting for Rails is patchy especially in the UK, there are one or two hosts but my advice is to have a form of dedicated or virtual host which you can configure with Apache, Mongrel (Ruby on Rails Application Server). As for scalability, there is no problem at all as sessions can be stored in a database or using Memcache and it uses mysql on the backend.

So the upshot is I really recommend all developers, not just .Net to look at Ruby on Rails. Like me I hope you will discover a flexible web framework which not only makes your life easier but more enjoyable and rewarding, but it might just make you a better developer.

For more information:

Ruby: http://www.ruby-lang.org
http://www.rubyonrails.org

Using ASP.Net / AJAX slide extender to create a color selector

Developed by Jon

Using the MS AJAX ASP.Net Extensions and the Ajax Control Toolkit (available through codeplex) You can easily make a colour selector using the Ajax Control Toolkits Slider Extender control.

The Slider Extender included in the Ajax control toolkit renders on the screen as a control that allows the user to click and drag to adjust a numerical value. Out of the box the slider extender is given an ASP:TextBox as it’s Target Control ID, the slider extender automatically hides these textboxes. We can use three slider controls to create a Red, Green and Blue sliders to set a colour. When working in RGB we can set a value of 0 to 255 for each of Red, Green or Blue. So the first thing we need is the three sliders on our HTML page.

 

ASP.Net MVC Diagram

Read more…

Tip: ASP.Net/AJAX – Javascript in Label/Textbox Text

Developed by Jon

Hi All,

I though I would share a problem that I had (and resolved) using ASP.Net AJAX. The problem occurs if you want to output some Javascript to a Textbox (for example: for a user to copy and paste into their own page), which is nested in an UpdatePanel. AJAX returns an HTTP 500 error.

The reason seems to be that the Javascript in the textbox interferes with the javascript generated by MS AJAX to handle the partial page updates of the UpdatePanel.

So if like me you need to output some Javascript to the page for a user to cut and paste, a work around is to use a literal control and the HTMLEncode function of the Server object as follows:

  1. Literal1.Text = Server.HTMLEncode(<Script src=’example.js’ type=’text/javascript’></script>)

I hope that helps anyone that has been puzzled by this error.

Tim Shankarian's MS Ajax Autocomplete Patch (and how to use)

Developed by Jon

Contrary to the advise I have received from several fellow developers (and never being one to miss a challenge), I have decided to ignore all advise and attempt to use the Microsoft Ajax framework with my current .Net project for TktIt.com. The MS Ajax Frawework is extended to provide functionality such as Autocomplete etc using the AjaxToolKit. Although a lot of these are excellent, (like the pop-up calendar control) the Autocomplete feature is poor compared to many others out there. However, thanks to Tim Shankarian, it has got a little better!

Read more…

A bite from the forbidden Apple..

Developed by Jon

As a .Net developer, I am probably the last sort of person that would leave the world of Microsoft Windows based PCs in favor of an Apple Mac. But I have successfully made the transition and wanted to share my experiences with any other .Net / would be Apple fans.

I had been deliberating for a good year whether to buy a mac or not. I originally had no intention of leaving my PC altogether and though that it would be nice to have a Mac (maybe a Mac Mini) for the more creative / design aspects of my work. But now, having used a Mac for the design side it is true that the Mac fits the design role better than a PC, for a start the operating system is beautifully designed and therefore inspires the same in your own work. On top of that it displays colour more accurately and printed material looks the same colour as it does on screen. Other features just make working faster such as when clicking the middle mighty mouse button accesses Exposé, which allows very rapid switching between programs. Dragging files around is a joy, you automatically drill down into directories as you hover over icons and the list goes on. To the point, in fact, that I can’t go back, I have sold my PC and am based 100% on Macs. So where does that leave me as a .Net developer.

Well thanks to the Intel processors in Macs, running windows when needed is a breeze, in fact I would swear in runs Windows faster than on my PC, even in Parallels (virtualisation software like Virtual PC or VMWare but that can display running applications on the Mac OS desktop rather than within a host window). Once Parallels is running, when I open Visual Studio 2005 Professional it seems to open instantly and is very workable, even on my MacBook. So now I can design my web sites’ look and feel in Mac OS X and then when ready to code in asp.net, boot up Parallels and work in Windows giving me the best of both worlds.

The transition wasn’t as easy as Apple like to make out. For a start you have to look at computing in a new way. When George Cook, a friend of mine who also recently made the jump, advised me to switch to Mac, he described the problems I encountered “like the matrix, your eyes are open but you are not seeing”. Which is very true when you switch to Mac OS X you have to stop treating it like a PC. For example, with Mac OS X when you click the red X in a window’s title bar, you don’t actually close the application, just the window, which can be very confusing for a windows user and your first reaction may be to try and kill it (either by pressing Apple-Q or by holding the mouse button over it’s icon in the launcher and selecting quit), but you don’t need to. This is Mac OS X and it is designed to have these applications left running. So for example when you next open iTunes, Firefox etc they open instantly, and amazingly it does not seem to impact performance (Although iPhoto seems to struggle). Getting emails over from Outlook on my PC was a challenge but I succeeded in the end (using IMAP) but in the end it has all been worth it and I have not looked back.

So I can recommend to anyone who would like to go Mac but feel that cannot as they are .Net developers, to go for it. I have had no regrets and for any Windows only software you can just boot into Parallels. I recommend you still keep your Windows copy of Microsoft Office on your Parallels based Windows, as Mac Office 2004 is not designed for Intel Macs so crawls along. Also there does not seem to be an official port of OpenOffice to Mac OS X but Neo Office is not bad if you really don’t want to run office in a virtual machine.

Writing an XML-RPC server or client in ASP.Net: Part 2

Developed by Jon

In my previous article I covered how to setup an XML-RPC server in .Net, in particular looking at how to service XML-RPC MetaWebLog API requests. In this second part I am looking at how we can easily construct an XML-RPC client to ping blog directory or search services such as Technorati, Google BlogSearch, blo.gs etc.

Read more…

Writing an XML-RPC server or client in ASP.Net: Part 1

Developed by Jon

Part Two available here

I have recently be working on a project which needed to implement the blogging MetaWebLog API, allowing users to post blog messages from their favorite blog editor, such as Windows Live Writer or blog directly from MetaWebLog API enabled websites such as Flickr or Digg. My tutorial below will show you how to easily create or consume an XML-RPC service such as the MetaWebLog API.

Read more…

Following the MVC code methodology with ASP.Net / SubSonic

Developed by Jon

I have been recently getting into Ruby on Rails and wanted to share my experiences of applying a similar philosophy to ASP.Net development. One of the core principles of RoR is based on the development of dynamic websites around the Model, View, Controller Methodology. This gives a clean code separation and by following this convention we reduce the risk of duplication and always know where to look for a certain bit of functionality.

Ruby on Rails is based on the MCV model and implements it cleanly out of the box. ASP.Net is not designed around this, so we have to take a slightly different view of our ASP.Net projects to achieve the same thing.

For this approach, I am making use of the SubSonic ActionPack (Zero Code DAL). If you do not know, or have not used SubSonic before, I cannot stress enough how useful this components is. Once SubSonic is setup correctly and pointing at your database. You simply run an .aspx page that generates classes for the selected tables in your database. This allows you then to access information in the database as if a member of a class. Effectively SubSonic is a self-building Object Relational Model (ORM).

View

The View of the MVC model is as you would expect, the .apsx page itself. To follow the model other than JavaScript, etc, avoid .Net coding in this file and keep it cleanly in the code-behind page.

Controller

In the MVC model, the controller deals with the interaction between the view that the user interacts with and the model, that contains are business logic. In here our event handlers, such as button1.click etc are handled as normal, however I stress these event handlers should not contain any business logic. Instead purely use code here to control the view. For example, in here you would call the Model to get you the required information to populate to a GridView and simple databind that data. The business logic to get the required information (for example all products of a certain category) is stored in your Model.

Model

To implement the Model in the MVC model, we need to create our own classes for this. I like to group all functions that relate to a certain theme in one class or model. Using our examples of Products, I would create a ProductModel.cs file and store all my functions such as the above GetProductsByCategory() method.

By doing this I can change the business logic at any time without having to worry about which ASP.Net code behind pages I have accessed the products table from.

To show you the simplicity of SubSonic, to access the Products by a certain category we could define the following:

public static ProductCollection GetProductsByCategory(string category)
{
     SubSonic.Query query = Product.CreateQuery();
     query.AddWhere(Products.Columns.Category, category);
     ProductCollection pc = new ProductCollection();
     pc.Load(query.ExecuteReader());
     return pc;
}

That’s it no SQL, no worries, the ProductCollection, SubSonic.Query and Product classes are all generated for us by SubSonic.

SubSonic DAL/ORM

The pink classes on the diagram labeled SubSonic ActionPack are created for us and automatically built around our pre-designed database.

Helper Files

Finally the helper files are classes created by us to contain methods that we regularly use throughout our application. For example, a method to strip HTML from a string. I normally name these with Helper in the name to distinguish them from my Models and SubSonic classes.
So as you can see it is fairly easy to take the MVC approach when building a Web Application in ASP.Net. The core engine behind this is the SubSonic ActionPack which really makes all the difference.

SubSonic .Net ActionPack Starter Kit Template

Developed by Jon

Having used SubSonic recently and found it an invaluable resource, reducing not only my development time, but code complexity too, I have created two Visual Studio 2005 Project Templates based on the SubSonic starter kit. I apologize if something like this already exists but I couldn\’92t find it in the official Starter Kit. I created these templates as I needed a quick way to create a new Web Site project in Visual Studio based on SubSonic with none of the example site content within.

Read more…