2009-04-08

AOP 2

This is fun :-) I’ve created an aspect like so

type
  BusinessClassAttribute = public class(System.Attribute, ITypeInterfaceDecorator)
  private
    FPackageName: String;
    property PackageName: String read FPackageName;
  protected
  public
    constructor (PackageName: String);
    method HandleInterface(Services: RemObjects.Oxygene.Cirrus.IServices; aType: RemObjects.Oxygene.Cirrus.ITypeDefinition);
  end;


I can now decorator my class like so

type
  [aspect: BusinessClass(’DomainClasses.Package1’)]
  Person = class
  end;

I can ensure this class  exists in the aspect implementation method.

constructor BusinessClassAttribute(PackageName: String);
begin
  FPackageName := PackageName;
end;

method BusinessClassAttribute.HandleInterface(Services: RemObjects.Oxygene.Cirrus.IServices; aType: RemObjects.Oxygene.Cirrus.ITypeDefinition);
var
  PackageType: ITypeReference;
begin
  PackageType := Services.FindType(PackageName);
  if (PackageType = nil) then
    Services.EmitError(’Package class not found: ’ + PackageName);
end;


Now when I compile my code I get the following compiler error:

Error  1  (ASPE) Package class not found: DomainClasses.Package1

Adding a class named Package1 to the source makes the error go away. Okay, maybe I am getting a little too excited about this, I am a nerd :-)

No comments: