Apparter4Delphi ประกอบด้วยห้องสมุดที่ช่วยให้ใช้แนวคิดของการเขียนโปรแกรมเชิงภาพ (AOP) ใน Delphi
AOP เป็นกระบวนทัศน์การเขียนโปรแกรมที่มีจุดมุ่งหมายเพื่อเพิ่มความเป็นโมดูลโดยอนุญาตให้แยกข้อกังวลข้ามการตัดข้าม มันทำเช่นนั้นโดยการเพิ่มพฤติกรรมเพิ่มเติมลงในรหัสที่มีอยู่โดยไม่ต้องปรับเปลี่ยนรหัสเอง แต่เราสามารถประกาศรหัสใหม่นี้และพฤติกรรมใหม่เหล่านี้แยกกัน
APPERTH4DELPHI ช่วยให้เรานำข้อกังวลข้ามการตัดเหล่านี้มาใช้
เพื่อรับสำเนาท้องถิ่นและเรียกใช้ทำตามขั้นตอนง่าย ๆ เหล่านี้
ในการใช้ไลบรารีนี้จำเป็นต้องมีเวอร์ชันที่อัปเดตของ Delphi IDE (XE หรือสูงกว่า)
โคลน repo
git clone https://github.com/ezequieljuliano/Aspect4Delphi.git
เพิ่ม "เส้นทางการค้นหา" ของ IDE หรือโครงการของคุณไดเรกทอรีต่อไปนี้:
Aspect4Delphisrc
เพื่อจัดเตรียมกระบวนทัศน์การวางแนวในโครงการของคุณด้วย APPERS4DELPHI ที่คุณต้องการ:
เพื่อแสดงให้เห็นการใช้งานลองดูวิธีการจัดการบันทึกของแอปพลิเคชัน
ใช้แอตทริบิวต์ PointCut (ควรสืบทอดมาจาก Appistattribute):
interface
uses
Aspect.Core;
type
LoggingAttribute = class(AspectAttribute)
private
{ private declarations }
protected
{ protected declarations }
public
{ public declarations }
end;
implementation
end.
ใช้คลาสที่แสดงถึงแง่มุมของคุณและมีวิธีการให้คำแนะนำ (คุณต้องใช้อินเตอร์เฟส 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.
ลงทะเบียนแอตทริบิวต์ PointCut ของคุณและคลาสมุมมองในบริบท (ควรเก็บบริบทนี้ไว้ในอินสแตนซ์ 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.
ในคลาสกฎธุรกิจของคุณใช้แอตทริบิวต์เพื่อกำหนดคะแนนการรวมของคุณ คุณสามารถใช้ตัวสร้างและ destructor เพื่อเพิ่มชั้นเรียนของคุณที่ผู้ประกอบ
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;
ดูปัญหาเปิดสำหรับรายการคุณสมบัติที่เสนอ (และปัญหาที่รู้จัก)
การมีส่วนร่วมคือสิ่งที่ทำให้ชุมชนโอเพ่นซอร์สเป็นสถานที่ที่น่าทึ่งในการเรียนรู้สร้างแรงบันดาลใจและสร้าง การมีส่วนร่วมใด ๆ ที่คุณทำ จะได้รับการชื่นชมอย่างมาก
git checkout -b feature/AmazingFeature )git commit -m 'Add some AmazingFeature' )git push origin feature/AmazingFeature ) แจกจ่ายภายใต้ใบอนุญาต Apache ดู LICENSE สำหรับข้อมูลเพิ่มเติม
หากต้องการติดต่อเราใช้ตัวเลือก:
https://github.com/ezequieljuliano/aspect4delphi