Tuesday, March 23, 2010

Introducing NakedPhp 0.1

I dedicated a bit of my time to develop a port of the Naked Objects framework for Java, which I named NakedPhp. Essentially, the Naked Objects pattern is the automatic animation of an object-oriented Domain Model via the generation of the user interface and of the persistence layer, both commonly sources of duplication and kitchen sinks for business logic. Naked Objects is a radical reinterpretation of MVC, where the Model is at the center and the other layers are inferred from it and customized. You will call methods and move objects around in such an application.
This is NOT yet another Php framework: NakedPhp leverages Zend Framework and Doctrine for all the stuff you would expect a normal framework to do.

Previous related posts
Naked objects, DDD and the user interface
A look at technical question on Naked Objects
Naked objects in Php
Where is business logic?

NakedPhp applies the same pattern to Php applications, and I have tagged its 0.1 release today (this is an alpha release.) The Api is borrowed from the original Java Naked Objects framework. This is by no means a complete framework, but it's a good start since its basic CRUD functions are already working.
NakedPhp integrates Zend Framework 1.x for the user interface management and Doctrine 2 for the persistence layer. As for all Doctrine 2 applications, it requires Php 5.3, which is why I don't have an online demo right now.
Links
NakedPhp 0.1 on SourceForge
Bug tracker
Git repository (view it online):
git clone git://nakedphp.git.sourceforge.net/gitroot/nakedphp/nakedphp

A simple example application realized with NakedPhp is provided in the released package, which uses a bundled sqlite database. An application for NakedPhp is just a plain old Zend Framework application with a controller that inherits from NakedPhp\Mvc\Controller, and that inits the Entitymanagerfactory (related to Doctrine 2) and Nakedphp resources with the mandatory options like the path to the model classes's folder. NakedPhp is very liberal about what you do in your application: it only generates the first-step views and leaves you the layout for customization.
I will write related documentation about how to hook NakedPhp in an application in the next days which I will store along with the code in the Git repository on SourceForge.
The behavior of the generated interface (which is not scaffolding: it's intended for actual use and not for being modified) is driven by annotations on the model classes and by methods with special names. For example, properties available on Entity objects are inferred from getters and their modification is possible if correspondent setters are present. Other methods are called when available to determine automatic hiding of properties and methods (hideName(), hideFindAllCities()), validation of data and so on.

The sample application provides a basic workflow for an hypothetical Domain Model for shops, pubs, or similar places. You can create Cities and Places, modify their properties, save them in the database, then clear the session and retrieve them via service classes. This is a direct interface to the Domain Model, without translations.
Intructions are included in the release for the super-simple setup of the example application (essentially running phing build-example).

Let me know if you are interested in a framework that writes the user interface for you. I know that the Naked Objects pattern is not appropriate for all situations, but the goal is to simplify the presentation of the real Domain Model to the user for certain kinds of model-driven applications. It is also providing a prototyping interface for Domain-Driven Design in Php.

7 comments:

Dan said...

Any screenshots, Giorgio?

Anonymous said...

i very love your own posting taste, very exciting.
don't quit as well as keep writing seeing that it simply good worth to look through it.
impatient to view alot more of your writing, goodbye :)

Avi Block said...

Isn't CRUD missing the point of DDD?

Giorgio said...

@Dan: I'll make some when I get home. Good idea. :)
@Avi: the Naked Objects pattern prescribe behaviorally complete objects, so other than CRUD operations you can actually call any exposed method on any object both in Naked Objects for Java and NakedPhp... but I had to start from somewhere and managing the creation and storage of objects is a good point. It is not classical scaffolding CRUD, for example: only property with setters are available for editing; the objects are created by Factory Methods; they are listed by Repository methods defined in the model; only the properties exposed by getters are visible; etc.

Jeboy said...

@giorgio first, I seconded the idea of only those properties that are exposed by getters are visible and can only be modified by providing setters, this is in context and makes sense.

Jeboy said...

@giorgio second, is the Naked Object's (NakedPhp) purpose only to simplify presentation, that is generating the UI automatically based on the domain model?

Giorgio said...

The Naked Objects pattern is about direct manipulation of the domain objects, without any translation. This means that the UI can be autogenerated from a sufficiently smart framework, but also that no translation layer (like a Presentation Model) is put over the domain model. It's not viable for every application, of course.

ShareThis