I remember that I have seen a post about the database and face-oriented, so I wrote such a small thing, (as a demonstration, it is also a right, 嘿嘿), if there is a problem, please correct it! ------- CLH // Abstract Interface Unit User;
Interface
type TUser = class protected FUserName: string; FPassword: string; FEmail: string; procedure SetUserName (const AName: string); virtual; abstract; function GetUserName: string; virtual; abstract; procedure SetPassword (const APass: string); virtual; abstract; function GetPassword: string; virtual; abstract; procedure setEmail (const AEmail: string); virtual; abstract; function getEmail: string; virtual; abstract; public property UserName: string read GetUserName write SetUserName; property Password: string read GetPassword write SetPassword; Property Email: String ReadeMail Write set device;
TUserBuilder = class private FUser: TUser; public procedure SetUserName (const AName: string); dynamic; procedure SetPassword (const APass: string); dynamic; procedure SetEmail (const AEmail: string); dynamic; constructor Create (User: TUser); END;
IMPLEMENTATION
{Tuserbuilder}
Constructor tuserbuilder.create (user: tuse); begin fuse: = user; end;
Procedure tuserbuilder.seTemail (const aemail: string); begin fuse.setemail (aemail);
Procedure tuserbuilder.setpassword (const apass); begin fuse.setpassword (apass);
Procedure tuserbuilder.setUsername (const aname: string); begin fusetusername (aname);
End.
// Real user
Unit generaluser;
Interface
User, Sysutils, Windows, Controls, Classes, Variants;
type TGeneralUser = class (TUser) protected procedure SetUserName (const AName: string); override; function GetUserName: string; override; procedure SetPassword (const APass: string); override; function GetPassword: string; override; procedure SetEmail (const AEmail: String); OVERRIDE; Function getemail: string; override; end; importation
{TGENERALUSER}
Function TGENERALUSER.GETEMAIL: String; Begin Result: = feedail;
Function TGENERALUSER.GETPASSWORD: STRING; begin if fpassword = '' Ten Result: = '' Else Result: = fpassword;
Function TGENERALUSER.GETUSERNAME: String; Begin Result: = FUSERNAME; END;
Procedure TGeneraLUser.setemail (const aemail: string); Begin if Aemail <> '' Then Femail: = AEMAIL; END;
Procedure TGeneraLUser.SetPassword (const apass: string); begin if apass <> '' the fpassword: = apass; end;
Procedure TGeneraLUser.SetUserName (const aname: string); begin if aname <> '' Then fusername: = aname;
End.
// User Control Interface Unit UserController;
Interface
Uses user;
Tuser; function insert (user: tuse): boolean; function delete (username: string): boolean;
IMPLEMENTATION
End.
// Proxy user controls Unit ProxyUserController;
Interface
Uses User, UserController, DBController;
type TProxyUserController = class (TInterfacedObject, IUserController) private FDbUserController: IUserController; public function GetUser (UserName: string): TUser; function Insert (User: TUser): Boolean; function Update (User: TUser): Boolean; function Delete (UserName: String): boolean; username: string: boolean; constructor create (connStr: String); end; importation
{TPROXYUSERCONTROLLLLER}
Function TProxyUserController.checkexsit (username: string): boolean; Begin
END;
Constructor TProxyUserController.create (Connstr: String); Begin FDBUSERCONTROLLER: = TDBController.create (ConnTr);
Function TProxyUserController.delete (username: string): boolean; begin fdbuserController.delete (username); end;
Function TProxyUserController.getuser (username: string): Tuser; Begin Result: = fdbuserController.getuser (Username);
Function TProxyUserController.Insert (user: tuse): boolean; begin if not checkexsit (user.username) THEN EXIT ELSE FDBUSERCONTROLLLLER.INSERT (user);
Function TProxyUserController.Update (user: tuser): boolean; begin fdbuserController.Update (user);
End.
// Real user control class, mainly to deal with the database unit dbcontroller;
Interface
Uses User, UserController, Resource, DB, AdoDB, SysuTils
type TDbController = class (TInterfacedObject, IUserController) private FConnection: TADOConnection; FQuery: TADOQuery; public function GetUser (UserName: string): TUser; function Insert (User: TUser): Boolean; function Update (User: TUser): Boolean; function DELETE (UserName: String): Boolean; Constructor Create (Connstr: String); Virtual; Destructor Destroy; Override;
IMPLEMENTATION
{TDBController}
constructor TDbController.Create (connStr: string); begin FConnection: = TADOConnection.Create (nil); FConnection.ConnectionString: = connStr; if not FConnection.Connected then FConnection.Connected: = true; FQuery: = TADOQuery.Create (nil) FQuery.Connection: = fConnection; end; function tdbcontroller.delete (username: string): boolean; begin
END;
DEStructor TdbController.Destroy; begin fconnection.close; freeandnil (fconnection); freeandnil; inherited;
Function TDBController.getuser (username: string): Tuser; Begin
END;
Function TDBController.Insert (User: tuse): Boolean; Begin with FQuery Do Begin Close; SQL.Clear; Sql.add ('INSERT INTO Account (Username, Pass, Email) Values (: username,: pass,: email)' Parameters.Parambyname ('username'). Value: = user.usename; parameters.parambyname ('pass'). Value: = user.password; parameters.parambyname ('email'). Value: = user.email; EXECSQL; END;
Function TDBController.Update (user: tuse): Boolean; Begin
END;
End.
// Client Unit Main;
Interface
Uses Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Generaluser, User, UserController, DBController, ProxyuserController;
type TForm1 = class (TForm) Button1: TButton; Edit1: TEdit; Edit2: TEdit; Edit3: TEdit; Button2: TButton; procedure FormCreate (Sender: TObject); procedure Button1Click (Sender: TObject); private FUser: TUser; FUserController: IuserController; fuserbuilder: tuserbuilder; {private declarations} end;
Var Form1: TFORM1;
IMPLEMENTATION
{$ R * .dfm}
procedure TForm1.FormCreate (Sender: TObject); var DbPath: string; begin DbPath: = 'Provider = Microsoft.Jet.OLEDB.4.0; Data Source =' ExtractFilePath (Application.ExeName) 'user.mdb' '; Persist Security Info = false '; FUser: = TGeneralUser.Create; FUserBuilder: = TUserBuilder.Create (FUser); FUserController: = TProxyUserController.Create (dbPath); end; procedure TForm1.Button1Click (Sender: TObject); begin FUserBuilder.SetUserName (edit1.text); fuserbuilder.setpassword (edit2.text); fuserbuilder.setemail (edit3.text); FuserController.insert (FUSER); END;
End.