Aspect4Delphiは、Delphiでアスペクト指向プログラミング(AOP)の概念を使用できるライブラリで構成されています。
AOPは、交差懸念の分離を可能にすることでモジュール性を高めることを目的とするプログラミングパラダイムです。これは、コード自体を変更せずに既存のコードに追加の動作を追加することで行います。代わりに、この新しいコードとこれらの新しい動作を個別に宣言できます。
Aspect4delphiは、これらの相互習慣の懸念を実装するのに役立ちます。
地元のコピーを稼働させるには、これらの簡単な手順に従ってください。
このライブラリを使用するには、Delphi IDE(XE以上)の更新バージョンが必要です。
レポをクローンします
git clone https://github.com/ezequieljuliano/Aspect4Delphi.git
IDEの「検索パス」またはプロジェクトを次のディレクトリに追加します。
Aspect4Delphisrc
プロジェクトのアスペクトオリエンテーションパラダイムをAspect4delphiで提供するには、必要です。
使用法を説明するために、アプリケーションのログを管理するためのソリューションを見てみましょう。
PointCut属性を実装します(できればAspectAttributeから継承):
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 )Apacheライセンスの下で配布されます。詳細については、 LICENSE参照してください。
お問い合わせのオプションを使用してください。
https://github.com/ezequieljuliano/aspect4delphi