InjectorBr is a dependency injection framework for Delphi applications. Dependency injection is a software design pattern that allows decoupling components of an application, making them more modular and easier to maintain. With it, developers can easily create modular and flexible applications, avoiding tightly coupled dependencies and reducing code complexity. The framework also includes features for managing component lifecycle.
Embarcadero Delphi XE and higher.
Installation using the boss install
command:
boss install "https://github.com/HashLoad/injectorbr"
✔️ Recurso 1
: Injector.Register<TClass>
to (Class)
✔️ Recurso 2
: Injector.RegisterLazy<TClass>
to (Class for LazyLoad)
✔️ Recurso 3
: InjectorInterface<IInterface>
to (Interface)
In addition to these three methods, the framework also offers the feature of creating a new instance of a class that is already registered. To do this, simply use the command:
✔️ Injector<TClass>.New
for (New Instance)
{ /////////////////////// Registering ///////////////////////// }
unit dfe.engine.acbr;
interface
uses
SysUtils,
dfe.engine.interfaces;
type
TDFeEngineACBr = class(TInterfacedObject, IDFeEngine)
public
class function New: IDFeEngine;
procedure Execute;
end;
implementation
{ TDFeEngineACBr }
procedure TDFeEngineACBr.Execute;
begin
raise Exception.Create('DFe Engine ACBr');
end;
class function TDFeEngineACBr.New: IDFeEngine;
begin
Result := Self.Create;
end;
initialization
InjectorBr.RegisterInterface<IDFeEngine, TDFeEngineACBr>;
end.
{ /////////////////////// Recovering ///////////////////////// }
unit global.controller;
interface
uses
DB,
Rtti,
Classes,
SysUtils,
Controls,
global.controller.interfaces,
dfe.engine.interfaces;
type
TGlobalController = class(TInterfacedObject, IGlobalController)
private
FDFeEngine: IDFeEngine;
public
constructor Create;
procedure DFeExecute;
end;
implementation
uses
app.injector;
{ TGlobalController }
constructor TGlobalController.Create;
begin
inherited;
FDFeEngine := InjectorBr.GetInterface<IDFeEngine>;
end;
procedure TGlobalController.DFeExecute;
begin
FDFeEngine.Execute;
end;
end.
{ /////////////////////// Registering ///////////////////////// }
unit dfe.engine.acbr;
interface
uses
SysUtils;
type
TDFeEngineACBr = class
public
procedure Execute;
end;
implementation
{ TDFeEngineACBr }
procedure TDFeEngineACBr.Execute;
begin
raise Exception.Create('DFe Engine ACBr');
end;
initialization
InjectorBr.RegisterSington<TDFeEngineACBr>;
end.
{ /////////////////////// Recovering ///////////////////////// }
unit global.controller;
interface
uses
DB,
Rtti,
Classes,
SysUtils,
Controls,
global.controller.interfaces,
dfe.engine.acbr;
type
TGlobalController = class(TInterfacedObject, IGlobalController)
private
FDFeEngine: TDFeEngineACBr;
public
constructor Create;
procedure DFeExecute;
end;
implementation
uses
app.injector;
{ TGlobalController }
constructor TGlobalController.Create;
begin
inherited;
FDFeEngine := InjectorBr.Get<TDFeEngineACBr>;
end;
procedure TGlobalController.DFeExecute;
begin
FDFeEngine.Execute;
end;
end.
{ /////////////////////// Registering ///////////////////////// }
unit dfe.engine.acbr;
interface
uses
SysUtils;
type
TDFeEngineACBr = class
public
procedure Execute;
end;
implementation
{ TDFeEngineACBr }
procedure TDFeEngineACBr.Execute;
begin
raise Exception.Create('DFe Engine ACBr');
end;
initialization
InjectorBr.RegisterLazy<TDFeEngineACBr>;
end.
{ /////////////////////// Recovering ///////////////////////// }
unit global.controller;
interface
uses
DB,
Rtti,
Classes,
SysUtils,
Controls,
global.controller.interfaces,
dfe.engine.acbr;
type
TGlobalController = class(TInterfacedObject, IGlobalController)
private
FDFeEngine: TDFeEngineACBr;
public
constructor Create;
procedure DFeExecute;
end;
implementation
uses
app.injector;
{ TGlobalController }
constructor TGlobalController.Create;
begin
inherited;
FDFeEngine := InjectorBr.Get<TDFeEngineACBr>;
end;
procedure TGlobalController.DFeExecute;
begin
FDFeEngine.Execute;
end;
end.
Our team would love to receive contributions to this open-source project. If you have any ideas or bug fixes, feel free to open an issue or submit a pull request.
To submit a pull request, follow these steps:
- Fork the project.
- Create a new branch. (
git checkout -b my-new-feature
) - Make your changes and commit. (
git commit -am 'Adding new functionality'
) - Push the branch. (
git push origin my-new-feature
) - Open a pull request.