This is a book all about writing Delphi code. It's not about the VCL or database access. It's just about how to use the language in the most effective way to write clean, testable, maintainable Delphi code. It covers a variety of powerful Delphi programming features and techniques including Generics, Interfaces, Exception Handling, Anonymous Methods, Collections, RTTI, Enumerators, Attributes, Dependency Injection and Unit Testing.
Foreword
Introduction
Acknowledgements
Frameworks used in Coding in Delphi
Delphi Spring Framework
DUnitX
Delphi Mocks
DSharp
1 Exceptions and Exception Handling
1.1 Introduction
1.2 Structured Exception Handling
1.3 How Not to Use Exceptions
1.4 How to Use Exceptions Properly
1.5 Conclusion
2 Using Interfaces
2.1 Introduction
2.2 Decoupling
2.3 What are Interfaces?
2.4 Interfaces Everywhere
2.5 A Simple Example
2.6 Implementing an Interface
2.7 Some Further Things to Note:
2.8 Interface Inheritance
2.9 Other Things to Think About
2.10 About TInterfacedObject
2.11 How to Actually Use Interfaces
2.12 Why Should You Be Using Interfaces?
3 Understanding Generics
3.1 Generics to the Rescue
3.2 Constraints
3.3 Generic Interfaces
3.4 Generic Methods
3.5 Generic Collections
3.6 Thinking Generically
3.7 A Contrived, Simple Example
3.8 A Practical Example
3.9 Conclusion
3.10 An Interview with Generics
4 Understanding Anonymous Methods
4.1 Introduction
4.2 Definition
4.3 Why?
4.4 Closures
4.5 Standard Declarations
4.6 A Practical Example
4.7 Another, Cooler Example
4.8 Anonymous methods in the RTL
4.9 Predicates
4.10 Conclusion
4.11 An Interview with Anonymous Methods
5 Delphi Collections
5.1 Introduction
5.2 General Notions about Collections
5.3 The Delphi-Provided Collections
5.4 TQueue
5.5 Collections in the Delphi Spring Framework
5.6 Why you should be using the Spring4D Collections
5.7 Conclusion
6 Enumerators in Delphi
6.1 Introduction
6.2 IEnumerator Interface
6.3 Specialized Enumerators
6.4 TEnumerable in Generics.Collections
6.5 Conclusion
7 IEnumerable
7.1 IEnumerable
7.2 Predicates
7.3 An Interview with IEnumerable (IEoT):
8 Run-time Type Information
8.1 Introduction
8.2 TValue
8.3 RTTI on Classes
8.4 Using RTTI to Affect Instances
8.5 General Notes
8.6 RTTI for Non-classes
8.7 Miscellaneous Methods
8.8 RTTI Compiler Directives
8.9 Conclusion
9 Attributes
9.1 Introduction
9.2 What are Attributes
9.3 A Simple Example
9.4 Conclusion
9.5 An Interview with Attributes
10 Using TVirtualInterface
10.1 A Slightly Better TVirtualInterface
10.2 An Actually Useful Example
10.3 ISimpleStub
10.4 TSimpleMock
10.5 Conclusion
11 Introduction to Dependency Injection
11.1 Introduction
11.2 What is a Dependency?
11.3 The Law of Demeter (LoD)
11.4 A Design Example
11.5 Law of Demeter in Delphi
11.6 A Step-by-Step Code Example
11.7 The Dependency Injection Container
11.8 Conclusion
12 A Deeper Look at Dependency Injection
12.1 Optional Dependencies
12.2 Setter Injection
12.3 Method Injection
12.4 The Delphi Spring Container and Service Locator
12.5 Registering via attributes
12.6 ServiceLocator as Anti-pattern
12.7 Dependency Injection Summary
13 Unit Testing
13.1 So What is Unit Testing?
13.2 Why Do Unit Testing?
13.3 Unit Testing in Delphi
13.4 General Rules for Unit Tests
13.5 Test Driven Development
13.6 A Basic Example
14 Testing with an Isolation Framework
14.1 A Quick Review
14.2 Isolation Frameworks for Delphi
14.3 Getting Started
14.4 Conclusion
Appendix A: Resources
Dependency Injection
Unit Testing
Source Control
Projects
General Good Stuff
Appendix B: My Delphi Story
Foreword
Introduction
Acknowledgements
Frameworks used in Coding in Delphi
Delphi Spring Framework
DUnitX
Delphi Mocks
DSharp
1 Exceptions and Exception Handling
1.1 Introduction
1.2 Structured Exception Handling
1.3 How Not to Use Exceptions
1.4 How to Use Exceptions Properly
1.5 Conclusion
2 Using Interfaces
2.1 Introduction
2.2 Decoupling
2.3 What are Interfaces?
2.4 Interfaces Everywhere
2.5 A Simple Example
2.6 Implementing an Interface
2.7 Some Further Things to Note:
2.8 Interface Inheritance
2.9 Other Things to Think About
2.10 About TInterfacedObject
2.11 How to Actually Use Interfaces
2.12 Why Should You Be Using Interfaces?
3 Understanding Generics
3.1 Generics to the Rescue
3.2 Constraints
3.3 Generic Interfaces
3.4 Generic Methods
3.5 Generic Collections
3.6 Thinking Generically
3.7 A Contrived, Simple Example
3.8 A Practical Example
3.9 Conclusion
3.10 An Interview with Generics
4 Understanding Anonymous Methods
4.1 Introduction
4.2 Definition
4.3 Why?
4.4 Closures
4.5 Standard Declarations
4.6 A Practical Example
4.7 Another, Cooler Example
4.8 Anonymous methods in the RTL
4.9 Predicates
4.10 Conclusion
4.11 An Interview with Anonymous Methods
5 Delphi Collections
5.1 Introduction
5.2 General Notions about Collections
5.3 The Delphi-Provided Collections
5.4 TQueue
5.5 Collections in the Delphi Spring Framework
5.6 Why you should be using the Spring4D Collections
5.7 Conclusion
6 Enumerators in Delphi
6.1 Introduction
6.2 IEnumerator Interface
6.3 Specialized Enumerators
6.4 TEnumerable in Generics.Collections
6.5 Conclusion
7 IEnumerable
7.1 IEnumerable
7.2 Predicates
7.3 An Interview with IEnumerable (IEoT):
8 Run-time Type Information
8.1 Introduction
8.2 TValue
8.3 RTTI on Classes
8.4 Using RTTI to Affect Instances
8.5 General Notes
8.6 RTTI for Non-classes
8.7 Miscellaneous Methods
8.8 RTTI Compiler Directives
8.9 Conclusion
9 Attributes
9.1 Introduction
9.2 What are Attributes
9.3 A Simple Example
9.4 Conclusion
9.5 An Interview with Attributes
10 Using TVirtualInterface
10.1 A Slightly Better TVirtualInterface
10.2 An Actually Useful Example
10.3 ISimpleStub
10.4 TSimpleMock
10.5 Conclusion
11 Introduction to Dependency Injection
11.1 Introduction
11.2 What is a Dependency?
11.3 The Law of Demeter (LoD)
11.4 A Design Example
11.5 Law of Demeter in Delphi
11.6 A Step-by-Step Code Example
11.7 The Dependency Injection Container
11.8 Conclusion
12 A Deeper Look at Dependency Injection
12.1 Optional Dependencies
12.2 Setter Injection
12.3 Method Injection
12.4 The Delphi Spring Container and Service Locator
12.5 Registering via attributes
12.6 ServiceLocator as Anti-pattern
12.7 Dependency Injection Summary
13 Unit Testing
13.1 So What is Unit Testing?
13.2 Why Do Unit Testing?
13.3 Unit Testing in Delphi
13.4 General Rules for Unit Tests
13.5 Test Driven Development
13.6 A Basic Example
14 Testing with an Isolation Framework
14.1 A Quick Review
14.2 Isolation Frameworks for Delphi
14.3 Getting Started
14.4 Conclusion
Appendix A: Resources
Dependency Injection
Unit Testing
Source Control
Projects
General Good Stuff
Appendix B: My Delphi Story