Saturday, 31 January 2009

Apparently Glenn Block distributes malware.

[Update: What I thought was an individual funny incident was in fact for the whole internet. See the The Register article about the snafu.]

We know that the MEF guys are a bit wicked, but that they would distribute malware is way over the top. If you don’t believe me, look what Google just showed me.

Glenn Block distributes malware

Monday, 26 January 2009

We did warn you…

Received in my inbox today from an ex-colleague. This made me smile.

Subject: ARGH THE FUCKIN ENTITY FRAMEWORK!
Received: 29 January 2009

What a load of s******t
Sorry had to get that off my chest...

 

Received: 26 January 2009 11:27

Lead Dev - "Oh it's really good you just drag and drop your tables on and it creates all your classes for you"
N - "F********* OFF"

 

Received: 26 January 2009 13:08

Just spent the last 2.5 hours hand editing some XML.  Probably due to a bug in the "designer" but who knows.
Bring back NHibernate :-(

No comment.

Sunday, 25 January 2009

How to fix Microsoft’s Two-Tier Service Application Scenario (REST)

Microsoft has released a beta version of a guidance talking about REST in 2-tier applications. I’ve had many rants about Microsoft’s attitude towards REST and the marketing branding they put on (some teams being much worse than others by arrogantly or unknowingly putting the word REST on the name of their framework).

This entry is no such rant, but an effort to outreach to the authors. The document has issues, but it is my belief that with the right corrections, it could be made accurate.

Of the importance of patterns

First thing come first, let’s talk about patterns. Here’s the definition for design pattern from Wikipedia.

A design pattern [..] is a formal way of documenting a solution to a design problem in a particular field of expertise.

From this, we would expect a pattern that is referenced or talked about to have been documented, contextual to a field of expertise and used to solve a design problem.

Let’s review what patterns are referenced from the document, as it will help us later to analyze the proposed guidance. Whenever a pattern is provided without references, I either assume the first documentation of a pattern as applying, or try, as a reader would, to google it and find what it could mean.

The router pattern

I assume this mean the pattern by which a URI is mapped to a component processing the request. My searching has returned an IBM article defining the router pattern as “[…] routing requests to specific pieces of business logic based on some defined criteria”. Anyone trying to search for the router pattern will be inundated with various definitions and hundreds of sub-patterns (content-based router pattern, dynamic router pattern, etc). By failing to reference which variant of a pattern is being included, and where the documentation for such pattern is located, a reader will be none the wiser. And they are, after all, looking for guidance.

The proposed fix: either reference which pattern is being talked about, or document what use you refer to.

The REST Entity pattern

A quick google search for “REST Entity pattern” will return only two results, the first one being the proposed guidance, and the second one being a presentation by Ganes Gunasegaran on a site called sagework, available as a pdf. That presentation does provide one slide defining the pattern as follows.

  • Resource can be read with a GET operation
  • Resource can be changed only by PUT and DELETE operations

Now further detective work returns a MindTouch page defining the Entity pattern. Reading the rest of Ganes’ presentation, it becomes very obvious that the rest of the patterns he presents are just pulled out of the MindTouch REST patterns page. And reading the description of the patterns in the Microsoft document, you will also notice the exact same definitions.

We now have a documented pattern, within the correct field of expertise, aka MindTouch. However, a quick search for the use of this pattern being referenced outside of MindTouch’s web presence returns very little. Furthermore, it doesn’t define the problem it is designed to solve. I would question the validity of such a pattern.

The proposed fix: again, reference the correct pattern you intended to include to start with, and keep it’s original name. In this specific instance, also make sure that this pattern matches the definition of what a pattern is, or redefine and document such a pattern yourself (or get the original authors to do it).

The entity translator pattern

This one is defined by Microsoft themselves, in the context of web services:

Implement an entity translator that transforms message data types to business types for requests and reverses the transformation for responses.

The Facade pattern

See http://en.wikipedia.org/wiki/Facade_pattern, “A facade is an object that provides a simplified interface to a larger body of code, such as a class library.” or P&P Pattlets “Provides a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.”

The Repository pattern

Fowler:  “A Repository mediates between the domain and data mapping layers, acting like an in-memory domain object collection.”

Domain Entity pattern

A google search on Domain Entity Pattern triggers an interesting result, from the same authors as the proposed guidance: Three-Tier Web Application Scenario. If you have a look at both, you will see that a lot of recycling between the two guidance documents has happened. Now of course, neither documents match our definition of a design pattern. An obscure page on wikipedia says that domain entities “are a super-set of Data Layer Entities or Data Transfer Objects, and may aggregate zero or more DLEs/DTOs” which I’ll consider compatible with the P&P definition. The naming is however confusing, and not widely in use.

Quick fix: If you want to use the same definition, aka “A set of objects modeled after a domain that represents the relationship between entities in the domain but do not contain behavior or rules related to the entities”, call it what the rest of the world calls them: an Anemic Data Model. If you are happy with the anti-pattern but are worried that it may reflect badly on the practices you suggest, call it a Data Model. If what you really mean is that it’s a DTO with the same definition as wikipedia, call it a DTO and change your definition to match what a DTO is.

Lets review the guidance

Now that we’ve cleared out the confusion introduced by the guidance document’s use of patterns, let’s review the various layers and how they relate to REST.

The lack of context

The first thing that strikes me is the complete lack of any references to recognized litterature introducing REST. If you’re going to talk about an architecture, just like with a pattern, you *have* to provide the references.

Fix: Introduce REST and Roy Fielding’s PhD thesis. Provide links to well-known restafarian web-sites, such as the excellent http://restpatterns.org that provides guidance in implementing rest architectures.

The routing

In a RESTful architecture, everything is mapped as a Resource. This is the thing you want to operate upon. Anything can be a resource. For the sake of this entry, let’s imagine that I define a resource as being my computer’s hard drive, the physical hardware equipment that sits inside my laptop’s case.

To be able to operate on a resource, I need to be able to address it. And in REST, I can do so by giving it an identifier. In the case of HTTP, this is a URI. Let’s give a URI to my hard-drive: http://www.serialseb.com/harddrive/fujitsu.

If I type the Uri in my browser, said browser will send an http request to my server. The server is now responsible for knowing what the heck it is that I want. This process is called URI dereferencing. It’s a big word, but it is what it is and what the common definition is. It’s the process by which a URI is matched to a Resource.

It is assumed that a handler will be responsible for doing this dereferencing process. Once the resource has been dereferenced, it is time to do something with it, and this is what an http method such as GET or POST does. It defines the operation that is to be done against the Resource.

We now have the elements to understand what Microsoft talks about when they mention the Router idea. In their scenario, the router uses both the Identifier and the Operation to call some bit of code, commonly referred to as a handler. It is a sad fact that Microsoft chooses, in a REST document, to disregard completely the existing and meaningful descriptions of a web operation.

Resources, representations and the “REST entity”

Let’s say that I want to add a song I just heard on the radio to my hard-drive. As you probably know, music is heard because the air between the singer’s vocal chords and my tympanic membrane vibrates. This vibration gets turned into an electrical signal and gets processed by my brain to let me make sense of the words that were transported as a vibration. When talking with a human, I would identify the song I just head as “If you seek Amy from Britney Spears”. If I talk with a computer,  I may need to assign it a name too, so let’s do that. http://www.britneyspears.com/songs/ifyouseekamy.

As far as I know, my hard-drive cannot persist air vibrations to disk. We need a binary stream, because that’s what hard-drives can persist. That binary stream would probably be an mp3 downloaded from a music service. This byte stream is not the song itself (as in the air vibrating), it’s a file in binary format that my computer can process. If the song is a resource, the mp3 file is a Representation of that resource.

What this means for my adding that file to my hard drive is that to download the song, I would need an mp3 file. If I dereference the URI for the song, I may get a representation of this song as an mp3 file. I never transmit the resource itself.

This is why REST is called Representational State Transfer. Now that I have my file, when I want to add it to my hard drive, I could do a POST to http://www.serialseb.com/harddrive/fujitsu and include the Representation of the song. I have effectively changed the state of my resource (my hard drive, the physical thing) by sending it a representation (the mp3 file).

Microsoft says “In REST a resource is an object that represents a specific state”. As you can probably tell by now, my hard-drive doesn’t represent a state, it has a state because it is a resource. I changed it’s state by sending it a Representation. I didn’t sing to my hard-drive to make it persist an mp3.

Quick fix: clear-up the definition to “In REST a resource is a thing that can have state. You can change that state by performing operations on the resources through transferring representations.” You can probably make it more obvious by stipulating that you recommend your business entities to be your resources, acted upon by a representation (your DataContract).

Furthermore, remember that MindTouch’s definition of a REST entity that Microsoft has included is defined A Resource that gets modified only through PUT and DELETE.

We’ve seen that a representation has by definition no behavior, as it is only a byte stream, and cannot be operated upon. Because Microsoft has specified that their use of the word REST Entity is a representation of a resource, it becomes obvious that they have wrongly applied the pattern proposed by MindTouch, which applies to resources.

Confusing resources and representations is a common problem for people new to REST, and one Microsoft has fallen into.

Fix: Drop the Entity (REST) naming. You have misunderstood the original meaning of the pattern. What you are talking about is a Representation that you would probably advise to be a DataContract.

Finally, we reach the Entity Translator. Microsoft proposes that such a component “translate[s] between business entities and REST entities exposed by the service”. We’ve now seen that REST entities are in fact representations. What is proposed here is a component that can turn a resource (aka the business entity) into a representation (aka your DataContract).

It is not surprising then that the definition “Resources exposed by the service represent an external contract while business entities are internal to the service” is inaccurate.

Fix: “Resources exposed by the service can only be retrieved and modified through Representations, which represent an external contract”.

And indeed, translators are required to move data from one format (your representation) to another (your business entity as an object living in memory).

The business layer

I won’t comment much on the architectural choices of Transaction Scripts and Facades, I have little interest in entering this debate. I will however take note of the definition of the service implementation (which, as we’ve seen, is usually called a handler):

“The service implementation is responsible for translating between external contracts and internal entities and then passing the request on to the business layer façade.”

This seems to indicate that the facade deals with external contracts, but the Entity Translator has already been introduced to deal with such a translation. This seems redundant and is probably a mistake.

Furthermore, if a business facade implements the logic of acting upon a business entity, and the translator maps between datacontracts and business entities, it would seem to me that you’d end up with an anemic service implementation. The only reason I can think of is to map Resource operations to business processes.

While I think such an infrastructure is redundant, here’s a proposed fix: “The service implementation is responsible for mapping operations on resources to business processes in your layer facade.”

Do we really need Messages?

I’m very confused by the proposed implementation of the business layer.

REST over http is often considered to be a Resource-Oriented architecture. The first, if not the most fundamental, design issue you will face is modeling your resources well. Like any domain modeling activity, this is not an easy process to get done right.

Provided you have thought of your architecture in terms of exposed resources, you then spend some time defining your representations, aka what goes on the wire. As we’ve seen, that will end up being your DataContract design.

There is a lot of inherent knowledge in resource instances: they have al the information you need to process your request. When I send a POST to http://www.serialseb.com/harddrives/fujitsu, the request contains the representation of the file I want to persist, the location in which to persist it. Nothing outside of that operation is required for the processing of the operation to happen.

Why then would one wrap the notion of adding a file onto a hard-drive into a message, pass it to a facade that dispatches the message, to finally get processed by an operation that reads the message to act upon data structures?

Any time you convert between various data structures, you introduce more complexity. Anytime you de-normalize and renormalize, you introduce potential bugs. The proposed solution does the following:

  1. Get the request, and transform the datacontract into an instance of the business entity
  2. Encapsulate the business entity into an untyped Message and pass it to a Facade
  3. The facade reads the message to dispatch it to a process
  4. The process opens-up the message to get back to the business entity in the operation and call the entity framework.

You have achieved absolutely nothing by having a facade. All the data that was required to dispatch the request to a business process was already in the service! You end up with an anemic service that does little if nothing, a business facade that’s not really a facade but a broker, and a business process that has to open-up the encapsulation format for no valid reason at all.

As this document is supposed to be a guidance as to best architect a solution, the proposed solution is just not acceptable. Proposing an anemic service is a symptom of a bigger problem: the guidance doesn’t talk at all about resource modeling, and assume that there will be redundant services mapping to the same business process, aka two services doing the same thing.

It looks to me as trying to slap a message oriented architecture in which the endpoint receives a message to be processed, on a resource-oriented architecture in which the endpoint is the resource on which to apply an operation. The semantics of a resource-oriented architecture eliminate the need for message dispatching.

Proposed fix: Get rid of the business facade as is, and let the service call the business process itself. The message is redundant.

Bad practice: Promote(object[] data) is just bad practice as you’ve now removed any single bit of semantics that were associated with the process.

Alternative fix

There is a simple alternative fix that can be applied to the document, by removing the REST references and describing it as a POX architecture.

Conclusion

I do not know if this guidance is the result of an incomplete understanding of REST architectures (which is quite widespread in Microsoft’s literature) or an attempt at over-simplification.

What I do know is that much needs to be modified before this document can be proposed as a best practice for delivering a RESTful solution. It lacks the proper and accepted terminology, completely bypasses architectural concerns around resource modeling, misrepresent what a REST architecture would look like. It also ignores caching (one of the REST constraints) and proposes an architecture that would make leveraging such caching difficult.

I also call for P&P to involve the communities that have formed around topics such as DDD and REST when they deliver beta versions of their guidance documents. Those errors I’ve highlighted could have been taken care of much earlier in the process, and save me the 5 hours I spent this afternoon writing this blog entry. That this is considered a beta 2 is however completely unacceptable.

Hopefully we will see an updated version of this guidance. If not, hopefully my blog entry will have enough google juice to start fixing the inaccuracies that Microsoft seems to spread about REST way too often.

Tuesday, 20 January 2009

Does history repeats itself?

I’ve been following Opera’s reactions to the EU antitrust regulations against Microsoft’s bundling IE in windows, which they have been calling for…

Anyone remembers what happened last time a browser vendor tried to leverage antitrust laws to explain their sinking (or in the case of Opera, their fairly constant and unimpressive) market share? No?

Bah.

Monday, 19 January 2009

User Experience is not only about the nice logo…

image

Enough said me thinks.

Wednesday, 7 January 2009

Alt.net Beers #5 – 12th of January (yes, I know it’s next Monday…)

With all the festive excitements, I really thought I had announced this when I put the date down on http://ukdotnet.ning.com but I obviously didn’t.

So as (nearly) every months, we’ll be holding our monthly alt.net meeting in Soho next Monday, with tester extraordinaire and dynamic language addict Ben Hall. Please register on http://ukdotnet.ning.com/events/altnet-london-beers-5 so I know how many of you to expect. We also usually wait for everyone to have arrived before starting, and if you’re not on the list we can’t start!

The format is a mini openconf-style session. Here are the rules, slightly changed to learn from the previous event.

  • Arrivals, discussion and socializing between 18:00 and 19:15, with one necessity: find the topic of the day and find the questions that we will try to answer.
  • Between 19:15 and 19:30 we will vote on the subject we want to talk about, in two rounds, by raising hands.
  • At 19:30 the big clock will start ticking for a time-boxed 60 minutes workshop on the subject. The goal is simple: can we as a group get insight and progress our understanding of the problem
  • If the subject stales, the subject can switch to a new one, the second down in the list of votes.
  • At 20:30 the clock will ring and that will be it of the conversation. We’ll do a roundtable of each person where I’ll be asking you to decide of an action you’re going to take based on the discussion (something new we didn’t do until now).
  • Then everyone will write it down for wiki publication, everybody will be able to continue the discussion in the pub or wherever they see fit.

The location

We should be able to hold our reunion at the Tequilla lounge bar, like last time. I’ll have confirmation tomorrow, and if not I’ll announce here a new location.

Tuesday, 6 January 2009

Fluent-nhibernate and nvarchar(max)

As you may know, text and ntext types are being depreciated in sql server 2005+, and replaced by varchar(max) and nvarchar(max).

If like me you rely on nhibernate to generate your database, the trick for getting nvarchar(max) is to set the length of the field to 10000. I wrote a small extension method to do this.

    public static class PropertyMapExtensions

    {

        public static PropertyMap WithMaxSize(this PropertyMap map)

        {

            return map.WithLengthOf(10000);

        }

    }

I use the automapping functionality, so this is what it then looks like.

            AutoPersistenceModel.MapEntitiesFromAssemblyOf<Movie>()

                .Where(t => t.Namespace == (typeof(Entity).Namespace) && !t.IsAbstract && t.IsPublic)

 

                .ForTypesThatDeriveFrom<Movie>(x =>

                {

                    x.Map(m => m.LongSynopsis).WithMaxSize();

                    x.Map(m => m.Notes).WithMaxSize();

                })

                .Configure(configuration);

And if you’re interested in all things fluent-nhibernate, why not come and listen to my talk about it on the 22nd of January at SkillsMatter?

Sunday, 4 January 2009

Using extension methods on null objects

One feature that is often disregarded in extension methods is that they get called for null objects. While instrumenting parts of OpenRasta, I faced the problem of having some classes not always created through the container. As such, sometimes their Log property would be null, and each call to a logging method would have to check for that condition. After doing a few back-and-forth, I settled on using an extension method, coupled with a null object.

Take a simple class, and add a Log property.

public class LoggingClass

{

    public ILogger Log { get; set; }

}

Then, create a simple extension method on the ILogger, and return a null object

        public static ILogger Safe(this ILogger logger)

        {

            if (logger == null)

                return NullLogger.Instance;

            else

                return logger;

        }

Now all the logging code can simply call the Safe() method to ensure that, in the absence of a logger, a null object will be used instead. The code using it looks like the following.

        public void DoSomething()

        {

            Log.Safe().WriteDebug("I did something!");

        }

Voila!

Friday, 2 January 2009

OpenRasta status update

This entry may be outdated. For the latest updates on openrasta, see www.openrasta.com.

Already a month since the last updates, and I’ve been churning quite a bit of code. Most of it has been to finish extending the support for multipart types, which was blocking a couple of scenarios (html file upload and silverlight bidirectional http being the two main ones).

Infrastructure

  • OpenRastaHandler is now a handler factory.
  • Added a logging infrastructure and some initial instrumentation of the code.
  • All the ambient data is now cleanly separated and requires an IContextStore (Think HttpContext.Current.Items)

IO

  • I thought I’d push the sub-stream implementation to later, but it is now done. Only one stream of a request exists, and when using a multipart encoding, a sub-stream will be created that lets you read within a multipart boundary transparently. This enables quite amazing scenarios.
  • StreamWriter is a bad boy, it closes a stream automatically when disposed. Added a DeterministicStreamWriter that lets you control the behavior of the stream on close.

Dependency Injection

  • Not much there, code has been expanded a bit to resolve properties too.

Html Forms

  • Support is now complete and stable.

Multipart support

  • This baby took much longer than I thought, but it has incredible powers. Multiparts as used in html forms are now fully supported, including files etc.
  • Codecs are recursive. That means each part in a multipart can itself be parsed by another codec. Think passing around form data, xml and binary all in one go, without coding involved.
  • Quoted-printable and base64 encodings in form names are also supported.

Complex-type construction

  • The default type construction algorithm used for both mutlipart/form-data and aplication/x-www-url-formencoded now supports both type strings and variable names. That means that for OperationResult Post(Customer c), you can now use c.FirstName or Customer.FirstName as the field-name, as well as using only the property name (aka firstname).
  • TODO: Sadly some of the code is still not shared between those two codecs, this is the next refactoring. Need to find a clean model to extract all this stuff into. Expect to see a lot of this code extracted in parameter binders, and an extension for codecs providing key/value pairs.  [Update: Done]

Markup

  • There’s new code for markup generation. The infrastructure is all based on chained extension methods for configuration, giving a nice fluent-like taste (except for the form tag, but can’t do anything about that).

Misc

  • Code coverage is slowly reaching 80%, but there’s still a lot of tests to write around error conditions.
  • There are some namespaces at 100%, which helped fix a bunch of errors in the internal dependency resolver.
  • More renames (MediaType has taken over from Content-Type nearly everywhere) and namespace reshuffling.

The code is getting more and more stable. Now I just need more testers and more people using OpenRasta. That, and a logo. And tutorials. And screencasts. Etc.