측
AOP는 교차 절단 문제의 분리를 허용함으로써 모듈성을 높이는 것을 목표로하는 프로그래밍 패러다임입니다. 코드 자체를 수정하지 않고 기존 코드에 추가 동작을 추가하여 그렇게합니다. 대신, 우리는이 새로운 코드 와이 새로운 행동을 개별적으로 선언 할 수 있습니다.
SAGON4DELPHI는 이러한 교차 절단 문제를 구현하는 데 도움이됩니다.
로컬 사본을 올리거나 실행하려면 다음을 수행하십시오.
이 라이브러리를 사용하려면 Delphi IDE (XE 이상)의 업데이트 된 버전이 필요합니다.
레포를 복제하십시오
git clone https://github.com/ezequieljuliano/Aspect4Delphi.git
IDE 또는 프로젝트의 "검색 경로"를 다음과 같은 디렉토리를 추가하십시오.
Aspect4Delphisrc
측
사용법을 설명하기 위해 응용 프로그램의 로그를 관리하기위한 솔루션을 살펴 보겠습니다.
PointCut 속성을 구현하십시오 (바람직하게는 SpecttRittube에서 상속) :
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 속성 및 해당 측면 클래스를 등록하십시오 (바람직하게는 싱글 톤 인스턴스 에서이 컨텍스트를 유지) :
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