Ir para conteúdo
Fórum Script Brasil
  • 0

(Resolvido) Trabalhando com MVC no Delphi


SCIENTIST

Pergunta

Boa Noite, estou trabalhando com Padrão MVC no delphi e estou tendo problemas na minha Camada Controller, mais especificamente em um tipo Enumerated.

Tenho o seguinte tipo na minha Classe ControllerCliente:

TEstadoClienteController = (active, insert, edit);
Tenho também uma variável chamada Estado do tipo TEstadoClienteController. Por último tenho um método público que retorna a variável estado:
function TClienteController.getEstadoAtual: TEstadoClienteController;
begin
 result := estado;
end;
Porém quando pego este retorno no formulário (camada VIEW) e faço o código abaixo:
if (controller.getEstadoAtual = active) then
showmessage('ATIVO')
else if (controller.getEstadoAtual = insert) then
showmessage('INSERIR')
else if (controller.getEstadoAtual = edit) then
showmessage('EDITAR');

Ele da o seguinte erro:

[DCC Error] uFrmCliente.pas(55): E2008 Incompatible types

Link para o comentário
Compartilhar em outros sites

5 respostass a esta questão

Posts Recomendados

  • 0
  • 0

pois então, veja bem: eu consigo recuperar a variável "ESTADO" no tipo enumerated em outra classe, até debuguei e o valor de ESTADO está como ACTIVE (corretíssimo). Porém faço o seguinte:

est = controller.getEstadoAtual();

if est = active then

showmessage('ativo");

da erro na linha "if est = active then "

Link para o comentário
Compartilhar em outros sites

  • 0

enumeração de tipos

exemplo:

type
   TEstado =  (active, insert, edit); // Define a enumeração

 var
   Estado: TEstado; // variável Uma enumeração

 begin
   Estado := active;//  Atribuir a uma variável enumerada

   if Estado = active then  
      showmessage('ativo");

 end;

veja o que consegue

abraço

Link para o comentário
Compartilhar em outros sites

  • 0

Pois então Jhonas, eu consigo fazer o código acima funcionar na mesma UNIT que o ENUMERATED TEstado foi declarado, porém se faço isso em outra UNIT não funciona (mesmo dando USES na UNIT)

Aqui vai a UNIT onde funciona, ClienteController:

unit ClienteController;

interface

uses
  PojoCliente, DaoCliente;

type
  TEstadoClienteController = (active, insert, edit);

  TClienteController = Class

   private
   pojo: TPojoCliente;
   dao: TDaoCliente;
   estado: TEstadoClienteController;
   class var Instance: TClienteController;
   constructor PrivateCreate;

   public
   class function getInstance: TClienteController;
   constructor create;
   destructor destroy;override;
   procedure Salvar;
   function Inserir:TPojoCliente;
   function getInstanceCliente:TPojoCliente;
   function getEstadoAtual:TEstadoClienteController;


End;

implementation

uses
  SysUtils, Dialogs;

{ TClienteController }

constructor TClienteController.create;
begin
 raise Exception.Create('Você não pode chamar o construtor da classe diretamente, chame o método getInstance()');
end;

destructor TClienteController.destroy;
begin
  inherited;
end;

function TClienteController.getEstadoAtual: TEstadoClienteController;
begin
 result := estado;
end;

class function TClienteController.getInstance: TClienteController;
begin
 if not Assigned(Instance) then
  Instance := TClienteController.PrivateCreate;
 result := Instance;
end;

function TClienteController.getInstanceCliente: TPojoCliente;
begin
 result := pojo;
end;

function TClienteController.Inserir:TPojoCliente;
begin
    estado := insert;
    pojo := TPojoCliente.Create;
    dao := TDaoCliente.getInstance;
    Result := pojo;
end;

constructor TClienteController.PrivateCreate;
begin
 inherited create;
 estado := active;
end;

procedure TClienteController.Salvar;
begin
  if (dao.Inserir(pojo)) then
      estado := active;
end;

end.
Agora o Formulario que estou tentando usar isso:
unit uFrmCliente;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ClienteController, ActnList, DB, ADODB, Grids,
  DBGrids;

type
  TfrmCliente = class(TForm)
    _leID: TLabeledEdit;
    _leNOME: TLabeledEdit;
    _leIDADE: TLabeledEdit;
    _btSalvar: TButton;
    _btInserir: TButton;
    ActionList1: TActionList;
    _acInserir: TAction;
    _actSalvar: TAction;
    Button1: TButton;
    DBGrid1: TDBGrid;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure _acInserirUpdate(Sender: TObject);
    procedure _acInserirExecute(Sender: TObject);
    procedure _actSalvarUpdate(Sender: TObject);
    procedure _actSalvarExecute(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    controller: TClienteController;

  public
    { Public declarations }
    constructor create(AOwner:TComponent;Controller:TClienteController);reintroduce;
  end;

var
  frmCliente: TfrmCliente;

implementation

uses
  PojoCliente, TypInfo;

{$R *.dfm}

{ TfrmCliente }



{ TfrmCliente }

procedure TfrmCliente.Button1Click(Sender: TObject);
var
 est: TEstadoClienteController;
begin
est := controller.getEstadoAtual;
     if est = active then
     showmessage('ativo');
end;

constructor TfrmCliente.create(AOwner: TComponent;
  Controller: TClienteController);
begin
 inherited create(AOwner);
 self.controller := Controller;
end;

procedure TfrmCliente.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  controller.Destroy;
end;

procedure TfrmCliente._acInserirExecute(Sender: TObject);
begin
 controller.Inserir;
end;

procedure TfrmCliente._acInserirUpdate(Sender: TObject);
begin
 {   if controller.getEstadoAtual = active then
 TAction(Sender).Enabled := true
 else
 TAction(Sender).Enabled := false;  }
end;

procedure TfrmCliente._actSalvarExecute(Sender: TObject);
begin
   (controller.getInstanceCliente).setId(strtoint(_leID.Text));
 (controller.getInstanceCliente).setNome(_leNOME.Text);
 (controller.getInstanceCliente).setIdade(strtoint(_leIDADE.Text));

 controller.Salvar;
end;

procedure TfrmCliente._actSalvarUpdate(Sender: TObject);
begin
{ if (controller.getEstadoAtual = insert) or (controller.getEstadoAtual = edit) then
 TAction(Sender).Enabled := true
 else
 TAction(Sender).Enabled := false;  }
end;

end.

Editado por SCIENTIST
Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      652k
×
×
  • Criar Novo...