Access variable contained in a BPL

2

I have a system developed in DelphiXE3 and I'm starting to make it modular. However another question arose. In a Unit that will be in one of the modules, I have:

type 
   TUsuario = record
     id:Integer;
     idEmpresa:Integer;
     usuario:string;
     foto:string;
     validade: TDateTime;
     senha:string;
     nome:string;
     admin:Boolean;
     ultimoAcesso:TDateTime;
     dataCadastro:TDateTime;
   end;

and

var
  usuario:TUsuario;

At certain times I will feed this variable and in others I will retrieve the information from it. My question: How to do this?


Updating

My system is distributed as follows:

Aplicação - Menu - Login - Package Default (Runtime Package) * Unit TUsuario * Unit FuncoesDefault * Unit FuncoesSeguranca - Package Cadastros (Carregado via LoadPackage) * CD001 - Uma tela de cadastros qualquer

Access Violation error, I'm creating the variable inside the Default package in Unit FunCodeDefault .

In my application, on the login screen, I instantiate the variable already created:

usuario:= TUsuario.Create;

In Event FormShow of form CD001, I call a function of the Unit FunConservice , responsible for checking the user's rights (insert, edit, remove , etc). This function is given a string with the screen code and my usuario variable, which was previously created on the login screen.

Soon on the first line of my function there is a if usuario.isAdmin to check if the user is an administrator. At this point I already get the Access Violation error.


Updating

Following is an excerpt from my Unit FuncoesDefault

unit FuncoesDefault;

interface

uses
  U_TUsuario;

var
  usuario:TUsuario;

implementation

initialization
  usuario := TUsuario.Create;

end.
    
asked by anonymous 30.04.2014 / 13:49

1 answer

4

First of all, I recommend that you abandon the practice of using record and start using class , which is a more modern and much more powerful language construction. To do this, study a bit about object-oriented programming, classes, constructor , destructor , methods, and properties.

Regarding you asked, once the TUsuario class is declared, declare a utility class to operate on those objects, for example TUsuarioRepositorio , which will have a series of methods to retrieve and save instances of TUsuario . Something like:

type
  TUsuarioRepositorio
  public
    class function UsuarioPorNome(const aNome: string): TUsuario;
    class procedure Salvar(aUsuario: TUsuario);
  end;

So in a certain part of your code you would create a user like this:

var
  novoUsuario: TUsuario;
begin
  ...
  novoUsuario := TUsuario.Create;
  novoUsuario.Nome := 'Huguinho';
  novoUsuario.Senha := 'M@rgar1da';
  novoUsuario.Admin := True;

  TUsuarioRepositorio.Salvar(novoUsuario);
  ...
end;

In this code an object of class TUsuario is instantiated, some of its properties are initialized and it is saved somewhere (file or database). Then, to retrieve this same user the code could be:

var
  usuarioProcurado: TUsuario;
  nome: string;
begin
  ...
  nome := 'Huguinho';
  usuarioProcurado := TUsuarioRepositorio.UsuarioPorNome(nome);
  if usuarioProcurado <> nil then
    ShowMessage(Format('O usuário %s tem o Id=%d', [usuarioProcurado.Nome, usuarioProcurado.Id]))
  else
    ShowMessage('Nenhum usuário existe com o nome='+nome); 
  ...
end;

With this working model you have separated the object from the code that manipulates the persistence of this object, which is great programming practice and design , and allows a high level of code modularization , because you can put the objects in package and repositories in another.

As the idea is to modularize you can put unit which declares TUsuario (for example, Usuarios.pas ) into a separate package . When compiled, the package project will generate a .BPL file (for example, SegurancaD17.BPL ) and a .DCP file ( SegurancaD17.DCP ). The package project should be configured to be a runtime package and the project of your application should indicate at least this as a runtime package .

In your application just uses of unit Usuarios . When compiling your project Delphi will notice that unit is in a runtime package and will generate dependency for it instead of .EXE >. When you distribute .EXE , you should distribute along SegurancaD17.BPL , the .dcp file is important to the compiler, not to the executable.

    
01.05.2014 / 15:22