Tuesday 2 November 2010

Turn off that LightSwitch, and Let's Get Naked!

So, no sooner had I blogged about VS Lightswitch (I mean literally the next morning) I discover that there is a whole architectural pattern out there that tackles the problem that I have been considering developing a framework for  - Naked Objects. The shame of my arrogance (mixed with some naivety I concede) to think that I may be the first out there to strive for, well not so much a Silver Bullet, but well, just something better for such architectural commonality.

Richard Pawson coined the term Naked Objects when he had the vision of exposing domain objects to the UI (OOUI).  Naked Objects was the result of his Ph.D. thesis, written a few years ago when I was still trying to get to grips with proper OOAD/P and .NET 1.1.

The same curiousity that got me intrigued by VS LightSwitch had me interested in Naked Objects, that is a sound belief in DDD; the presentation, data (service and whatever other) layers are simply scaffolding for the domain. Get the domain correct, your app from a business representation perspective will be correct. As a software engineer that is the most difficult thing to get correct, and is the thing that is most likely to make or break the success of your project/app.
 
UI Designers of course argue different, arguing that a good UI is critical and fundamental to a successful application (it is this stick that Larry Constantine beat the Naked Objects concept with some time back.
I am not entirely unsympathetic to their stance and I certainly think a good UI helps, but without a sound domain model a fancy UI is worthless.  The same cannot be argued for the reverse (I know since I've developed a few such apps, a good few of which were deemed a success and are still in operation today).

Interestingly, Pawson has recounted how Trygve Reenskaug himself had stated that the Naked Objects pattern was the ultimate end game to his Model-View-Controller pattern.  This certainly gives weight and validity to Naked Objects as an architectural pattern (in the West of Scotland we call this "hawners").

Of course Pawson has went on to develop his own framework for his Naked Objects pattern, namely Naked Objects and more specifically Naked Objects MVC for .NET, utilising the ASP.NET MVC 2.0 technology.  The Naked Objects MVC framework is interesting from a number of points of view:
  • The Naked Objects framework is solely centered on domain objects as indeed Pawson claims. In other words, there is no code needed from you the developer, for the presentation layer or the data layer, leaving you to concentrate on getting your domain model correct.  If Naked Objects and VS LightSwitch were ever in direct competition, in my opinion, Naked Objects would win for this reason alone. Even although the Entity Framework (EF) Code-First CTP was released before VS LightSwitch beta, VS LightSwitch is still driven from the data model, suggesting that the intended user demographic of VS LightSwitch is pre-dominently people with no proper OO experience.  Naked Objects on the other hand is clearly a product for OO developers.
  • The domain objects are also true POCOs, meaning that you can port your domain model to another framework or to your own custom framework/application at a later date if you decide the output of Naked Objects is not to your liking without any rework to your domain.
  • The Data Layer of Naked Objects is build on the Code-First component of EF, which doesn't require any data modelling or data access code, you hook up your domain model and away you go.
  • The Presentation Layer uses reflection to obtain not just the state, but also the behaviour of your domain objects in order to display the functionality on offer in your UI.  This means that there is only actually a few generic views and controllers needed in the framework in order to render this behaviour and state at run time.
  • The Naked Objects MVC product also allows you to customise your views, meaning the end result doesn't have to be a set of similar looking views obviously generated from boiler plates. Indeed, you can add on your own views to extend and customise your UI further if necessary.  (My guess is that this is Pawson's reply to Constantine's criticism's of generated, standard, no-design UIs.)
However, there are a couple of concerns I have about Naked Objects MVC at first glance:
  • The UI uses terminology such as "Create Instance", which is great for technical people, but it means business users need to learn a new set of terminology to use your application.  To me, this seems to contradict the Ubiquitous Language so keenly advocated by DDD.
  • The use of reflection for all domain objects in order to build dynamic pages at runtime does concern me a little with regard to performance. Depending on the complexity of your objects and/or intended UI page there is a possibility that you may end up with a performance problem in your web application. (I am willing to be set on this one if I have it wrong.)
  • The data layer is built on a CTP of EF.  Admittedly, CTP4 does seem to have made major advances for the reputation of the EF as an ORM, but I know my boss isn't interested in running production applications against CTPs any time soon.
  • Pawson also claims that you have flexibility in your application by allowing you to customise your Views not just by stylesheets, but by use of a raft of very helpful HTML helper classes as part of the Naked Objects framework.  He also makes a case for ultimate flexibility as the framework allows you to use your own custom Views and with every likelihood your own Controllers also.  Personally, I'm not convinced by this as a selling point of Naked Objects MVC because if you take away the Views and Controllers you have cranked yourself, for all intents and purposes you are left with the domain model you also created and the EF CTP.
As a final observation Pawson claims that Naked Objects works well for sovereign applications but it does not lend itself very well to transient applications.  Personally, I don't think that application posture is as applicable today as it once was; today's end users don't just use software as a job tool, but as a medium that is part of their day-to-day social interaction. As a result, their expectations are higher and sub-consciously or otherwise, the expectation (no matter if they use it 7 hours per day or just in passing) is not just on how it looks, or what functionality it offers but how it actually behaves (usability).  If the UI generated by the framework is rigid in structure and not intuitive to users, this could limit the adoption of Naked Objects frameworks such as Naked Objects MVC.

My previous observation of having a sound domain model and not necessarily having a good UI should be qualified by the times in which this software was written.Going back to touch on Constantine's point about the UI being vital; end users these days are not expecting a good UI as apposed to a good functionally sound domain implementation, or even vice versa - they want both.

3 comments:

  1. Thanks for posting this blog. Like you, I like LightSwitch - having been given a preview by Jay Schmeltzer more than a year ago. But I like Naked Objects more!

    I would just like to respond to the various reservations that you listed though:

    1. 'The UI uses terminology such as "Create Instance"...' The 'Create Instance' and similar actions you refer to apply only if you use the SimpleRepository class, which is intended only in the very earliest stages of prototyping - quickly to be replaced by your own repository/factory methods.

    2. 'The use of reflection for all domain objects in order to build dynamic pages at runtime does concern me a little with regard to performance'. This has often been predicted as an issue but all I can say is that we have run very large, very complex applications and reflection has never turned out to be a performance issue. (That's not to say we've never hit any performance issues: rather that the performance issues have been much more conventional, such as database queries or serialisation). Possibly this is because most of the reflection is done up-front when you start up the application and Naked Objects builds its meta model.

    3. 'The data layer is built on a CTP of EF.' That's true only if you are working 'Code First'. The real applications that we build using Naked Objects today use EF in 'Model First' or 'Database First' mode, which does not require any CTP.

    4. I didn't fully understand your last reservation. I think that you are saying that if you write your own Views and Controllers and Domain Model then what is Naked Objects really doing for you? I have two responses to that. The first point is that Naked Objects MVC is a whole lot more than just a generic controller and some views. The two key parts are the object management framework, and the reflector. Let me give just one example. In a large application where your domain model is split over several model projects, using EF you would normally have to manage each of the contexts explicilitly. With Naked Objects that is completely transparent. However, the second point is that I agree that if you want entirely custom views and controllers then the case for Naked Objects is much lower. But a much more common case is to have an application that uses the generic Views/Controller for most of the functionality but custom versions for small slices.

    Richard

    ReplyDelete
  2. Richard

    Thanks for your response.

    1. You know, after blogging last night I did think about this one and whether I was being a little premature or over-assuming. My apologies for jumping the gun, and thanks for setting me straight.

    3. I now understand your framework a little better. For me, I would always go with Code-First and the lack-there-of with EF up until CTP4 has stopped me from even taking the wrapper off. The fact that Microsoft has labelled the data-first perspective as "model-first" is a misnomer in my opinion.

    4. I guess what I was trying to say was what you explained with your 2nd response (if you want entirely custom views and controllers then the case for Naked Objects is much lower). You just put is so much more eloquently than I did!

    Interesting times ahead, and I have to say that I'm tempted to give Naked Objects MVC a try out in a real world environment. Am I correct in saying the licensing model allows you to use Naked Objects in your dev environment, but you simply can't deploy without buying a licence?

    One thing I forgot to mention in the original post was that I can see one scenario where Naked Objects would definitely fit, and that is when you want to upgrade your technology platform. Naked Objects would help you replace a legacy application quickly.

    ReplyDelete
  3. "Am I correct in saying the licensing model allows you to use Naked Objects in your dev environment, but you simply can't deploy without buying a licence?"

    That is correct.

    ReplyDelete