Jan 30

http://www.developerdeveloperdeveloper.com/ddd8/

Today I attended DeveloperDeveloperDeveloper 8, or DDD8, at Microsoft HQ in Reading. There were, I think, several hundred delegates attending – with many more sadly disappointed. The event itself opened it’s registration and was completely filled in around 12 minutes! I was not able to register immediately due to a meeting at the office, but was put on a waiting list. Thankfully some kind soul cancelled their registration which allowed me to attend :)

There was a pretty varied schedule of speakers, including @RobAshton from my company’s vNext team covering Multi-tenant MVC Projects. Here’s a summary of the talks I attended and what I thought:

Test Driven Development to save, time, money and your sanity – Richard Hopton

I’ve not covered much TDD before and fortunately Richard was able to pitch his talk at just the right level. I was sat in with my old project manager and I think we both took a lot away from the hour we spent listening to the benefits of adopting TDD for software development.

I liked the way TDD actually forced developers to improve the way they approach their applications, encouraging a tidy, logical and concise methodology. We were demoed a work in progress DiamondMaker application that made it easy to see how a suite of tests could be used to provide QA, automation and improved design. Free swag included a fridge design kit for Expression Blend 3, utterly useless but rather fun!

Hello Document Databases – Neil Robbins

I wasn’t originally going to go to this, but I thought I’d give it a punt and see what happened. Neil gave a great introduction to the dominance of the now ubiquitous RDBMS and explored some of the problems – chiefly overkill for small web apps and for huge, fault tolerant data stores for distributed systems. In this instance we looked at Couch DB, although we could have covered the excellent Lucene project or MongoDB.

Couch DB offers a really different view on interacting with a data store; essentially doing away with SQL and connection drivers and replacing them with views (functions more or less) and a RESTful interface. I had a little go with it when I got home – replacing my SQL Server Management Studio Express with curl from a Linux Terminal app and passing in HTTP GET/POST requests and receiving JSON in return. Lovely stuff :)

Overall – very impressed with the content of the talk and it really sparked my curiosity to investigate using Couch DB for little web apps…

No swag from this one, and was disappointed to not receive any feedback when I tweeted for biscuits to aid my rumbling belly :(

An Introduction To Mono – Toby Henderson

I have dabbed with Mono before so had a fairly strong understanding of the implementation, IDE and features available so the first half of the talk was a little academic. What I wasn’t aware of was the excellent tools now available with MonoDevelop 2.2 – the tools I saw today were really amazing. MonoTools for Visual Studio really made cross platform .NET development a breeze – full support for remote debugging and testing, direct from Visual Studio.

Wow moment was watching an app get launch from VS, directed to a OpenSUSE VM host, hitting a breakpoint and returning to VS in the host OS. To say Mono development has come along is truly an understatement.

Swag: none. Boo!

C# on the iPhone with Monotouch – Chris Hardy

I have to admit I wasn’t planning to go to this one, originally favouring the Entity Framework talk. Somehow I ended up in this one, but I didn’t regret it. Chris delivered an excellent talk covering Monotouch for iPhone development. I can see real benefit in putting a C# abstraction layer on top of Objective-C/XCode. I had a go with Objective-C for a small Mac OS app last year and I found it a little fiddly, so being able to use C# to abstract it all away was a real plus.

I was hooked until the price of Monotouch was revealed – $399 for a personal license! Seriously!? Plus you’d need a Mac and iPhone to do any serious development. It’s a heavy upfront investment for individuals, but if you’ve got a good idea and can get your app past Apple’s dev-police, then you could be in for a real treat for stable, rapid application development :)

Swag: none again!

An Introduction to IoC containers with Castle Windsor – Mike Hadlow

I felt I had to go to this as some of my office’s projects make extensive use of the Inversion of Control and Dependency Injection design pattern, and I really didn’t know much about them. This was a real eye opener to me – I saw a lot of similar code to what I would write for quick ‘n’ dirty apps or for older apps I’d written.

By adopting these two design patterns I was able to see how a yucky .NET app using ADO.NET and a chunky main method could be abstracted two-fold:

  • Pull most functionality out of the Main() method
  • Introduce interfaces and data repositories to allow different components to be used, for example for data stores/logging providers etc
  • Using a container that effectively mimics a microkernel that defines the requirements of each ‘tenant’ of a system

Now I know we use StructureMap over Castle Windsor as a Container package due to configuration preferences so I’ll spend more time looking at that. But holy shit – what an eye opener into application design. How do we NOT make more use of this!? It’s really great to have something put to you that allows such a huge degree of flexibility but is actually rather easy to understand.

I really like the idea of adding extra messaging/logging/data providers. NHibernate with FluentNHibernate as a data repository? No problem. Maybe LinqToSql or Couch DB – also no problem. Great integration with MSTest/NUnit so TDD will, in fact, lend it self very naturally to this approach. I really need to look into mocking objects for this purpose too.

Swag: t-shirt, whoop

Final thoughts today

I think I have absorbed a huge amount of information from today’s event. Much more than I’d ever, ever, ever get from any corporate training event. I cannot state how useful this event has been to my professional development and would really like to thank a) all the speakers, b) all the people who turned up and c) Microsoft for hosting it and providing excellent hospitality and facilities. I’ll definitely try to make it to next year’s

Now to try and apply some of these new ideas to anything Drupal related… we’ll see!

Share this post:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Fark
  • LinkedIn
  • MySpace
  • Slashdot
  • StumbleUpon
  • Twitter
Tagged with:
Jan 10

Free World book cover By Tim Garton Ash

I picked up a copy of Timothy Garton Ash’s (TGA) book before I went on holiday. My first job was spent working with many of the world’s leading academics in the field of politics and international relations, so I had a vague interest in the subject of international relations. I had previously read a few autobiographies written by journalists and enjoyed the lively and clear manner of their writing.

What struck me within the first few pages of reading TGA’s book was the hugely accessible style. TGA covers topics such as the deep and profound identity crisis faced by Britain in the last few decades, the approach to terrorism by the world’s governments, and the positive effects that free trade can have for the developing countries of the world.

The chapter on Britain’s relationship with the US and Europe forced me to re-examine many of the opinions I held as well as many of those that I had heard. Free trade had the most profound impact on my thinking. It occurs to me that a great deal of charitable work takes place in the UK – most clamouring for a regular, small donation to fund food aid, medicine and so on and so forth. What many members of the public are not very aware of is the notion of free trade. I think I can summarise it briefly – surely it’s better for a developing country to develop itself further by trade than it is by simply receiving handouts from others?

The amounts given by developed countries in foreign aid are sizeable in their own right, although the percentage of GDP is sometimes very questionable. However, what I found more alarming to read is the closed nature our trade markets have. I was amazed to see the now out of date policies of the European Common Agricultural Policy and the way Europe imposes harsh trade tariffs and conditions on developing nations. At a high level – not encouraging trade with developing nations seems tantamount to encouraging the aid packet mentality. Surely it would make sense to treat other nations as equals and not maintain policies that only seek to provide nothing more than a token effort to include them in globalised trade?

In light of the arguments outlined by TGA, I am happy to put my thoughts into action and give my 1% of net income towards making the world a better, and fairer place. My contribution will go towards encouraging free trade – what will yours be?

Share this post:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Fark
  • LinkedIn
  • MySpace
  • Slashdot
  • StumbleUpon
  • Twitter
Tagged with:
Nov 06

So the company I work for is in the process of rolling out a project for Service Oriented Architecture to provide some kind of interface between all our disparate applications; currently sitting as a mix of Java, ASP.NET, classic ASP and Drupal. Wow. I’m currently working with our Drupal based sites and oh no… I have to integrate with our “mega transformation and messaging engine”, also known as an Enterprise Service Bus using web services.

SOAP, REST or XML-RPC?

This was very much a contract first, code after project, so SOAP was needed. Yes, that meant lots of WSDL docs and XSDs… the syntax is frightening in places but it’s great in it’s own way. The stuff I’ll be discussing covers SOAP 1.1 using document/literal encoding.

You could indeed use REST… and if you’re working with XML-RPC, I would imagine you’re either supporting a legacy system or you’re really into punishing yourself!

Drupal and web services?

Two factors spring to mind with Drupal…. loose typing and the unique manner of it’s URL mapping.

Loose typing is an inconvenience. When a SOAP contract is defined it defines an element and it’s data type. So how does your underlying PHP code understand that should be treated as a boolean? The short answer is…. it doesn’t. Bummer.

What tends to happen is a lot of assumption takes place. The SOAP contract will not provide anything other than a boolean value, so you can, usually, cast your data into the required data type and help the process along. There are other issues with complex types but I will come to those later. If you’re accustomed, like I am, to the excellent capabilities of .NET to generate a class based on a WSDL you’ll find yourself stressing a bit with PHP. No matter… onwards.

URL mapping, a bit like the controller in an MVC framework, binds a given path to a module and a function, or page callback. The problem is how do you get Drupal to work with SOAP requests/posts to a given URL and if it’s possible, how does it perform?

I looked at the services module for Drupal and found it to be incomplete, slow and difficult to tailor for my project’s needs. That said, I think the concept of their module is excellent and would like to see it develop into a more polished product. In the end, something of a compromise was realised; PHP does SOAP servers and clients, Drupal is PHP – so why not create something that takes advantage of the two and avoids many of the complications?

SOAP server or SOAP client

This depends on the nature of your application. For my task I had to create both – most data going in across a SOAP server and some data being pushed out via a SOAP client. The SOAP server is more interesting, so let’s look at that…

I am aware of the NuSOAP libraries for PHP4, but as I work with PHP5 we’re lucky enough to have the native SoapServer and SoapClient objects at our disposal. The SoapServer client is easily set up with a few lines of code:


< ?php
// see http://www.php.net/manual/en/soapserver.soapserver.php for a comprehensive examination
$server = new SoapServer("some.wsdl");
$server->addFunction("YourFunctionName");
$server->handle();
?>

That’s it. The hard part comes with writing your function and getting your WSDL generated, or written, correctly.

Interpreting your request

Ok, let’s assume we have a WSDL doc which defines a quick function to add data about a user. It defines the following elements – I’ll use pseudo-code for this as I haven’t got a WSDL generator at hand:

* email (string)
* active (boolean)
* roles (array of strings)


/**
* function accept and process a SOAP request
*/
function AddData($request) {
$email = (string)$request->email;
$active = (bool)$request->active;
$roles = (array)$request->roles;
}

Note how elements within the SOAP request are always accessed as you would access object properties. You might be able to use array syntax, but I’m not sure if this would work reliably… if it all.

Most common gotchas when processing SOAP requests

  • Is it a string, or an array!? – the answer is… it depends. Say $request->roles only contains a single element – PHP doesn’t know the type, only sees a single element and assumes it’s a string. If there is more than a single element, PHP assumes it’s an array. This has caused me some serious ball aches when I’m expecting an array with a single element.
  • Booleans… yes, 1, or true? – sadly another area of ambiguity. I would argue, for consistency, people use the string representation (ie: true/false). However, that’s not always guaranteed and PHP evaluates “Yes” and “No” to 1 and 1 respectively. Not sure why, but quickly solved with a function to force “Yes”/”No” into the right form.

Integrating with Drupal

You could do what I did at first by writing some independent PHP which happens to interact with the same database as your Drupal site. That’s not so great as it doesn’t allow you to use many of good things Drupal has – such as the database abstraction library, variable system or access to your module code (code re-use!!).

A chap I work with (http://codeofrob.com) suggested bootstrapping Drupal to a minimal level to achieve this. So my SoapServer started to look like this:


< ?php
require_once './includes/bootstrap.inc';
/**
* Sixth bootstrap phase: load bootstrap.inc and module.inc, start
* the variable system and try to serve a page from the cache.
*/
drupal_bootstrap(DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE);
require_once './includes/common.inc';
$serviceName = $_GET['s'];
include_once './services/' . $serviceName . '/service.php';

Let's look at what's going on here. This file lives in the root of my site and acts as an interface to all my disparate services. More importantly, this file is bootstrapping Drupal up to the DRUPAL_BOOTSTRAP_LATE_PAGE_CACHE level which allows me to take advantage of many aspects of the system. Ultimately - this is a process which is comprised of several server side includes (for the various modules) and a few global variable loads. It stops short of fully bootstrapping Drupal so there is a performance bonus. Of course, if I needed access to something which requires a full bootstrap I would have to increase this.

Integration with Drupal as a fully fledged module

I've not looked into this fully, but I believe this would be the nicest, most usable format for a web services module to reside in. There are a number of concerns I have about this:

  1. Speed. The services module is not good (at the time of writing this) in terms of performance and functionality.
  2. Authorisation. Can you use anything other than forms based authentication? I currently use HTTP basic authentication or IP based access - I'm not sure this would be particularly easy to do directly in Drupal.
  3. Focus. Web services are massively varied - both in terms of messaging formats, protocols and payloads. Could a module with a fancy admin interface abstract some of this away? Difficult job if you ask me!

Wrapping up

I have to admit I've only glossed over this topic for now as I'm still understanding some parts of it. My intention is to provide an alternative look at integrating your Drupal site with some of the more monolithic pieces of an enterprise scale system.

Another great resource is this book: Pro PHP XML & Web Services (Books for Professionals by Professionals) - it was a real help in understanding SOAP, web services and more importantly - how I could get PHP to work with it all.

Share this post:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Fark
  • LinkedIn
  • MySpace
  • Slashdot
  • StumbleUpon
  • Twitter
Tagged with:
Aug 31

One of the fun tasks I had to look at recently was establishing a way of serving up multiple sites from a single installation of Drupal on a machine. This is incredibly useful for developers as it allows them a great deal of control over their local development sites with the convenience of not exclusively dealing with IP addresses… unless they want to :)

For those who might be new to this kind of concept, it’s fairly similar to how IIS might serve up multiple websites on a single IP address by analysing the host headers. However, we’re using Apache so we don’t need to worry about IIS’s interesting interpretation of GUI design.

Drupal.org has a fairly detailed overview of the process, but it’s missing a fairly useful step I believe which is how to alias another IP to your local loopback adapter. I’m running a copy of OS X 10.5 (Leopard to most) and if I ping the address 127.0.0.2 I don’t get a response. “That’s strange” you might say… and indeed in terms of convention, it is. If I do the same on a Windows or Linux box it immediately comes back with a chirpy:

64 bytes from 127.0.0.2: icmp_seq=3 ttl=64 time=0.059 ms

Lovely. So why does OS X not respond on this? It could well be down to Apple developers’ interpretation of Special use IP v4 Addresses.

Either way, it doesn’t respond by default. A temporary solution is this sudo ifconfig lo0 alias 127.0.0.x up – this assigns an additional address to the loopback adapter (lo0). It isn’t persistent and I haven’t found out exactly why at this stage, but it’s on the to do list :)

Configuring Apache

I’m assuming you’ve already got Apache set up with the prerequisites for Drupal. If not, get this done first as I’m not covering clean URLs, PHP configuration or installation basics…

Once you’ve got an IP address to listen on, you’ll need to get Apache listening on that address and serving up the right stuff. Your Apache config may differ to mine as implementations are different (sometimes considerably!) from distro to distro. Broadly speaking, you will want to find the place where you can configure your VirtualHosts. Sometimes that’s at the bottom of your httpd.conf file, other times it’ll be in a totally seperate directory – it varies depending on your server version and distro. Eeek.

Boiler plate VirtualHost config

Assuming you’re using MAMP on OS X – you can find the VirtualHosts section at the bottom of the /conf/apache/httpd.conf file. All you have to do is add the following declaration:

<VirtualHost 127.0.0.x:80>
DocumentRoot /path/to/your/drupal/root/
ServerName whatever.name.you.like
</VirtualHost>

Adding a DNS entry, or editing your local hosts file

If you can do this part through DNS on your network, that’s ace. If not, you can edit a file on your machine that binds a name to a given IP address. This little beauty lives under:

/private/etc/hosts

under OS X. I know, what a great path name! For more information, see Wikipedia’s entry for the hosts file.

On a new line, add your 127.0.0.x address plus the corresponding name you’re using for your VHost. Save the file, ping whatever.name.you.like and you should get a chirpy response on 127.0.0.x. If not, make sure you’ve not got any spelling mistakes or haven’t bound the alias improperly. If you screwed up aliasing… using the ifconfig -alias command to remove it.

Once that’s done – reboot Apache for the changes to kick in. Point your browser to your site name to check Apache is doing what it’s meant to do.

Configure Drupal

Ok, so by now you should have a web site that is serving up Drupal which should be prompting you to install a new site. Nice.

The next step is to add an entry into the /drupal/sites/ folder. Create a folder in this for your site, eg: whatever.name.you.like – it’s really important you’re consistent as if the folder name doesn’t match what’s in the host header Drupal won’t know which site to serve up and defaults to the /drupal/sites/default/ folder.

Once you’ve created your folder, all you need to do is copy the settings.php file from another site, or the default.settings.php file under /drupal/sites/default/ into whatever.name.you.like/settings.php.

After that, edit the file, complete the $db_url (and if necessary – the $base_url) for your specific database, save the file and browse to http://whatever.name.you.like

If all has been done correctly you should now be looking at a separate site being served up by a single instance of Drupal.

Summary

This approach is really useful, because if you have a lot of sites and/or clients you can run all those sites locally. All you need is:

  • A database per site
  • A free IP address on your loopback adapter
  • An entry in your hosts file or DNS for your site name
  • An Apache VirtualHost
  • A sites folder for Drupal with the same name

Other considerations

  • You don’t have to use a loopback adapter. It could be another network card in your machine, or indeed another machine.
  • You don’t have to use site names – you can stick with IP addresses too.
  • Drupal settings.php files are just that – PHP files. If you’ve got loads of sites, consider centralising all site settings into a single file and using the require_once() function to control which files are loaded, depending on which site is being requested.

Share this post:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Fark
  • LinkedIn
  • MySpace
  • Slashdot
  • StumbleUpon
  • Twitter
Tagged with:
Aug 31

So recently I’ve been spending a lot of time working on a data migration process which involves a heavy amount of BASH scripting. Consequently I’ve been spending most of my time with my laptop and it’s OpenSUSE 11.1 installation, because Cygwin just won’t cut the mustard for what I need it.

Part of the work involves converting a MS SQL Server 2005 database to CSV format, for use in conjuction with our target MySQL database.

bcp… or not to bcp…

bcp is a great application – it’s fast and doesn’t clog up the db logs,  but it’s a tad tricky to get to grips with. Even more so in my case where the source DB had some very strange collation issues resulting in some irritating cases of CSV data not being generated correctly. Amazing how many email addresses contain pipes (|) or double quotes etc! I’m not necessarily knocking bcp here – I believe our source database was a bit of a mess and is most likely the point of failure in the operation.

Enter Mono…

Decidedly fed up with bcp queries that just don’t output reliable CSV data, I quickly wrote a C# application using MonoDevelop to extract my data and ‘massage’ it into the format I needed. In many cases this included some minor transformations which would have been very fiddly using T-SQL alone. It was a very strange experience to write C# under a non-Windows OS, and even stranger to see it run and perform much like it would using my office PC.

It’s been a while since I last looked at Mono when it was very much less developed, but I was very impressed by the quality of the MonoDevelop IDE. Not only did it support the expected ‘intellisense’ and debugger it also came with XSP – a lightweight web server that is able to host ASP.NET web apps under a non Windows OS.

I was surprised again when I saw just how much software has been built using Mono.

Turnaround time

Although my dabble with Mono isn’t exactly a groundbreaking test drive by any means, I was hugely impressed at the progress that has been made in terms of implementing an open source, .NET development framework. It made my life easier in terms of allowing me to work on multiple platform technology on a single machine without the need for a VM – which is what I’ve been using in the past. Overall – less time spent faffing with a dodgy database and ultimately a major task delivered on time. Whoop!

Your thoughts…

Have you worked with Mono lately? Do you think it’s a good thing, or should the effort be spent on improving existing .NET technology on the Windows platform? I’d be interested to see what seasoned developers think of this.

Share this post:
  • Print
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Fark
  • LinkedIn
  • MySpace
  • Slashdot
  • StumbleUpon
  • Twitter
Tagged with:
preload preload preload