ATTRIBUTES AND RTTI CODE FASTER!. ATTRIBUTES “Attributes are a language feature in Delphi that allows annotating types and type members with special objects.

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

Written by: Dr. JJ Shepherd
Using attributes and RTTI to automate programming tasks Primož Gabrijelčič thedelphigeek.com Primož Gabrijelčič thedelphigeek.com.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Road Map Introduction to object oriented programming. Classes
1 CS1001 Lecture Overview Homework 3 Homework 3 Project/Paper Project/Paper Object Oriented Design Object Oriented Design.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
C++ data types. Structs vs. Classes C++ Classes.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
1 CS1001 Lecture Overview Object Oriented Design Object Oriented Design.
Chapter 13: Object-Oriented Programming
Ranga Rodrigo. Class is central to object oriented programming.
Introduction to Object-oriented Programming CSIS 3701: Advanced Object Oriented Programming.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Porting Implementation of Packet Utilization Standard from ADA to JAVA Annelie Hultman (TEC-EME) Donata Pedrazzani (TEC-EMS) ESA/ESTEC 2004 JPUS de-briefing.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Chapter 9 Object-Oriented Software Development F Software Development Process F Analyze Relationships Among Objects F Class Development F Class Design.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
Working With Objects Tonga Institute of Higher Education.
Agenda Object Oriented Programming Reading: Chapter 14.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Working With Objects Tonga Institute of Higher Education.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
GoodOO Programming Practice in Java © Allan C. Milne v
JAVA BEANS JSP - Standard Tag Library (JSTL) JAVA Enterprise Edition.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
.NET MCQs MULTIPLE CHOICE QUESTION
Classes (Part 1) Lecture 3
Object Oriented Programming
More About Objects and Methods
Microsoft Visual Basic 2005: Reloaded Second Edition
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
Road Map Introduction to object oriented programming. Classes
Creating Your OwnClasses
Indexer AKEEL AHMED.
group work #hifiTeam
Classes Variables That Are Not of a Built-in Type Are Objects
Java Programming Course
Tonga Institute of Higher Education
CIS16 Application Development and Programming using Visual Basic.net
How to organize and document your classes
CIS 199 Final Review.
NAME 436.
Data Structures and ADTs
C++ data types.
Creating and Using Classes
CS 240 – Advanced Programming Concepts
Presentation transcript:

ATTRIBUTES AND RTTI CODE FASTER!

ATTRIBUTES “Attributes are a language feature in Delphi that allows annotating types and type members with special objects that carry additional information. This information can be queried at run time. Attributes extend the normal Object-Oriented model with Aspect- Oriented elements.” - Delphi documentation

ATTRIBUTES Code annotation Programmer annotates the code Run time querying Program reads those annotations and reacts accordingly

EXAMPLE type TMyClass = class [MyAttribute] FField: string; end;

IMPLEMENTATION Attribute = class Descending from TCustomAttribute Traditionally not starting with a T and ending in an Attribute type MyAttribute = class(TCustomAttribute) end;

SIMPLIFICATION Attribute part of the name can be removed in the annotation Standard practice type TMyClass = class [MyAttribute] FField1: string; [My] FField2: integer; end;

RTTI ctx := TRttiContext.Create; try t := ctx.GetType(aClass); for f in t.GetFields do for a in f.GetAttributes do … finally ctx.Free; end; Attributes exist only when observed!

USEFUL HELPERS RTTIUtils Robert Love DSharp.Core.Reflection Stefan Glienke

PARAMETERS Attributes can accept parameters type TMyClass = class [StoreAs('ExternalField')] FField: string; end;

CONSTRUCTOR Parameters are passed to an appropriate constructor type StoreAsAttribute = class(TCustomAttribute) public constructor Create(externalName: string); end;

OVERLOADED ATTRIBUTES Attribute can accept different parameter types or variable number of parameters type TMyClass = class [StoreAs('ExternalField1')] FField1: string; [StoreAs('ExternalField2', true)] FField2: string; end;

OVERLOADED CONSTRUCTORS type StoreAsAttribute = class(TCustomAttribute) public constructor Create( externalName: string); overload; constructor Create(externalName: string; storeAsAttribute: boolean); overload; end;

DEFAULT VALUES type StoreAsAttribute = class(TCustomAttribute) public constructor Create(externalName: string; storeAsAttribute: boolean = false); end;

ADVANCED ATTRIBUTES Multiple attributes are allowed on one element [Serialize] [RootNode('Options')] TewOptions = class [Serialize] [RootNode('Optons')] TewOptions = class [Serialize, RootNode('Options')] TewOptions = class

ATTRIBUTABLE ENTITIES Attributes can be applied to classes, records fields, properties methods, method parameters non-local enumeration declaration, variable declarations Even to entities that are not accessible with RTTI!

PROBLEMS Not working on generic classes W1025 Unsupported language feature: 'custom attribute‘ Meaning can be unclear [GpManaged(true)] [GpManaged(true)] FList: TObjectList;

PRACTICAL EXAMPLES

AUTOMATICALLY CREATED FIELDS type TewOptions = class(TGpManaged) strict private FActiveLanguage: string; [GpManaged] [GpManaged] FEditor : TewOpt_Editor; [GpManaged] [GpManaged] FHotkeys: TewOpt_Hotkeys; public property ActiveLanguage: string read FActiveLanguage write FActiveLanguage; property Editor: TewOpt_Editor read FEditor; property Hotkeys: TewOpt_Hotkeys read FHotkeys; end;

OBJECT SERIALIZATION - XML type [XmlRoot('Person')] TPerson = class(TObject) … public [XmlAttribute('First_Name')] property FirstName: String read FFirstName write FFirstName; [XmlElement('LAST_NAME')] property LastName: String read FLastName write FLastName; [XmlIgnore] property MiddleName: String read FMiddleName write FMiddleName; end;

OBJECT SERIALIZATION - INI type TConfigSettings = class(TObject) … public [IniValue('Database','ConnectString','')] property ConnectString: String read FConnString write FConnString; [IniValue('Logging','Level','0')] property LogLevel: Integer read FLogLevel write FLogLevel; [IniValue('Logging','Directory','')] property LogDirectory: String read FLogDir write FLogDir; end;

UNIT TESTING - DUNITX type TMyExampleTests = class public [Test] [TestCase('Case 1','1,2')] [TestCase('Case 2','3,4')] [TestCase('Case 3','5,6')] procedure TestOne(param1: integer; param2: integer); [Test] procedure TestTwo;

ORM MAPPING type [TAttrDBTable('NONE')] TReportItem = class(TObject) protected [TAttrDBField(PP_VEHICLE_FIELD)] FVeiculoId: integer; [TAttrDBField(PP_DRIVER_FIELD)] FMotoristaId: integer; end;

DATA VALIDATION type TMyConfig = class [MustNotEmptyString] [FolderMustExists] property WorkingDir: string; [MinValue(1)] [MaxValue(7)] property DefaultDay: Integer; [MustNotEmptyString][LimitToTheseChars('xyz')] property DefaultCity: string; end;

COMMAND-LINE PARSING type TCommandLine = class public [CLPDescription('Set precision'), CLPDefault('3.14')] property Precision: string read FPrecision write FPrecision; [CLPPosition(1), CLPDescription('Input file'), CLPLongName('input_file'), CLPRequired] property InputFile: string read FInputFile write FInputFile;

REST SERVER [Authorize('User', TNCWAAuthorizer)] [ExtraHeader('Access-Control-Allow-Headers', 'sessionId')] TFilesController= class(TRestPublicController) function Put(const fileName, [FromBody]content: string): TRestResponse; function Save(const fileName, [FromBody]content: string): TRestResponse;

CONCLUSION

PROS & CONS + Big code reduction + Self-describing code - Meaning sometimes unclear - RTTI can get complicated

LINKS Overview GpAutoCreate Serialization DUnitX ORM mapping Validation

QUESTIONS?