يتكون Side4Delphi من مكتبة تتيح استخدام مفهوم البرمجة الموجهة نحو الجانب (AOP) في دلفي.
AOP هو نموذج برمجة يهدف إلى زيادة النموذج من خلال السماح بفصل المخاوف المتقاطعة. يفعل ذلك عن طريق إضافة سلوك إضافي إلى التعليمات البرمجية الحالية دون تعديل الكود نفسه. بدلاً من ذلك ، يمكننا إعلان هذا الرمز الجديد وهذه السلوكيات الجديدة بشكل منفصل.
Side4Delphi يساعدنا على تنفيذ هذه المخاوف المتقاطعة.
للحصول على نسخة محلية وتشغيل اتبع هذه الخطوات البسيطة.
لاستخدام هذه المكتبة ، يلزم إصدار محدث من Delphi IDE (XE أو أعلى).
استنساخ الريبو
git clone https://github.com/ezequieljuliano/Aspect4Delphi.git
أضف "مسار البحث" الخاص بـ IDE أو مشروعك على الدلائل التالية:
Aspect4Delphisrc
لتوفير نموذج التوجيه الجانبي في مشروعك مع Side4Delphi تحتاجه:
لتوضيح الاستخدام ، دعونا نلقي نظرة على حل لإدارة سجلات التطبيق.
قم بتنفيذ سمة نقطة (يفضل أن ترث من SideAttribute):
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.
في فئة قاعدة عملك ، استخدم السمة لتحديد نقاط انضمامك. يمكنك استخدام المُنشئ والمدمر لإضافة فصلك في ويفر.
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 ) موزعة تحت رخصة أباتشي. انظر LICENSE لمزيد من المعلومات.
للاتصال بنا ، استخدم الخيارات:
https://github.com/ezequieljuliano/aspect4delphi