delphi mvc framework - wordpress.com...2016/02/01 · aboutme • name: mathias pannier •...
TRANSCRIPT
Delphi MVC FrameworkMathias Pannier
About me
• Name: Mathias Pannier
• Position: Software developer / Team leader
• More than 15 years of experience with software development in Delphi
• Blog: mathiaspannier.wordpress.com
mathiaspannier.wordpress.com
DMVCFramework
• Delphi MVC Framework - is a powerful framework for web solution in Delphi (Quelle: https://code.google.com/p/delphimvcframework/)
• Source files for the development of web services (REST Server/Clients)
• Website: https://github.com/danieleteti/delphimvcframework
• Support site: https://www.facebook.com/groups/delphimvcframework
mathiaspannier.wordpress.com
DMVCFramework
• Lead developer and initiator is Daniele Teti (http://www.danieleteti.it- Author of Delphi Cookbook)
• Delphi Cookbook contains some sections about DMVCFramework
• At the moment 4 developer
• First public release 10/29/2013
• Last update January 2016 (Time of presentation is January 2016)
• Works with XE3 and newer
mathiaspannier.wordpress.com
Features
• Simple to use (really)
• RESTful (RMM Level 3) compliant
• Fancy URL with parameter mappings**
• Specialized renders to generate text, html, json**
• Powerful mapper to map json to objects and datasets to objects**
• Can be packaged as stand alone server, apache module (XE6 or better) and ISAPI dll
mathiaspannier.wordpress.com
Features
• Integrated RESTClient
• Messaging extension using STOMP
• Load balancing and cache using REDIS
• It use Embarcadero WebBroker but is not tied to it
• Designed with services and web client app in mind
• Server side generated pages using Mustache (in the past eLua)
• Etc.
• Source: https://github.com/danieleteti/delphimvcframework und https://github.com/danieleteti/delphimvcframework/blob/master/docs/ITDevCON%202013%20-%20Introduction%20to%20DelphiMVCFramework.pdf
mathiaspannier.wordpress.com
mathiaspannier.wordpress.com
REST
• ... representational state transfer (REST) is the software architectural style ... REST's coordinated set of constraints, applied to the design of components in a distributed hypermedia system, can lead to a higher-performing and more maintainable software architecture.
• Constraints: Client-server, Stateless, (HTTP) Cacheable, Uniform interface (URI, HTTP verbs, JSON, HATEOAS – navigation by URIs), Layered system
• Source: https://en.wikipedia.org/wiki/Representational_state_transfer
mathiaspannier.wordpress.com
Architecture
• One application (EXE or ISAPI DLL)
• Several controller (TMVCController)
• Several actions per controller (Methods)
• Controller, action and parameter are parts auf the url
• www.myserver.com/blog/posts/123
mathiaspannier.wordpress.com
1. Example - Delphi WebBroker Application (ISAPI/EXE)
typeTWebModule1 = class(TWebModule)
procedure WebModuleCreate(Sender: TObject);private
DMVC: TMVCEngine;end;
procedureTWebModule1.WebModuleCreate(Sender: TObjec t);begin
DMVC := TMVCEngine.Create(self);DMVC.AddController(TBlogController);
end;
mathiaspannier.wordpress.com
1. Example – The controllertype
[MVCPath('/blog')]
TBlogController = class(TMVCController)
public
[MVCPath('/posts/($id)')]
procedure GetPostByID(CTX: TWebContext);
end;
implementation
procedure TBlogController.GetPostByID(CTX: TWebContext);
var
aBlogPost: TBlogPost;
begin
aBlogPost := LoadFromDataBaseByID(CTX.Request.Parameters['id'].ToInteger);
Render(aBlogPost);
end;
mathiaspannier.wordpress.com
Parameter
• CTX.Request.Params['id']
• URL Mapped parameters
• Query String parameters
• Form parameters (HTML Form Submit)
• Cookie fields
• Bsp.: • www.myserver.com/blog/posts/123
• www.myserver.com/blog/posts?id=123
mathiaspannier.wordpress.com
Renders
• Methods of the controller to produce the response stream
• Renders can set the HTTP Status Code and the Content Type
• Currently you can render
• Text, DataSets, JSONValues, Delphi Objects, List of Delphi Objects, Exceptions, HTML pages using Mustache, Raw Streams
mathiaspannier.wordpress.com
Renders exampleprocedure TBlogController.GetPostByID(CTX: TWebConte xt);
var
aBlogPost: TBlogPost;
begin
aBlogPost := TBlogPost.Create;
aBlogPost.ID := CTX.Request.Parameters['id'].ToInte ger;
aBlogPost.Title := 'Hello World.';
aBlogPost.Body := 'This is a test message.';
aBlogPost.CreateDate := Now();
aBlogPost.Author := 'Mathias Pannier';
Render(aBlogPost);
end;
procedure TBlogController.GetPostByID(CTX: TWebConte xt);
begin
DataSet1.SelectSQL.Add('select * from BLOG_POSTS wher e ID = :ID');
DataSet1.Params[0].AsInteger := CTX.Request.Paramete rs['id'].ToInteger;
DataSet1.Open;
Render(DataSet1);
end;
mathiaspannier.wordpress.com
Mapper
• „Converter" for Delphi Objects, JSon and DataSets
• Example.:
• ObjectToJSONObject
• JSONObjectToObject
• DataSetToJSONArray
• JSONArrayToDataSet
• ...
• Can be used independent of the DMVCFramework
mathiaspannier.wordpress.com
Mapper example
procedure TForm1.Button1Click(Sender: TObject);
var
aBlogPost: TBlogPost;
begin
aBlogPost := TBlogPost.Create;
aBlogPost.ID := 1;
aBlogPost.Title := 'Hello world.';
aBlogPost.Body := 'This is a test message.';
aBlogPost.CreateDate := Now();
aBlogPost.Author := 'Mathias Pannier';
ShowMessage(Mapper.ObjectToJSONObjectString(aBlogPost));
aBlogPost.Free;
end;
mathiaspannier.wordpress.com
Demo
mathiaspannier.wordpress.com
Alternatives
• Hand made with Indy components
• DataSnap (Embarcadero; shipped with Delphi)
• mORMot Framework
• TMS XData
• kbmMW
• RemObjects
• Etc.
mathiaspannier.wordpress.com
Why DMVCFramework
• Lightweight, small, fast
• Easy to use
• Open source
• Closer to REST/Web development/HTTP
• Mapper/Renders independently usable
• Performance and stability of DataSnap ... (https://robertocschneiders.wordpress.com/2012/11/22/datasnap-analysis-based-on-speed-stability-tests/)
mathiaspannier.wordpress.com
Questions?
mathiaspannier.wordpress.com