Sneak peek to simple integration between DMVCFramework and DORM

This is a really simple (not optimized and dirty) integration between the upcoming DMVCFramework (WebBroker based MVC framework) and DORM, “the Delphi ORM”.

This is the DMVCFramework controller with the relative mapping and methods. In the method “GetUsers” dorm is used to execute a select to the database using the sanitized parameter passed on the url.

unit UsersControllerU;

interface

uses MVCFramework, dorm;

type

  [MVCPath('/users')]
  TUsersController = class(TMVCController)
  strict private
    dormSession: TSession;

  strict protected
    procedure MVCControllerAfterCreate; override;
    procedure MVCControllerBeforeDestroy; override;

  public
    [MVCPath('/($id)')]
    [MVCHTTPMethod([httpGET])]
    procedure GetUsers(CTX: TWebContext);
  end;

implementation

uses
  dorm.loggers, dorm.adapters, dorm.Commons, UsersBO;

{ TUsersController }

procedure TUsersController.GetUsers(CTX: TWebContext);
var
  User: TUser;
begin
  User := dormSession.Load < TUser > (CTX.Request.ParamsAsInteger['id']);
  Render(User);
end;

procedure TUsersController.MVCControllerAfterCreate;
begin
  inherited;
  dormSession := TSession.CreateConfigured('dorm.conf', TdormEnvironment.deDevelopment);
end;

procedure TUsersController.MVCControllerBeforeDestroy;
begin
  dormSession.Free;
  inherited;
end;

end.

Now, if you run the application and go to http://localhost/users/1 (the server is running on port 80), you’ll get the following:

Stay tuned.

#3 “dorm, the Delphi ORM” bullettin

A veeery log time after the last dorm bullettin. But, as usual, I was been very busy on some projects (not only dorm) and the time goes by…

However, dorm has been extended, polished and improved over the last few months. Has been used in a couple other projects in my compoany (www.bittime.it).

So, here’s a small list of improvements and some other tips:

– ObjStatus support (more to come)

– TdormSession non-visual component. Check “samplesDelphiXE3TdormSession_Sample01formSample1.dproj”

– dorm is now in Continuous Integration (not for all supported databases, but I’m improving that)

– I’m integrating a JSON/DataSet/ObjectList mapper into dorm to be used in RESTful DataSnap/WebBroker (or not) http servers. More to come.

– Delphi XE3 Support

– Removes SuperObject as external lib. Now dorm uses an internal patched version.

– More demos added (Most noticeably id under “samplesDelphiXE3TODOManagerTODOManager.dproj”)

– Added samples using the new Visual LiveBinding feature in Delphi XE3

About the ObjSupport it’s worth to spend some words about it.

Since the beginning, dorm is completely “perisstence ignorant”: In other words, you can persiste what you want, you dont need to inherit from a specific class or implement a specific interface. This is a very powerful feature but make some internal dorm mechanism very complex. But I really WANT to have this feature, so I’ve added another “mode” to work with dorm: ObjStatus.

If dorm engine finds a property nemed “ObjStatus” of a specific type, it’ll use that property to track the object status (Clean, Dirty, Deleted).

In this way, I gained a *LOT* of speed compared to the prior version regarding persistence of complex objects graph.

eg.

procedure DoSomethingOnPersonByID(ID: Integer);
var
  p: TPerson;
begin
	p := Session.Load(ID);
	p.FirstName := 'Daniele';
	p.Car.Model := 'Civic';
	p.Phones.Add(TPhone.Create('555-555-33-22', 'Home'))
	Session.Persist(p); //generates insert, update, delete for related objects too
	p.Free;
end;

As Usual you can find the project code here https://code.google.com/p/delphi-orm/

#2 “dorm, the Delphi ORM” bullettin

This is the second post regarding a fast update on the last changes to the dorm project in terms of management and code.

  • Welcome to 2 new contributors: Marco Mottadelli and BraveCobra (this is the full list http://code.google.com/p/delphi-orm/people/list)
  • Added 2 new PersistentStrategy for MSSQLServer based on dbExpress (the 1st use the Embarcadero dbExpress driver, while the 2nd use the DevArt dbExpress driver)
  • Added another PersistentStrategy for MSSQLServer based on ADO (so also Delphi Professional users can use dorm with MSSQLServer)
  • The new mapping strategy is under development. There will be “3 levels” of mapping: Config File, RTTI Attributes and “Conventions Over Configuration” (CoC) (do you wanna check the code in dev branch? Click here)
  • Added more unittests
  • Roadmap updated
  • Introduction to dorm, now is a PDF document
  • The config file has been splitted in “Persistence” and “Mapping” for a more flexible configuration
  • Added packages (Thank you BraveCobra)
  • Added MappingCreator. Now you can create the mapping file using an existent database.
  • IWrapperList (a duck typed list) now is used EVERYWHERE! You can use whatever list from whatever library you want to use. The list must have only a specific methods named as following: Add, Clear, GetElement/GetItem, Count
  • Added FillList methods
There are plenty of new features still to come. Stay tuned!

#1 “dorm, the Delphi ORM” bullettin

This is the first post regarding a fast update on the last changes to the dorm project in terms of management and code.

  • Welcome to 2 new contributors: mrbar2000 and magnomp (this is the full list http://code.google.com/p/delphi-orm/people/list)
  • Added a PersistentStrategy for SQLite.
  • New Google Group related to dorm. Post your questions, comments and request here!
  • Added support for Generics Collections. No more needs of TdormCollection. You classes are completely decoupled from the framework.
  • A brand new mapping strategy is under development. There will be “3 levels” of mapping: Config File, RTTI Attributes and “Conventions Over Configuration” (CoC) (do you wanna check the code in dev branch? Click here)
  • Added more unittests
  • Roadmap updated
  • Initial support for MS SQLServer in a development branch (Thanks to Claudio Regonat)
There are plenty of new features still to come. Stay tuned!

Duck Typing in Delphi

During a new dorm feature development, I’m faced a nice problem:

I want to have a generic access to a “kind” of list

Let’s say:

procedure DoSomething(Obj: TMyListType);
begin
...
end;

But, I want to have that generic access without a Layer Supertype object, because I need to be able to use objects from other libraries or 3rd party. In this case traditional polimorphism is not usable, so I’ve opted for an interface…

procedure DoSomething(MyIntf: IMyListInterface);
begin
...
end;

Cool, but I want to have that access without any change to that object. So implement an interface is not a viable solution. This is particulary true because the generics data structure in Delphi do not implement an interface. Will be nice to have a fully interfaced list and dictionaries in a future Delphi version.

So, how I could implement a generic access to a generic list?

procedure DoSomething(MySomething: ???);
begin
...
end;

However, operations on that list are few and well known, so I’ve opted for a “DuckTyping“.

Introducing the duck
Introducing the duck

The name of the concept refers to the duck test, attributed to James Whitcomb Riley, which may be phrased as follows:

“When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck.”

So, in my case, I’ve created an Adapter object wich adapts the external interface (few, well know operations) to the “duck” inside it.

The adapter is useful because I can use the compiler checking that the RAW RTTI access doesn’t allow.

Write an “RTTI adapter” using the new Delphi RTTI, is very simple. This is the code of the class TDuckTypedList that allow to use any object as a “list”. How I can define that the object is actually a list?

The criteria is:

If the object has
– An “Add” method with one parameter of type TObject (or descendant);
– A “Count” property;
– A “GetItem” method that have an integer parameter and return an object;
– A “Clear” method;
then, that object is a list.

So I can write the following:

DuckObject := TDuckTypedList.Create(Coll);  //the adapter
for x := 0 to DuckObject.Count - 1 do
  DoSomething(DuckObject.GetItem(x));  

I’ve done some speed tests comparing this way to the classic static way, and the speed is almost the same because the RTTI lookup is cached in the constructor of the adapter. So, so far so good.

This solution is already in use inside the dorm code in a feature branch.

Full code is available in the dorm SVN

Any comments?

dorm, “The Delphi ORM”, officially published at ITDevCon

As all the attendees have seen, at the last ITDevCon, I’ve officially published dorm as an OpenSource project.

dorm, The Delphi ORM
dorm, The Delphi ORM

You can find the project on google code: http://code.google.com/p/delphi-orm/

An introduction to dorm is available on google docs.

dorm begins as a my personal project more than one year ago, and now is sponsored by bitTime Software that offers custom development, consultancy, mentoring and so on.

Why dorm?

These are my personal reasons of because I’ve started to write dorm:
I WANT an ORM for Delphi… but:
I dont want (or I cant) change my database
I want persistence ignorance
I want to access to the «metal»
I want to persist PODO
I don’t hate SQL, but I know that 90% of it is boilerplate and errors prone

First example

Insert and retrieve object with dorm
Insert and retrieve object with dorm

 

You can persist anything you want.
bitTime actively use dorm, so it’s continuosly updated and evolved.

Repeat, you can find the project on google code and an introduction to dorm is available on google docs.

Let me know what do you think about.

Do you need an ORM like dorm in Delphi?

Sneak preview about DORM, The Delphi ORM

My yesterday post about this busy time, have raised some interest about DORM, the Delphi ORM.

So, also if I still haven’t released any files, wish to expose some internals about DORM.

DORM is an implementation of the DataMapper design pattern written having Hibernate in mind.

It’s completely unit tested and have the following features:

  • External file mapping. (JSON format)
  • Persistence ignorance (every TObject can be persisted)
  • Support for One-One and One-Many relations (still no many-many)
  • Support for LazyLoading (you can enable§/disable lazyloading by file or by code by per-basis needs)
  • Support for IdentityMap
  • Support for custom “finder” (you can still use complex SQL if you want)
  • Complete support for CRUD
  • Transactions
  • Built in logging system to log *EVERY* sql or action performed by the framework
  • Opened to multiple data access strategies (interfaced based, not inheritance based) for use with different database (now I’ve developed the firebird one using DBX)
  • Caching for RTTI (the TSession object have a single TRttiContext holding ALL metadata)

Code is still under heavely development.

Those are 2 test-method to show the use of DORM:

TPerson = class(TObject)
...
property Phones: TdormCollection.... //implements IList
end;
 
TPhone = classs(TObject)
...
end;
 
//and now the unit tests
 
procedure TTestDORMHasMany.Setup;
begin
  Session := TSession.Create;
  Session.Configure(TStreamReader.Create('dorm.conf'));
end;
 
procedure TTestDORMHasMany.TearDown;
begin
  Session.Free;
end;
 
procedure TTestDORMHasMany.TestHasManyLazyLoad;
var
  p: TPerson;
  t: TPhone;
  guid: string;
begin
  p := TPerson.NewPersona;  //static method. Return a fully populated TPerson object
  try
    t := TPhone.Create;
    p.Phones.Add(t);
    Session.Save(p);
    guid := p.guid;  //GUIDs, or other PK types, are generated automagically by DORM. Obviously there is a specific class loaded to do this specified in the dorm.conf file)
  finally
    Session.Commit;
  end;
  Session.StartTransaction;
 
  // Test with lazy load ON
  Session.SetLazyLoadFor(TypeInfo(TPerson), 'Phones', true);
  p := Session.Load(TypeInfo(TPerson), guid) as TPerson;
  try
    CheckEquals(0, p.Phones.Count);
  finally
    Session.Commit;
  end;
 
  Session.StartTransaction;
  // Test with lazy load OFF
  Session.SetLazyLoadFor(TypeInfo(TPerson), 'Phones', false);
  p := Session.Load(TypeInfo(TPerson), guid) as TPerson; // Without commit, AV becouse IdentityMap doesn't work properly
  try
    CheckEquals(1, p.Phones.Count); // Child objects are loaded
  finally
    Session.Commit;
  end;
end;
 
procedure TTestDORMHasMany.TestLoadHasMany;
var
  list: IList;
  t, t1: TPhone;
  p: TPerson;
  guid: string;
begin
  p := TPerson.NewPersona;  //static method. Return a fully populated TPerson object
  try
    t := TPhone.Create;
    t.Numero := '555-7765123';
    t.Kind := 'Casa';
    p.Phones.Add(t);
 
    t1 := TPhone.Create;
    t1.Number := '555-7765123';
    t1.Kind := 'Casa';
    p.Phones.Add(t1);
    Session.Save(p); // save Person and Phones
    guid := p.guid;
  finally
    Session.Commit;
  end;
 
  Session.StartTransaction;
  p := Session.Load(TypeInfo(TPerson), guid) as TPerson;
  try
    CheckEquals(2, p.Phones.Count);
  finally
    Session.Commit;
  end;
end;
 

Mapping, contained in a file called “dorm.conf”, is similar to the following:

{
  "persistence": {
    "database_adapter": "dorm.adapter.Firebird.TFirebirdPersistStrategy",
    "database_connection_string":"127.0.0.1:C:\MyProjects\DORM\experiments\dorm.fdb",
    "username": "sysdba",
    "password":"masterkey"
    },
  "config": {
    "package": "dorm.bo.Person",
    "logger_class_name": "dorm.loggers.FileLog.TdormFileLog"
  },      
  "mapping":
    {      
      "TPerson":
      {
        "table": "people",
        "id": {"name":"guid", "field":"guid", "field_type":"string", "size": 100, "default_value": ""},
        "fields":[
          {"name":"firstname", "field":"first_name", "field_type":"string", "size": 100},
          {"name":"lastname", "field":"last_name", "field_type":"string", "size": 100},
          {"name":"age", "field":"age", "field_type":"integer"},
          {"name":"borndate", "field":"born_date", "field_type":"date"}
          ],
        "has_many":[{
          "name": "Phones",
          "class_name":"TPhone",
          "child_field_name":"guid_person",
          "lazy_load": false
        }],
        "has_one": {
          "name": "car",
          "class_name":"TCar",
          "child_field_name":"guid_person"
        }
      },
      "TPhone":
      {
        "table": "phones",
        "id": {"name":"guid", "field":"guid", "field_type":"string", "size": 100, "default_value": ""},
        "fields":[
          {"name":"number", "field":"number", "field_type":"string", "size": 100},
          {"name":"kind", "field":"kind", "field_type":"string", "size": 100},
          {"name":"guid_person", "field":"guid_person", "field_type":"string", "size": 100}
          ]
      }
    }
}

The PODO (Plain Old Delphi Objects) can be binded to the VCL controls with a set of MediatingView (Model-GUI-Mediator Pattern) with an Observer mechanism to mantain things in synch.

Any comments? Someone interested?