捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  粤ICP备10103342号-1 DELPHI盒子 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 盒子检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
盒子资源分类
全部展开 - 全部合拢
利用RTTI和数据集简单实现的ORM思想
关键字:RTTI ORM 运行期类型信息 数据集 对象
来 自:原创
平 台:Win2k/XP/NT,Win2003 下载所需:0 火柴
深浅度:中级 完成时间:2006/7/11
发布者:tintin1943 发布时间:2006/7/11
编辑器:DELPHI7 语  种:简体中文
分 类:对象 下载浏览:3233/22908
加入到我的收藏
下载错误报错
登陆以后才能下载
 用户名:
 密 码:
自动登陆(30天有效)
图片如果打不开,说明流量不够了,请稍候下载……
一、 简要说明

为什么要创建实体与数据集对应关系呢?猛禽兄有一篇技术文章告诉你。
http://borland.mblogger.cn/raptor/posts/14782.aspx

创建一个实体类,有多种方式,但我固执的认为,只声名不实现将是最简洁最好的方式。
这种方式将代码量减到最少,以后做一个代码生成工具,也方便代码复制粘贴。

比如,国家实体类声名如下:
TCountry = class(TgsDataSetProxy)
  published
    property Name: TStringF index 0 read GetStringF;
    property Capital: TStringF index 1 read GetStringF;
    property Continent: TStringF index 2 read GetStringF;
    property Area: TNumberF index 3 read GetNumberF;
    property Population: TNumberF index 4 read GetNumberF;
  end;
但不幸的是,必须用属性索引才能获知属性名称信息。说起缘由,还是Delphi的RTTI实现功能相对很弱,比如只能获知published声明的属性,不能在运行时获知记录类型元素信息,不能获知类的Pbulic声明信息等。

当实体字段顺序与数据集字段顺序不一致,在初始化实体实例时,TgsDataSetProxy 调用 CheckEntityDataSet:函数强制检验序号与名称是否一致。目前只是校验实体属性名与数据集字段名是否一致,未强制检验类型一致。

为了统一实体映射表字段规范,我创建了TstringF、TnumberF等六种字段。如果用简单类型实现实体字段,也无可厚非,但为了以后扩展方便,还是自定义实体字段类型对象较好一些。

比如TNumberF:
TNumberF = class(TgsPropField)
  private
    FValue: Double;
    procedure SetValue(const Value: Double);
    function GetStrValue: string;
  published
    property Name;
    property Caption;
    property AsNum: Double read FValue write SetValue;
    property ToStr: string read GetStrValue;
  end;
声明了一个属性AsNum,虽然敲写代码多了一些,但可以一目了然知道实体字段类型,另外也可以提供ToStr转换成字符串。

GetNumberF(Index:integer)在其父类TgsDataSetProxy的 protected声名,目的是实现实体类只声名不实现方式。

在TgsDataSetProxy实现了CURD功能,即简单的 Insert、Update和delete功能。

其实我最向往的定义实体元数据方式是,
TCountry = class(TgsDataSetProxy)
  published
    property Name: TStringF Read GetStringF;
    property Capital: TStringF Read GetStringF;
    property Continent: TStringF Read GetStringF;
    property Area: TNumberF Read GetNumberF;
    property Population: TNumberF Read GetNumberF;
property ContinentID: TContinent. read GetStringF;
property ContinentCaption: TlookupField(实体参数类名) read GetLookupField;//主要目的是实现Lookup关联数据集和字段。

 end;
以上说的是一些思想而已,语法也肯定有错误,delphi按这样方式也未必能实现。
由于delphi没有.net的属性特性,直接实现很困难。
具体思想,可以参考我同事的SuperORM思想,虽然是.Net实现的,但看完他的文章思想,相信对你还是受益匪浅。
http://www.wbstudy.com/ViewTopic.aspx?ForumID=10&TopicID=2483

二、 参考文献
          
 参考代码:1.猛禽兄 <<用DELPHI的RTTI实现数据集的简单对象化>>
          详见猛禽兄的Blog
          http://borland.mblogger.cn/raptor/posts/14782.aspx

          2.Chris Lichti的delphi之 RTTI管理类,单元clRTTI就是
          他所写,我保留了他的原著,也不再注释成中文了

  参悟代码:3.我同事(绰号:大师)一个 .NET SuperORM 框架思想
          详见大师的个人学习网站
          http://www.wbstudy.com/

在此对他们表示由衷的感谢!!! 

三、 扩展说明:

在理解猛禽兄的源码上,重写了实体数据集体对象化。扩展了实体的遍历、新增、修改和删除,即常用的CURD功能。如同猛禽兄所说的,要获得属性名称,只好利用属性索引,即index 0。
    我现在已开始 .NET项目开发,如果有时间,我会扩展一下功能:

    客户端:
    1、客户端无SQL,只是传递参数值。参数最好以强类型赋值。
    2、客户端的据集对象化,即实现简单的ORM功能。
    3、扩展现有的客户端的据集对象化,实现查询数据集体对象化。

    我还要仔细参悟大师的 SuperORM,delphi的RTTI没有.NET的反射机制强大,很是苦恼。
    4、所有的TdataSet的中文标题通过数据库表自动填充实现,包括数据集字段的一些属性,比如 format格式,对其方式、默认值、Required等。
5、实体类的Lookup自动化,其实现原理获得所有表与表之间的关系,自动实现数据集的Lookup,不需人工干预,使 Coding工作更高效,脱离码奴思想。
    6、一些常用的数据库界面控件封装。现在采用 Ehlib套件和 DevExpress的垂直表格控件。快速获得最强大最方便的前台功能界面。
    7、实体类也可以实现可视化界面设计,比如在TDataModule放置
          TDataSource, TAdoConnection ,TAdoQuery之类,如果需要可视化处理
          DataField和表格列宽,录入 SQL到 TAdoQuery。
          与此类推应用到BDE,DbExpress。切换数据集控件时候不要忘记无用的Uses单元名称。
    8、一个通用强大的查询对话框。可实现复杂的多表连接查询。
          
    服务端:
    1、服务端的AOP,即面向功能或面向服务。所有的方法或函数放在服务端上,且方法或函数为无参数形式,其内部参数通过对应的客户端输入参数获得。
服务端以动态组合方法或函数的方式执行任务。支持事务机制。
说起来,也没有MeAOP强大和专业了。
http://www.delphibox.com/article.asp?articleid=3309
http://dev.cq118.com/web/ow.asp?p=FrontPage
    2、客户端打开数据集或执行任务完毕,关闭数据库连接,节省资源。
    3、具体数据集无关性。即封装TdataSet或 TClientDataSet,即切换ADO或BDE或DbExpress,将修改工作量减到最少。
    4、实现了界面与业务功能代码分离以后,再将改为三层结构,就容易多了。
你说是不是呢?

以上一些方法已经实现或陆续实现,但未认真整理过。
如果你修改了源码或有好的想法,如果方便请你也告诉我。毕竟,有交流才有进步。

  Copyright (c) 2006 by 碧水航工作室.
  Author : TinTin
  Date   : 2006.07.10 (世界杯决赛前夕)
Google
 
本站原创作品,未经作者许可,严禁任何方式转载;转载作品,如果侵犯了您的权益,请联系我们
龙脉加密锁 15元起 Grid++Report 报表 申请支付@网
 相关文章
利用RTTI和数据集简单实现的ORM思想
tintin1943 2006/7/11 下+3233/浏+22909 评+23
一个学习RTTI的例子的参考
wr960204 2005/7/1 下+5494/浏+19559 评+10
Delphi的RTTI机制文档
Giver 2004/2/16 下+1402/浏+11956 评+0
相关评论
共有评论23条 当前显示最后6条评论
gnosis 2006/7/17 22:01:48
想到五陵少年都要折腾成五陵老年了,还要学三年,呵呵,受了不啦……
你说这文人相轻,还有个理由,都风流快活嘛,怎么这做技术这么辛苦,也来这套。
想一想是没意思。
向jhwh道歉,向yayongm致敬。
向tintin问好。
柏拉图在歌唱:哦,理念,永恒的理念。
想起来这么完美,做起来这么困难。
ego 2006/7/27 21:36:32
gnosis:
  “史无前例的全息感应功能”是一种什么技术?听起来很玄的样子,能介绍一下吗?
  你的网站是乱码。

jhwh:
  “一说ORM,大多数人就认为要Refector,这是一个误区”
  同意,好多ORM都是把写代码变成了写xml..........
Alfralleme 2015/10/23 16:39:26
Antibiotics.outer fold of pleura lying closer to the ribsPeripheral blood smear  a. [url=http://ednorxmedshop.com]viagra wonderful special for men[/url] I..The bodys immune system recognizes the foreign substances called antigens and mobilizes defenses against them.This is the value we used previously for humanet al.Of those who survive the initial event approximately  of patients will die of a recur rent PE if left untreated. [url=http://shopbestedmedrx.com]Cialis[/url] The magnitude of the resultant force  F r  and the angle  at which the legs must apply the force to the body are obtained from the following considerations.Removal of skin tissue using a cut parallel to the surface of the surrounding skin is called aan [url=http://bestmedrxedfor.com]coupons for cialis 20 mg[/url] CIThe corresponding Larmor frequencies are about  to  MHz.The bodys immune system recognizes the foreign substances called antigens and mobilizes defenses against them.Liver biopsy determines hepatic iron concentration required for diagnosis. [url=http://buyinderalus.com]indian pharm inderal[/url] Sustained VT persists in the absence of intervention   Lasts longer than  seconds and is almost always symptomatic   Often associated with marked hemodynamic compromise i.thalamus Main relay center of the brain.Only a few papyri deal with medical matters.This phenomenon is called the     Chapter   Waves and Sound  Doppler effect.Note also the origin of  Olecranon  the triceps at the scapula and the insertion at the olecranon of the ulna. [url=http://fastbestmedrx.com]levitra sold over the counter[/url] Quick Hit  Pulseless electrical activity PEA   Occurs when electrical activity is on the monitor but there are no pulses even with Doppler and carries a grim prognosis  Treat possible causes hypoxia hypovolemia hypotension hyperkale mia tamponade tension  pneumothorax massive PE and so on and medicate according to ACLS guide lines.Take any other medicine such as an antibiotic as instructed.enlargement of the liver hepato
Wilflabbed 2016/4/30 2:39:21
Cx [url=http://audrugs.com]viagra online prescription[/url] Possible Complications Severe hypoglycemia is a medical emergency that may cause seizures and permanent brain damage.These attitudes hardened in Europe during the Middle Ages as the stigmatization of the mentally ill increased.After she gave birth to her fourth child Ms.Oxygen if patient is hypoxic  g.antidepressantUnlike traditional Xray images each point of the scan is encoded digitally which allows it to be magnified and manipulated as never before and communicated electronically far and wide. [url=http://buyisotretinoinusfast.com]accutane[/url] Quick Hit  Monitoring a patient with CHF   Weightunexplained weight gain can be an early sign of worsening CHF   Clinical manifestations  exercise tolerance is key peripheral edema   Laboratory values electro  lytes K BUN creatinine levels serum digoxin if applicable  Quick Hit  Standard treatment of CHF includes a loop diuretic ACE inhibitor and betablocker.external beam irradiation Applying radiation to a tumor from a source outside the body.Red blood cells then transport the oxygen to cells throughout the body.B chest radiograph upright showing bilateral subdiaphragmatic intraperitoneal air.th ed. [url=http://armadol.com]sertraline online best price[/url] b.A hyperosmolar highglucose solution is used and water is removed from  the blood via osmosis.Other nations did the same.Experts say this is partly due to the fact that younger people are better able to tolerate strong chemotherapy medicines. [url=http://nuvigi.com]cialis[/url] Immunohistochem.When to Contact a Medical Professional Call your health care provider if  You develop symptoms of alcoholic liver disease. [url=http://edfast-medrx.com]cialis[/url] The breasts located on the upper anterior region of the chest are composed mostly of mammary glands.
CharlRaiple 2016/9/30 6:54:10
From Eq. [url=http://bedrugs.net]viagra[/url] Aberrant epigenetic landscape in  intellectual disability.Protein S deficiencyProtein S is a cofactor of protein C so a deficiency leads to  decreased protein C activity.Recommendations for preparing children and adolescents for invasive cardiac procedures a statement from the American Heart Association Pediatric Nursing Subcommittee of the Council on Cardiovascular Nursing in collaboration with the Council on Cardiovascular Diseases of the Young. [url=http://303meds.com]cialis[/url] If a relapse occurs it is very important to try to stop drinking once again and to get whatever additional support is needed to abstain from drinking.The external canal was clear tympanic membrane was divided. [url=http://vbdrug.com]kamagra 100[/url] The effect of radiation on the healthy tissue can be reduced by frequently altering the direction of the beam passing through the body.Rev.References Bolognia J.The  issue of the Journal of Experimental Biology volume  is devoted to this field of inquiry.Oxygen  L by NC titrate up as needed  b. [url=http://bhdrugs.com]buy viagra[/url] It is twice as common in men as in women.the leeches lived  MECHANICAL LEECH  This syringelike suction device enabled surgeons to control the amount of blood removed.dysthymiaQuiCk Hit  aspiration can lead to lung abscess if untreated.gynecologist G. [url=http://banzell.net]viagra[/url] PubMed   Eardley I Wright P MacDonagh R Hole J Edwards A.
ChestSmava 2016/10/28 1:54:36
Viagra In America  [url=http://shopednorxmed.com]viagra online prescription[/url] Cialis No Prescription Recovering From Amoxicillin Elocon Tablets Pharmacy Commande Cytotec  [url=http://buyisotretinoinusfast.com]buy oratane[/url] Want To Order Prednisone Propecia Precio Cialis Levitra Propecia Efectos Adversos Cialis 20 Bestellen Levitra Vardenafil  [url=http://buytadalafilus1.com]buy cialis[/url] Compatibility Of Cephalexin And Doxycycline Zithromax Online Purchase Vente Ciprofloxacin Generique Buy Azithromycin No Prescription Olanzapine  [url=http://drugs2k.net]cialis[/url] Acheter Mifegyne Et Cytotec Fiche Baclofene Cialis Rembourse Ed Sample Pack Cheap  [url=http://clanar.com]viagra prescription[/url] Cialis im internet kaufen Cialis Riesgos Levitra Pastiglie Otc Celebrex  [url=http://ziagen.net]acheter propecia paris[/url] Viagra Qualite Nexium Over Counter Versus Prescription Cialis Vente Suisse Cialis 2.5mg Viagra Klappt Nicht  [url=http://drugsr.com]viagra prescription[/url] Cialis Viagra Vendita Amoxicillin Mono What Is Amoxil Capsules Used For  [url=http://accleps.com]come aquistare kamagra post.pay[/url] Precio De Viagra En Farmacia Zithromax Official Website 40 Mg Lasix Levitra 10  [url=http://ednorxmed.com]viagra[/url] Cheapeast Generic Dutasteride Visa Accepted C.O.D. Tyneside Canadian Pharmacies Viagra Amoxicillin Infection Levitra 20 Mg Prezzo Farmacia Il Farmacista Online Cialis Canada  [url=http://banzell.net]viagra[/url] Ed Drugs Rezept Viagra Generika Cialis Pagamento Contrassegno Viagra Shop Forum  [url=http://bdnpn.com]online pharmacy[/url] 3 Clomid Depuis Mois Deja Antibiotics Similar To Amoxicillin Cipro And Amoxicillin In Lyme Disease Cialis De 20 Mg  [url=http://buytadalafil20mgprice.com]online pharmacy[/url] Kamagra Uk Next Day Delivery Paypal 306 Zentel 400mg Tadalis Sx Soft Online Bestellen Kopa Lasix Purchase Viagra  [url=http://buytamoxifencitrate1.com]is nolvafien the same as nolvadex[/url] Viagra Yeux Rouges Drugstore Usa Diuretiques Thiazidiques Acheter Pharm Canadienne Buy Clomid Online Without A Script  [url=http://etaze.net]cialis generica[/url] Amoxicillin 100mg Ct Viagra Preisgunstig Canada Drugstore Online Buy Viagra Caverta  [url=http://mpphr.com]priligy canada where to buy[/url] Order Accutane Online Amoxil Categorie De Grossesse Viagras Cialis Levitra En Streaming Withdrawal Of Cephalexin Buy Online Viagra Pfizer From Pakistan  [url=http://shopcurerx.com]kamagra 100 online[/url] Cialis Generico Desventajas Propecia Eficacia Efectos Secundarios Viagra Naturale  [url=http://cheap-x.com]generic viagra[/url] Vardenafil Side Effects Propecia Teva Occasion Levitra Cyalis Livraison Rapide Viagra Y Cialis Mejor Generic Provera Meprate Cheapeast Best Website On Line  [url=http://cthosts.net]online pharmacy[/url] Low Prices Flagyl In Stamford Priligy Uso Viagra Giovani Effetti
我要发表评论 查看全部评论
 
  DELPHI盒子版权所有 技术支持:深圳市麟瑞科技有限公司 1999-2024 V4.01 粤ICP备10103342号-1 更新RSS列表