D2010 RTTI + Attribute 简单实现ORM

 还记得David I 今年四月来盛大时,被问及“反射机制能再做得好一点吗?我们想放弃RTTI
”,David I 回答“这的确是需要考虑的地方,当然RTTI我们不会放弃的”。(这个白胡子的老哥哥还真很可爱,当年Borland几经起落,唯一能看得顺眼的就是David I)。 我还以为RTTI在D2010最多只是改良,炒冷饭而已。没想到,RTTI不仅能反射Public、protected、Private里的信息,还能动态执行该类里的方法,更惊奇的是,还支持Attribute。D2010 New RTTI 在某种程度上,比肩UniCode,在扩展框架上有无限的遐想空间。下面说一下 D2010 RTTI + Attribute 简单实现ORM。

      1、支持ORM,最基础的两个信息是表的信息和字段信息。这两个信息,如果用Attribute 来辅助,代码更简洁和可读性更好。可以把属性名当做真实字段名,也可以将特性里的属性当成真实姓名,再加上字段标题(可以当成注释)、必填字段、是否为主键、显示格式等等,如果没有Attribute ,类、属性的辅助信息必须用其他信息来描述,非常麻烦。

uses
SysUtils, RTTI, TypInfo,Types;

type
Table = class(TCustomAttribute)
private
    FName: string;
    FTitle: string;
published

public
    constructor Create(ATableName, ATitle: string);
    property Name: string read FName write FName;
    property Title: string read FTitle write FTitle;
end;

FieldInfo = class(TCustomAttribute)
private
    FFieldName: string;
    FTitle: string;
published
public
    constructor Create(AFieldName, ATitle: string);
    //字段名
    property FieldName: string read FFieldName write FFieldName;
    //标题
    property Title: string read FTitle write FTitle;
end;


2、有了这两个Attribute,我们必须创建一个解析属性和Attribute的类,并且能解析Insert、update、delete、select等SQL语句。我们姑且叫 TStorable。这个类可以根据需要扩展你所想要的东西。目前只实现了Insert方法,其他的方法,留给勤奋的人去遐想。

TStorable = class

public
    //插入SQL语句
    function Insert: string;
    //获取字段标题
    function GetFieldTitle(const AFieldName: string): string;
    //设置
    //function SetAttributeValue(const PropName, AttributeValue: string): Boolean;
end;

function TStorable.GetFieldTitle(const AFieldName: string): string;
var
Context: TRttiContext;
typ: TRttiType;
A1, A2: TCustomAttribute;
Prop: TRttiProperty;
begin
Context := TRttiContext.Create;
try
    typ := Context.GetType(ClassType);
    for Prop in typ.GetProperties do
    begin
      for A2 in Prop.GetAttributes do
      begin
        if (A2 is FieldInfo) and SameText(FieldInfo(A2).FieldName, AFieldName) then
        begin
          Result := FieldInfo(A2).Title;
          Break;
        end;
      end;
    end;
finally
    Context.Free;
end;
end;

function TStorable.Insert: string;
var
Context:TRttiContext;
Prop:TRttiProperty;
typ:TRttiType;
A1,A2:TCustomAttribute;
Sqls,Fields,Values,Value:string;

begin
Context := TRttiContext.Create;
try
    Sqls := '';
    Fields := '';
    Values := '';

    typ := Context.GetType(ClassType);
    for A1 in typ.GetAttributes do
    begin
      if A1 is Table then
      begin
        Sqls := 'Insert Into '+Table(A1).Name; //获取Insert表名
        for Prop in typ.GetProperties do
        begin
          for A2 in Prop.GetAttributes do
          begin
            if A2 is FieldInfo then    //AHa
            begin
               Fields := Fields + ','+ FieldInfo(A2).FieldName ;
               // the value of the attribute
               Value := Prop.GetValue(Self).ToString;
              //根据数据类型对属性值加以编辑
              case Prop.GetValue(Self).Kind of
                tkString, tkChar, tkWChar, tkWString, tkUString:
                  Value := QuotedStr(Value);
                tkInteger, tkInt64, tkFloat:
                  Value := Value;
              else
                 Value := QuotedStr(Value);
              end;
              Values := Values + ',' + Value ;
            end; //for A2 in Prop.GetAttributes
          end;
        end; //enf of for Prop
        Delete(Fields,1,1);
        Delete(Values,1,1);

        Sqls := Sqls + ' (' + Fields + ') VALUES (' + Values + ');';

        Result := Sqls;

      end; //if A1 is Table then
    end; //for A1 in typ.GetAttributes do

finally
    Context.Free;
end;
end;

constructor FieldInfo.Create(AFieldName, ATitle: string);
begin
FFieldName := AFieldName;
FTitle := ATitle;
end;

3、有了上面的解析类和SQL基础,我们必须创建一个实体类。属性名是否为中文,可以有不同的说法。偶目前栖身在一个医疗行业公司,医疗专业英语术语又臭又长,奥巴马未必能拼写出几个术语。如果用属性名用中文描述,将其真实的字段名放在Attribute 里,或许更能提高程序的可读性和维护性。

unit uContact;

interface
uses SysUtils,uAttribute;

type
[Table('CONTACTS','联系人信息')]
TContact = class(TStorable)
private
    FName: string;
    FAge: integer;
    F电话: string;
published
public
    [FieldInfo('NAME','名称')]
    property Name: string read FName write FName;
    [FieldInfo('AGE','年龄')]
    property Age: integer read FAge write FAge;
    [FieldInfo('电话','联系电话')]
    property 电话:string read F电话 write F电话; //尝试一下中文字段名,习惯就好
end;
implementation


end.

4、调用示例就很简单了:

procedure TForm4.btn1Click(Sender: TObject);
var
Contact:TContact;
begin
Contact := TContact.Create;
Contact.Age := 32;
Contact.Name := 'TinTin';
Contact.电话 := '135*****918';//你还会记得918的屈辱吗?

ShowMessage(Contact.Insert);

ShowMessage(Contact.GetFieldTitle('Age'));
Contact.Free;
end;

5、综述:

ORM确实在对象映射上使用起来非常方便,但并非万能,如果过分依赖于ORM,不仅不能了解数据库表与业务的关系,而且还容易写出低效的SQL查询语句。Update语句,须谨记,字段值变化才去更改,否则,会增加数据库的数据不一致风险及其增加数据库日志开销。Delete语句,配合有关键字信息的Attribute,必要时候,还要校验是否影响单条或多条记录。

这只是一个简单的例子,离真正的生产力还差一步,为了执行SQL语句,你可以在TStorable 实现数据集的读写,然后才调用执行SQL语句。

 

 

内容概要:本文介绍了ENVI Deep Learning V1.0的操作教程,重点讲解了如何利用ENVI软件进行深度学习模型的训练与应用,以实现遥感图像中特定目标(如集装箱)的自动提取。教程涵盖了从数据准备、标签图像创建、模型初始化与训练,到执行分类及结果优化的完整流程,并介绍了精度评价与通过ENVI Modeler实现一键化建模的方法。系统基于TensorFlow框架,采用ENVINet5(U-Net变体)架构,支持通过点、线、面ROI或分类图生成标签数据,适用于多/高光谱影像的单一类别特征提取。; 适合人群:具备遥感图像处理基础,熟悉ENVI软件操作,从事地理信息、测绘、环境监测等相关领域的技术人员或研究人员,尤其是希望将深度学习技术应用于遥感目标识别的初学者与实践者。; 使用场景及目标:①在遥感影像中自动识别和提取特定地物目标(如车辆、建筑、道路、集装箱等);②掌握ENVI环境下深度学习模型的训练流程与关键参数设置(如Patch Size、Epochs、Class Weight等);③通过模型调优与结果反馈提升分类精度,实现高效自动化信息提取。; 阅读建议:建议结合实际遥感项目边学边练,重点关注标签数据制作、模型参数配置与结果后处理环节,充分利用ENVI Modeler进行自动化建模与参数优化,同时注意软硬件环境(特别是NVIDIA GPU)的配置要求以保障训练效率。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值