Openbiz metadata Programming for PHP

Source: Internet
Author: User
Meta-data (Metadata) programming idea originates from the high-level language of Java, which is simply to separate the business logic from the implementation code, to describe the mapping relationship between the business using only descriptive language such as XML, and to complete the programming without writing implementation code.

Meta-data programming features from Java

The original data is one of the advanced technologies in software architecture, allowing you to write less code to achieve more, and bring the reusability of business logic to the extreme. It may sound a bit abstract, so let's look at a practical example to help you understand the meta-data programming concept I'm advocating.

The openbiz architecture allows PHP meta-data programming becomes possible

For the scripting-level PHP language, it has become one of the mainstream languages of web development. But because his origins are a process-oriented programming language (I'm talking about php3, a shovel that year), not like Python or Ruby, comes up with the ultimate object language for the object.

Based on this simple background, and perhaps the reason that PHP itself is open source free, we see that a high-level extension to the PHP language is always a step behind the commercial language of. Net, Java, Objective c. When PHP5 released, he shouted, "Dude, it's now object-oriented!" "Java,. NET and cocoa asked him, "Do you have metadata programming concepts?" Do you have reusable controls at the UI level? Why hasn't Zend put on your clothes yet? Go back and play the shovel.

The development route of the Zend framework is always keen on the reuse of the underlying logic code, such as Zend_cache, Zend_mail, Zend_gdata, Zend_table does realize a lot of the reuse of the underlying logic, solving a lot of subtle and complex problems.

Here we actively recognize the contribution made by Zend, but "Big Brother you go a step forward!" ", No! Our eldest brother like to do menial work, do not like to engage in literature.

Finally special humor, after Zend unremitting efforts, eldest brother finally put a complex thing (PHP) abstract for another complex thing (Zend Framework). You count Zend API interface is not much simpler than the extension of PHP itself.

I have to mention the University of Illinois graduate master brilliant student, a former Motorola technical director, Chinese-American Rocky Swen, (I have always been a follower of his technical thinking), as early as 2003, PhP4 that era, he proposed to let PHP based on metadata, do not write code to implement the concept of programming , and has a prototype of the Openbiz framework, after 9 years of perfection to today.

This idea at that time to let me light, you see Zend, codeigniter,cakephp is an inherited framework is equivalent to a set of reusable code base, the Openbiz framework is unique in that it is an explanatory framework, equivalent to the "compiler" role. When other development environments and frameworks are working to get developers to write less code, rocky, don't let them write code that describes mapping relationships directly in a simple XML language to complete programming.

Let's compare these procedures.

An example of an order-managed data Object

The traditional PHP data Object notation

Class Orderdo

{

protected $m _id;

protected $m _client;

protected $m _product;

protected $m _price;

protected $m _timestamp;

Public Function Create () {...}

Public Function Delete () {...}

Public Function Update () {...}

Public Function Search () {...}

Public Function list () {...}

}

PHP openbiz-based metadata notation

That's great! This allows me to see two possibilities for the future, and if the main work of programming is done based on a descriptive XML language, then there is a great chance that you will have a completely WYSIWYG programming approach.

Another extreme may be the object factory concept mentioned below, automation programming.

Object Factory concept, a program that will write programs!

Every time I mentioned this concept I was thrilled, as if we were just a stone's throw away from intelligent programming. This idea, as far as I know, was the first to ask. NET introspection (this Chinese translation is very strange) this concept, that is, by the main program dynamically create another independent subroutine, dynamically compiled, and then on demand loading and destruction (like Transformers), then I was very excited to see, since then the concept is basically no longer mentioned.

Until later I read and analyzed the underlying source code of Openbiz surprisingly found the idea of an object factory based on PHP implementations. To analyze the idea, take the data object as an example:

XML-based metadata files are considered to be an assembly order for "factory", which describes how the object should be "assembled", and the mapping of the object to the Stratigraphic database, and the mapping of other objects at the same level (e.g. one-to-many ORM)

After the object factory is given a production instruction to create such an object, the desired object is created and assembled as described, and the object body and state are cached in the system in a serialized manner, optimizing performance for triggering the call again. Until the metadata configuration file changes, the object only needs to be produced dynamically once, that is, unlimited use.

Based on this programming logic, we solve a common modification and extension problem.

For example: The customer will often re-project acceptance when the bottom layer data field modification, "You see the contact management This module, can you add a birthday and favorite fields, or the end of the paragraph is afraid ...".

What to do?

Change it. Delete read/write (CRUD), list, search one can not be less all to change.

Who changed?

Sure you change it, because you write the program.

Openbiz metadata is not the same, now I only modify a data description file, then the object factory will detect the metadata configuration file changes, and then he to automatically rewrite the object and all associated with the mapping call (ORM).

When you're dealing with a system with a particularly complex business coupling, you'll find these upper-level objects "You have me, I have you" stacked calls are extremely complex (disgusting). For example, in the document modification record view also calls the contact's these several fields, and so on, you are sure to be able to make an error in every corner of this data structure?

This inhuman power can be also! But the object factory can, because it is created on-demand production.

There is a flaw in the processing of objects by the PHP language itself, where the lifetime of an object cannot be cross-viewed, and when a page request is executed, all resources associated with it are automatically destroyed and recycled. In particular, data objects are objects that typically have database links and cursor states.

The openbiz session management mechanism works with persistent object States for implementations like factories.

The object factory invokes the Metaobject::setsessiondata () interface of the abstract class before the object is delivered to automatically restore the object state.

This way, when writing a program, we can cross-view the data that the user has entered during the entire user session. For example, the information that the user has entered is displayed as the default value on the current view.

Example of invocation syntax in PHP:

$defaultValue =

Bizsystem::getobject ("Package.do.DataObjName")

->getactiverecord ()

->fieldname;

Syntax example of simpleexpression through openbiz in metadata

< p="">

Defaultvalue= "{@package. Do.dataobjname[fieldname]. Value} "

/>

Describe the business logic based on openbiz metadata, not only the data map

If you are already interested in this meta-data programming approach, please continue looking down.

The correlation mapping between the data object and the data table can be meta-data, and what can be meta-data?

MVC's three-layer structure, UI can also be metadata, view and form these UI-level elements can also be used in XML language to describe the load and trigger relationships, such as

Which form should be loaded with this view?

Which field of the data object should the text control on this edit form be bound to?

Which method of the class should be triggered when this button is pressed?

We imagine that the use of additions and deletions (CRUD) These common logical framework of the bottom can be achieved, if my demand is when the customer after the order, the automatic e-mail notify me, and send SMS notification to the distribution department. Such a relatively complex business logic, how to implement meta-data?

Data object triggers and configurable plug-in services

The triggering of this email and text message should not be implemented at the UI level because we have to consider the logic of sending the message regardless of where the order was generated. So the business logic should be coupled to the data object, that is, the logic should be triggered whenever an order is generated.

While e-mail and texting some of the common reusability logic, can be defined as pluginservice, such as in the Mail service, the recipient, the title, the content should be the parameters of the API, and the e-mail account, the SMTP server information relative to the business of the entire system generally change is not small, Should be used as the metadata interface, and how to link to the server to send mail is the object logic of the concrete reuse. The subtleties of this design will be analyzed in the next article.

So realize the logic just said, I just need to create a

Orderdo_trigger.xml meta-data file

 
   
  
     
   
      
   
         
    
                                                            
    
     
   
     
  
   
 
  

When the "INSERT" event occurs, the outgoing mail service is called, the recipient is the user's mailbox, and the message content template is Orderconfirmemail

So far, a single line of code has not been written to describe this typical business logic in the form of metadata. And with the traditional development method, to deal with such problems, 150 lines above the code of the work cost is how to avoid. And readability is not necessarily so clear.

Speaking of here, I too like openbiz this bird (I mean his logo), it feels like the wings for PHP plug.

Now we phper can say to Java that PHP can now describe business logic, you can only be the data map and configuration information of Java.

A comparison between openbiz metadata programming and Zend naming rules matching programming

This paper introduces the idea of meta-data is not the only openbiz, if it is simply the abstraction of data objects, there is a kind of advanced mode that has to be mentioned, named rule matching pattern. This is also an extension of Java. Translate to PHP implementation methods, for example:

$obj = new Stddataobj ($tableName); $obj->name= ' ABC '; $obj->attribute_1 = 123; $obj->attribute_2 = 456; $obj Save ();

I don't need to define the structure of the object before using it, but instead create it as needed, and let it automatically match the database when it's saved. But it seems that this kind of logical extensibility is limited to the data logic, and the extended logic also uses the traditional definition declaration method.

It is important that the developer's main work is not freed from the code.

This article takes the Openbiz framework as an example, and lets us see another possibility of programming.

From how to simplify the code to the extreme, try not to write code, but only describe the business logic.

This is the essence of maximizing reuse in object-oriented thinking.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.