Hola pape19.
Decir la manera "correcta" de hacerlo sería como decir que tu código es incorrecto y no es así, por que como mencionas, funciona bién. Pero creo que se le podrían hacer algunas mejoras.
Si bién está probado, el código siguiente es sólo un ejemplo de guía.
Código Delphi
[-]
unit uEmpleado;
interface
uses
Windows, Messages, SysUtils, Controls;
type
TDataSetAction = (daEdit, daInsert, daAppend);
type
TEmpleado = class(TObject)
private
FNombre: string;
FNacimiento: TDate;
FReferencia: Integer;
procedure SetNacimiento(const Value: TDate);
procedure SetNombre(const Value: string);
procedure SetReferencia(const Value: Integer);
procedure SetAction(const Value: TDataSetAction);
public
constructor Create;
procedure Save;
procedure Delete;
property Action: TDataSetAction write SetAction;
property Nombre: string read FNombre write SetNombre;
property Nacimiento: TDate read FNacimiento write SetNacimiento;
property Referencia: Integer read FReferencia write SetReferencia;
destructor Destroy; override;
end;
implementation
uses Unit3;
constructor TEmpleado.Create;
begin
inherited;
with DataModule1 do
begin
if not IBTransaction1.InTransaction then
IBTransaction1.StartTransaction;
IBDataSet1.Open;
end;
end;
procedure TEmpleado.SetAction(const Value: TDataSetAction);
begin
with DataModule1.IBDataSet1 do
case Value of
daEdit: Edit;
daInsert: Insert;
daAppend: Append;
end;
end;
procedure TEmpleado.SetNombre(const Value: string);
begin
if Value <> FNombre then
FNombre := Value;
end;
procedure TEmpleado.SetNacimiento(const Value: TDate);
begin
if Value <> FNacimiento then
FNacimiento := Value;
end;
procedure TEmpleado.SetReferencia(const Value: Integer);
begin
if Value <> FReferencia then
FReferencia := Value;
end;
procedure TEmpleado.Save;
begin
if Nombre = EmptyStr then
raise Exception.Create('Error: Nombre es un dato requerido');
with DataModule1 do
begin
IBDataSet1.FieldByName('REFERENCIA').AsInteger := Referencia;
IBDataSet1.FieldByName('NOMBRE').AsString := Nombre;
IBDataSet1.FieldByName('FECHA_NAC').AsDateTime := Nacimiento;
IBDataSet1.Post;
end;
end;
procedure TEmpleado.Delete;
begin
if MessageBox(0,'¿ Desea eliminar el registro ?',
'',MB_ICONQUESTION+MB_YESNO)= IDYES then
DataModule1.IBDataSet1.Delete;
end;
destructor TEmpleado.Destroy;
begin
with DataModule1 do
begin
IBDataSet1.Close;
IBTransaction1.Commit;
end;
inherited;
end;
end.
Un ejemplo de uso:
Código Delphi
[-]
...
implementation
uses uEmpleado;
var
Empleado: TEmpleado;
procedure TForm1.FormCreate(Sender: TObject);
begin
Empleado:= TEmpleado.Create;
end;
procedure TForm1.btnEditClick(Sender: TObject);
begin
with Empleado do
begin
Action:= daEdit;
Referencia:= IntToStr(EditRef.Text);
Nombre:= EditNomb.Text;
Nacimiento:= DateToStr(EditNac.Text);
Save;
end;
end;
procedure TForm1.btnInsertClick(Sender: TObject);
begin
with Empleado do
begin
Action:= daInsert;
Referencia:= IntToStr(EditRef.Text);
Nombre:= EditNomb.Text;
Nacimiento:= DateToStr(EditNac.Text);
Save;
end;
end;
procedure TForm1.btnDeleteClick(Sender: TObject);
begin
Empleado.Delete;
end;
...
procedure TForm1.FormDestroy(Sender: TObject);
begin
Empleado.Free;
end;
Saludos