探讨“数据库系统的面向对象开发” (0分)

莫知

Unregistered / Unconfirmed
GUEST, unregistred user!
数据库系统的面向对象开发(1)
nathan2001

摘 要:数据库系统的面向对象开发
关键字:面向对象 数据库
类 别:数据库

---起源-------------------
我刚学DELPHI的时候也是按书中介绍的那样做我的数据库程序。生成一个DATAMODULE,在
上面放数据控件,比如TTABLE、TQUERY、TDATASOURCE。做小程序时这种方法很好效率也可
以,一般的项目一周就可以了。但做比较大的项目就有些混乱了。在一个DataModule里放上
好几十个数据控件,搞得我晕头转向。当然你也可以把数据控件放在多个DataModule里,这
样可以让你不晕头转向但程序“肥”了起来。这样做还有一个缺点,你不能把商业规则(
Business Logic)放在一处,程序做到最后你可能已经不知道哪儿是哪儿了。还会出现摸名
奇妙的错误。

---分析--------------------
用面向对象的方法做一般的应用程序比数据库程序容易一些,因为它不涉及到数据库。但
在做数据库程序时应该考虑类跟跟数据库是怎样联系的。还有DELPHI提供很多数据控件我们
是不是用它还是用一般的控件。

---解决--------------------
创建一个跟数据库联系的基类,其他的类从基类继承。
基类:DbRecordSet
TDbRecordset=class(TComponent)
private
FMasterFields: string;
FMasterSource: TDataSource;
function GetIsEmpty: boolean;
function GetRecordCount: integer;
function GetState: TDataSetState;
function GetDataSource: TDataSource;
procedure SetMasterFields(const Value: string);
procedure SetMasterSource(const Value: TDataSource);
protected
FTable:TTable;
FQuery:TQuery;
FDataSource:TDataSource;
FDisplayFields:TStringList;
FSqlType:TSqlType;
FIsEmpty:boolean;
FRecordCount:integer;
FOnRecordScrolled:TNotifyEvent;
FRecordListSource:TDataSource;
procedure FTableAfterScroll(DataSet: TDataSet);
function GetDisplayField: TStringList;virtual;
public
property DataSet:TTable read FTable;
property DataSource:TDataSource read GetDataSource;
property IsEmpty:boolean read GetIsEmpty;//表是不是为空
property RecordCount:integer read GetRecordCount;//记录数
property State:TDataSetState read GetState;
property DisplayField:TStringList read GetDisplayField;//要在DBGrid显示的字段
property RecordScrolled:TNotifyEvent read FOnRecordScrolled write FOnRecordScrolled;//记录滚动事件
property MasterSource:TDataSource read FMasterSource write SetMasterSource;
property MasterFields:string read FMasterFields write SetMasterFields;
property RecordListSource:TDataSource read FRecordListSource;//记录集合
procedure Insert;virtual;//添加记录
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
function Delete:boolean;virtual;//删除记录
function EmptyTable:boolean;virtual;
function First:boolean;virtual;//第一条记录
function Last:boolean;virtual;//最后一条记录
function Next:boolean;virtual;//下一条记录
function Prior:boolean;virtual;//前一条记录
function Post:boolean;virtual;//保存记录
function NoMsgPost:boolean;virtual;//没有提示信息的保存
function Cancel:boolean;virtual;
procedure OpenSql(TheQuery:TQuery;SqlStr:string;SqlType:TSqlType);//运行SQL语句
procedure RefreshRecordList;virtual;
procedure PRefresh;virtual;
end

图书类-TBooks
const
C_Book='Book';//表名
TBook=class(TDbRecordset)
private
FISBN:string;
FBookName:string;
FBookType:TBookType;
FBookTypeId:string;
FAuthor:string;
FPublisher:string;
FContent:string;
function GetBookName: string;
function GetContent: string;
function GetISBN: string;
function GetPublisher: string;
function GetType: string;
function GetAuthor:string;
procedure SetBookName(const Value: string);
procedure SetContent(const Value: string);
procedure SetISBN(const Value: string);
procedure SetPublisher(const Value: string);
procedure SetType(const Value: string);
procedure SetAuthor(const Value: string);
function GetBookTypeList: TStringList;
procedure BindFieldValues;
public
property ISBN:string read GetISBN write SetISBN;
property BookName:string read GetBookName write SetBookName;
property BookType:string read GetType write SetType;
property Author:string read GetAuthor write SetAuthor;
property Publisher:string read GetPublisher write SetPublisher;
property Content:string read GetContent write SetContent;
property BookTypeList:TStringList read GetBookTypeList;
constructor Create(AOwner:TComponent);override;
destructor Destroy;override;
function Post:boolean;override;
end;

这是我的一些意见,请大家多多指教.
最近找到相关的网站--www.martinfowler.com/ISA
写的很好,基本意思是一样的有用JAVA写的范例


 
等待你的(2)[:)]
 
不是我写的,转载的是Codelphi.com上的文章!
 
跟个帖子了。
呵呵!
再继续了。
 
很早以前就好象有面向对象的数据库
但那个时候好象是失败了
 
左轻候的RE就是很好的例子,,
最近我一直在研究....打算在我这次接手的项目中应用一下...

面向对象的数据库? 我没见过呀,,,现在有么? 是什么?
 
左轻候的RE?

我怎么没见过,你有吗?送我一份吧。

zzmcy@21cn.com
 
去他主页下呀..
http://onekey.yeah.net
 
原来是 Rich Explorer ADO版呀

这个有,有的。

就是查起来太慢,之后我改用了CHM版
 
呵呵,可是他的设计方法很好呀...
数据逻辑和界面部分完全分离,,,,,..

PS:离线数据当然用CHM版啦....:)
 
当然,当然了,

我也研究过他的这个程序,
很有收益呀!
 
MVC模式的应用
 
哪位富翁能给个RE的其他连接?左轻侯主页上的连接无效了。
 
有点新意,关注!
 
关注,收藏。值得仔细研究研究。
 
很好,我也一直在考虑这个问题

GOOD GOOD STUDY,DAY DAY UP!
 
我觉得,你是否多此一举了?
按照你的说法,每个表的记录都要继承TDbRecordset,如果表多的话,
手工写的代码就很长了,而且代码简单,但重复的多,基本上都是copy+paste的。

区别仅仅是为了类似这样:
TQuery.FieldByName('BookName').AsString和TBook.BookName的上书写区别


 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
577
import
I
顶部