Aspek4Delphi terdiri dari perpustakaan yang memungkinkan penggunaan konsep pemrograman berorientasi aspek (AOP) di Delphi.
AOP adalah paradigma pemrograman yang bertujuan untuk meningkatkan modularitas dengan memungkinkan pemisahan kekhawatiran silang. Ia melakukannya dengan menambahkan perilaku tambahan ke kode yang ada tanpa modifikasi kode itu sendiri. Sebaliknya, kita dapat mendeklarasikan kode baru ini dan perilaku baru ini secara terpisah.
Aspek4Delphi membantu kami menerapkan kekhawatiran silang ini.
Untuk mendapatkan salinan lokal dan berjalan ikuti langkah -langkah sederhana ini.
Untuk menggunakan pustaka ini, diperlukan versi terbaru dari Delphi IDE (XE atau lebih tinggi).
Kloning repo
git clone https://github.com/ezequieljuliano/Aspect4Delphi.git
Tambahkan "jalur pencarian" dari IDE Anda atau proyek Anda di direktori berikut:
Aspect4Delphisrc
Untuk memberikan paradigma orientasi aspek dalam proyek Anda dengan aspek4delphi yang Anda butuhkan:
Untuk mengilustrasikan penggunaan, mari kita lihat solusi untuk mengelola log aplikasi.
Menerapkan atribut pointcut (lebih disukai mewarisi dari aspectAttribute):
interface
uses
Aspect.Core;
type
LoggingAttribute = class(AspectAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
implementation
end.
Menerapkan kelas yang mewakili aspek Anda dan berisi metode saran (Anda harus mengimplementasikan antarmuka iaspect):
interface
uses
System.SysUtils,
System.Rtti,
Aspect,
Aspect.Core,
Logging.Attribute,
App.Context;
type
ELoggingAspectException = class(Exception)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
TLoggingAspect = class(TAspectObject, IAspect)
private
{ private declarations }
protected
procedure OnBefore(
instance: TObject;
method: TRttiMethod;
const args: TArray<TValue>;
out invoke: Boolean;
out result: TValue
); override;
procedure OnAfter(
instance: TObject;
method: TRttiMethod;
const args: TArray<TValue>;
var result: TValue
); override;
procedure OnException(
instance: TObject;
method: TRttiMethod;
const args: TArray<TValue>;
out raiseException: Boolean;
theException: Exception;
out result: TValue
); override;
public
{ public declarations }
end;
implementation
{ TLoggingAspect }
procedure TLoggingAspect.OnAfter(instance: TObject; method: TRttiMethod;
const args: TArray<TValue>; var result: TValue);
var
attribute: TCustomAttribute;
begin
inherited;
for attribute in method.GetAttributes do
if attribute is LoggingAttribute then
begin
LoggingFile.Add('After the execution of ' +
instance.QualifiedClassName + ' - ' +
method.Name
);
Break;
end;
end;
procedure TLoggingAspect.OnBefore(instance: TObject; method: TRttiMethod;
const args: TArray<TValue>; out invoke: Boolean; out result: TValue);
var
attribute: TCustomAttribute;
begin
inherited;
for attribute in method.GetAttributes do
if attribute is LoggingAttribute then
begin
LoggingFile.Add('Before the execution of ' +
instance.QualifiedClassName + ' - ' +
method.Name
);
Break;
end;
end;
procedure TLoggingAspect.OnException(instance: TObject; method: TRttiMethod;
const args: TArray<TValue>; out raiseException: Boolean;
theException: Exception; out result: TValue);
var
attribute: TCustomAttribute;
begin
inherited;
for attribute in method.GetAttributes do
if attribute is LoggingAttribute then
begin
LoggingFile.Add('Exception in executing ' +
instance.QualifiedClassName + ' - ' +
method.Name + ' - ' +
theException.Message
);
Break;
end;
end;
end.
Daftarkan atribut pointcut Anda dan kelas aspeknya dalam konteks (lebih disukai menyimpan konteks ini dalam contoh singleton):
interface
uses
Logging.Aspect;
Aspect.Context;
function AspectContext: IAspectContext;
implementation
var
AspectContextInstance: IAspectContext = nil;
function AspectContext: IAspectContext;
begin
if (AspectContextInstance = nil) then
begin
AspectContextInstance := TAspectContext.Create;
AspectContextInstance.RegisterAspect(TLoggingAspect.Create);
end;
Result := AspectContextInstance;
end;
end.
Di kelas aturan bisnis Anda, gunakan atribut untuk menentukan poin gabungan Anda. Anda dapat menggunakan konstruktor dan destruktor untuk menambahkan kelas Anda di penenun.
interface
uses
System.SysUtils,
Logging.Attribute,
App.Context;
type
EWhatsAppMessageException = class(Exception)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
TWhatsAppMessage = class
private
{ private declarations }
protected
{ protected declarations }
public
constructor Create;
destructor Destroy; override;
[Logging]
procedure Send; virtual;
end;
implementation
{ TWhatsAppMessage }
constructor TWhatsAppMessage.Create;
begin
inherited Create;
AspectContext.Weaver.Proxify(Self);
end;
destructor TWhatsAppMessage.Destroy;
begin
AspectContext.Weaver.Unproxify(Self);
inherited Destroy;
end;
procedure TWhatsAppMessage.Send;
begin
//Execution of send.
end;
Lihat masalah terbuka untuk daftar fitur yang diusulkan (dan masalah yang diketahui).
Kontribusi inilah yang membuat komunitas open source menjadi tempat yang luar biasa untuk dipelajari, menginspirasi, dan menciptakan. Kontribusi apa pun yang Anda buat sangat dihargai .
git checkout -b feature/AmazingFeature )git commit -m 'Add some AmazingFeature' )git push origin feature/AmazingFeature ) Didistribusikan di bawah lisensi Apache. Lihat LICENSE untuk informasi lebih lanjut.
Untuk menghubungi kami, gunakan opsi:
https://github.com/ezequieljuliano/aspect4delphi