Nov
01

事务脚本与领域模型
2011-11-01 15:46:59 作者:Chatterley

在newcp的开发过程中,经过权衡后最终使用基于YII框架开发。在使用了YII框架的MVC结构,程序结构自然而然使用了Controller->Model->View。在Model细分层次结构为Service->Logic->ARModel。在这样的结构下,无论是使用JAVA还是.NET的项目,往往容易被套在事务脚本形式,newcp也不例外。每个功能处理都是使用同一个流程。
最后在编写代码的时候,则会出现如下的代码结构(为了便于阅读代码都已经过调整):

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  1. //检查余额
  2. $accountService = new FundsAccountService();
  3. $account = $accountService->checkBalance($orderDetails->customer_id, $orderDetails->amount);
  4. if($account == false) {
  5. //……
  6. }
  7.  
  8. //产品接口操作
  9. $busiductService = new BusiductService();
  10. $busiduct = $busiductService->getBusiduct($order->busiduct_id);
  11. $productOrder = OrderFactory::createOrderInstance($busiduct->product_code);
  12.  
  13. $productOrder->service($orderDetails->orderduct_name, $orderDetails->action);
  14.  
  15. //更新订单状态--已操作
  16. $this->updateStatus($id, BusinessOrder::STATUS_SERVICED);
  17.  
  18. //扣款操作
  19. $financeTypeService = new FinanceTypeService();
  20. $form = new FundsDetailForm();
  21. $form->customer_id = $orderDetails->customer_id;
  22. $form->amount = $orderDetails->amount;
  23. $form->finance_type_id = $financeTypeService->getTypeByBusiness($orderDetails->busiduct_id, $orderDetails->action);
  24. $form->cheque_number = $orderDetails->id;
  25. $form->effective_date = new CDbExpression('now()');
  26. $form->note = $orderDetails->orderduct_name;
  27.  
  28. if(false == $accountService->deduct($form)) {
  29. //……
  30. }
  31.  
  32. //更新订单状态-已支付
  33. $this->updateStatus($id, BusinessOrder::STATUS_PAID);
  34.  
  35. //更新产品数据信息
  36. $context = $this->getContext($orderDetails);
  37. $productOrder->complete($orderDetails->orderduct_name, $context);
  38.  
  39. //更新订单状态--已入库
  40. $this->updateStatus($id, BusinessOrder::STATUS_STORED);
  41.  
  42. //更新订单状态--已完成
  43. $this->updateStatus($id, BusinessOrder::STATUS_FINISHED);

事务脚本优势在于简单,但对于复杂业务则冗余代码,并且复用性可扩展性很低。对于复杂的业务必须使用领域模型来替代。实际表现的代码应该如下:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  1. //检查余额
  2. $account = new Account($orderDetails->customer_id);
  3. if(false == $account->checkBalence($orderDetails->amount)) {
  4. //……
  5. }
  6. //产品接口操作
  7. $busiduct = new Busiduct($order->busiduct_id);
  8. $busiduct->service($order);
  9.  
  10. //扣款操作
  11. if(false == $account->deduct($orderDetails)) {
  12. //……
  13. }
  14.  
  15. //更新产品数据信息
  16. $busiduct->complete($order);

这中表现形式很直观,便于阅读,并且少了很多updateStatus操作。其实这些updateStatus操作是封装于service、complete等接口中。

这里只是一个简单的例子,实际设计过程中,领域网往往相对比较复杂。领域模型需要一些面向对象技术,以及对领域逻辑相对比较清晰,并且要利用一些架构模式或设计模式。
领域模式如果设计不好则会导致对象过于臃肿,并且降低程序的响应速度。

注:
事务脚本:将所有逻辑组织成单个过程,在过程中直接调用数据库,或者只通过简单的数据库封装器。
领域模型:创建了一张由互联对象组成的网,其中的每一个对象都代表某个有意义的个体(实际对象)。

推荐(0)
收藏
分享至: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 添加到饭否 QQ书签 POCO网摘 Digbuzz我挖网 
Sep
10

php面向过程还是面向对象?
2010-09-10 10:13:56 作者:Chatterley

  过程狂热

  过程狂热曾在上课时被计算机教师批评,因为这种方法没有使用更加抽象的实现方式。而支持PHP面向过程者的观点“它可以工作!”并不能提高其编程水平和档次。毕业后他们可能找到一个工作,写驱动程序,文件系统或其它的偏向底层的编程,他们的注意力集中于速度和代码的精炼。

  “过程狂热”极端的例子是抵制对象,抵制抽象化。他们总在想着如何让程序运行起来更快,而不在乎别人是否能读懂他们的代码。他们常常把编程当成竞赛而不是团队活动。除了PHP外,他们最喜爱的编程语言是C和汇编。在PHP世界中他们可能会开发PECL模块,贡献出高效率的代码。

  对象狂热

  对象狂热者热衷于在任何时候使用PHP面向对象的风格来书写代码。他们没有真正考虑过用这种方式是否会影响程序的执行效率。有时候让人觉得他们更享受抽象的设计概念而不是现实的代码。他们通常很可能是项目管理者或文档书写者。

  对象狂热者指出,如果没有抽象的设计方法我们仍然在使用0和1进行编程。他们喜欢用伪码来描述问题。极端的例子是对象狂热者即使知道有时候会牺牲效率仍然使用对象。除了PHP,他们最喜欢的语言是Java和Smalltalk。在PHP世界中,他们可能会开发PEAR模块,贡献文档化非常好,易于维护的代码。

推荐(0)
收藏
分享至: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 添加到饭否 QQ书签 POCO网摘 Digbuzz我挖网 
Sep
09

What Is INFORMIX-ESQL/C?
2010-09-09 14:13:20 作者:Chatterley

ESQL/C is an SQL application programming interface (API) that enables you to embed Structured Query Language (SQL) statements directly into a C program. The ESQL/C preprocessor, which the esql command calls, converts each SQL statement and all Informix-specific code to C.

To access a database server through an ESQL/C application

1. Write embedded SQL statements and calls to ESQL/C library functions into your C code to create an ESQL/C program.
2. Convert the ESQL/C program to a C executable file with the esql command.

Compiling an ESQL/C Program: You type the esql command to compile your ESQL/C program. The esql command passes your ESQL/C source file to the ESQL/C preprocessor and to the C compiler. It passes along options that are specific to both the ESQL/C preprocessor and the C compiler to preprocess, compile, and link your ESQL/Cprogram.

The esql command follows these steps to carry out the conversion:

* In Stage 1, the ESQL/C preprocessor performs the following steps:
o Incorporates header files into the source file when it processes all include directives ($include and EXEC SQL include statements)
o Creates or removes compile-time definitions when it processes all define ($define and EXEC SQL define) and undef ($undef and EXEC SQL undef) directives
o In Stage 2, the ESQL/C preprocessor processes any conditional compilation directives (ifdef, ifndef, else, elif, endif) and translates embedded SQL statements to ESQL/C function calls and special data structures.

Stages 1 and 2 mirror the preprocessor and compiler stages of the C compiler. Successful completion of the preprocessing step yields a C source file (.c extension).

Default Compilation Order
ESQL/C source program -> ESQL/C preprocessor -> C source program with SQL statements and ESQL/C calls -> C language preprocessor and compiler -> Executable program

推荐(0)
收藏
分享至: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 添加到饭否 QQ书签 POCO网摘 Digbuzz我挖网 
Jul
22

查看ubuntu是32bit还是64bit
2010-07-22 09:49:54 作者:Chatterley

执行命令:
$ file /bin/ls

推荐(0)
收藏
分享至: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 添加到饭否 QQ书签 POCO网摘 Digbuzz我挖网 
Jul
06

web服务器
2010-07-06 10:50:49 作者:Chatterley

Lighttpd和Nginx都属于轻量级的Web服务器,它们的出现自然会抢占一些属于Apache的市场份额。不过 Lighttpd、Nginx和Apache并不是相互排斥,相互对立的。Lighttpd和Nginx的长处在于处理静态页面,它们的处理速度可以达到 Apache的2~3倍,极端情况下能达到10倍。因此通常情况下,Apache用来作为后台服务器,处理PHP、CGI等,生成动态内容;Nginx用来作为前端服务器,从而充分利用它占用资源少的优势来处理静态页面的大量请求,而Lighttpd则通常用于图片服务器。

推荐(0)
收藏
分享至: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 添加到饭否 QQ书签 POCO网摘 Digbuzz我挖网 
得到OpenID
使用OpenID提供商
35OpenID 35OpenID MyOpenID MyOpenID Flickr Flickr
Google Google Yahoo Yahoo! AOL AOL
Blogger Blogger LiveJournal LiveJournal Verisign Verisign
ClaimID ClaimID Technorati Technorati Vidoop Vidoop
OpenID OpenID 帮助
您还没有登录,请登录后继续操作。
提示:您必需打开Cookie才能使用本系统
请输入您的 OpenID OpenID 登录:
例如:http://yourname.openid.35.com
close