delphi源代码三层框架【DLL插件】

本文介绍了如何在软件开发中通过DLL插件的方式,将核心功能代码封装在服务端,客户端通过接口调用,实现单据流水号生成等业务逻辑,同时展示了TETUTestdll类的实现和客户端调用示例。
摘要由CSDN通过智能技术生成

1,考虑到软件开发过程中有些功能函数或业务逻辑不想放在客户端,如单据流水号的生成,核心算法;可将此功能代码封装在DLL里面,编译后以插件的形式放在服务端,然后客户端通过固定的接口函数调用,结果会以OleVariant类型返回;简单灵活易用。

服务器DLL模板格式:

unit ETU.Demo;

interface

uses
  SysUtils, Windows, Messages, dbclient, Classes, Dialogs, IPlugin, midasLib;

{
  //IBPlugin为继承的接口类

}

type
  TETUTestdll = class(TInterfacedObject, IBPlugin)
  private
    tcpFun: HWND;
    // 业务处理
    function getdbData(out resultData: OleVariant; out sErrorMsg: string): boolean;
  protected
  public
    ci: integer;
    CC: string;
    // 对象构造方法
    constructor create(const paramStr: string);
    destructor Destory;
    {
      //dll 入口函数: 不可改变名称与参数
      paramStr: 参入的json格式的参数字符串 或普通字符串,只要客户端与服务端一直即可
      resultData:返回 OleVariant 类型的接口(可以是普通字符串,数值,json 格式的字符串,clientDataset.data 数据集
      等)
      sErrorMsg: 返回错误信息
      result:成功返回true
    }

    function MainMethod(const paramStr: string; out resultData: OleVariant; out sErrorMsg: string): boolean;

  end;

implementation

{
  //若需调用中间层的方法 tcpFun.dll :封装对应的中间层方法

  (顺序: 连接服务器->操作对应的方法->断开连接)
}
// 连接中间服务器
function setSQLConnection(out sErrorMsg: string): boolean; stdcall; external 'tcpFun.dll';
// 执行sql语句
function ExecSqlText(cds: TClientDataSet; sSqlText: string; DBType: string; haveResult: boolean; out sErrorMsg: string): boolean; stdcall; external 'tcpFun.dll';

// 断开服务器连接
function DisSQLConnection(out sErrorMsg: string): boolean; stdcall; external 'tcpFun.dll';

constructor TETUTestdll.create(const paramStr: string);
var
  sErrorMsg: string;
begin
  inherited create();

end;

destructor TETUTestdll.Destory;
begin

  inherited;

end;

function TETUTestdll.getdbData(out resultData: OleVariant; out sErrorMsg: string): boolean;
var
  i: double;
  cds: TClientDataSet;
  sSqlText: string;
begin
  result := false;

  if not setSQLConnection(sErrorMsg) then
    raise Exception.create(sErrorMsg);
  try
    cds := TClientDataSet.create(nil);
    try
      sSqlText := 'select top 11 * from stundent';
      result := ExecSqlText(cds, sSqlText, '1', true, sErrorMsg);
      if not result then
        raise Exception.create(sErrorMsg);
      resultData := cds.Data;

    finally
      FreeAndNil(cds);
    end;
  finally
    DisSQLConnection(sErrorMsg);
  end;

end;

function TETUTestdll.MainMethod(const paramStr: string; out resultData: OleVariant; out sErrorMsg: string): boolean;
begin
  try
    result := getdbData(resultData, sErrorMsg);
  except
    on ee: Exception do
    begin
      sErrorMsg := ee.Message;
    end;
  end;

end;

end.

IBPlugin 接口单元(引用即可无需修改):

unit IPlugin;
interface
uses Windows;
type
IBPlugin = interface (IInterface)
['{FA895923-9826-4249-B9E7-A5D0F1CF758D}']
//声明一个方法
function MainMethod(const paramStr: string; out resultStr:OleVariant;out sErrorMsg: string):boolean;
end;
type
  TPluginProperty = record
    objClass: IBPlugin;     //DLL对应的对象
    objhandle: cardinal;    //句柄
    token: string;          //访问秘钥
    loadtype: string;       //调用方式
    filepath: string;       //dll文件路径
  end;
 //对象创建,可以传入一个参数
type
  TCreateObject = function(const paramStr: string): IBPlugin; stdcall;
implementation
end.

编译后复制DLL 文件到服务端的插件目录【Plugin】,然后在服务器页面设置插件信息:

客户端调用:

{
--------说明:-----------
PluginMethod(fileid: string; Token: string; paramStr: string; ouresultData:OleVariant; out sErrorMsg: string): Boolean;

PluginMethod :调用DLL接口函数
fileid: DLL对应的ID 
Token: 调用秘钥
paramStr: 传入的参数(与dll插件类型一致:普通字符串或json格式)
ouresultData:返回的结果
}
//例如:
procedure TFclient.Button1Click(Sender: TObject);
var
  sErrorMsg: string;
 vData: OleVariant;
begin
 
  if ser.PluginMethod('1', '123', '', vData, sErrorMsg) then
    CDS1.Data:=vData
  else
    showmessage(sErrorMsg);
end;

 源码框架下载地址:https://download.csdn.net/download/dgthm/87647460

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Delphi三层框架是一种软件开发架构,将应用程序划分为三个主要层次:用户界面层(表示层)、业务逻辑层和数据访问层。这种框架旨在实现应用程序的模块化、可维护和可扩展。 用户界面层是用户与应用程序交互的部分,通常由窗体、对话框或其他交互元素组成。在Delphi中,可以使用可视化设计工具创建用户界面,例如在窗体上放置按钮、文本框和其他控件来实现用户输入和数据展示。 业务逻辑层是处理应用程序的核心功能和业务规则的地方。在这一层中,开发人员可以编写代码来处理用户的输入、数据处理、验证和计算等操作。这些代码可以实现应用程序的业务逻辑,并将结果返回给用户界面层。 数据访问层是与数据库或其他数据源进行通信的部分。在这一层中,开发人员可以使用Delphi提供的数据库连接组件进行数据的读取、写入和查询操作。这些组件可以帮助开发人员与多种数据库系统进行交互,并提供了一套方便的API来处理数据的访问和操作。 通过将应用程序划分为这三个层次,Delphi三层框架可以实现功能的解耦、简化代码的维护和提高应用程序的性能。开发人员可以专注于不同层次的工作,从而提高开发效率和系统的可靠性。此外,这种框架还可以支持多种数据库系统,使应用程序具有更好的灵活性和可移植性。 总之,Delphi三层框架是一种用于构建模块化、可维护和可扩展应用程序的架构。它将应用程序划分为用户界面层、业务逻辑层和数据访问层,使开发人员能够更好地管理和组织代码,并实现功能的复用和系统的可靠性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值