Transcript TURBOMan

TURBOMan
The Ultimate Relational
Business Object
Manager
3 Layer Application Development
• User Interface Layer
• Business Rules Layer
• Data Access Layer
Logical separation of the layers is best to
promote code re-use and unit testing.
The Delphi Way
• UI – Forms
• Data Access Layer – TDataSet,
TDataSource
• Business Layer? Roll your own…
– DataSet events (OnBeforePost)
– Explicit fields created
– OnCalcFields
– DataModule
Git-R-Dun
• Typically business rules are embodied in
methods of the form class.
• Problem: the form is part of the UI layer! This
violates the three-tiered approach and does not
promote reusability or consistent application of
business logic. DataModules become large and
don’t allow simultaneous usage.
• Result: data consistency bugs, hard to detect
and fix.
A Better Approach
• Construct a class that encapsulates the
data associated and knows how to
perform the necessary related business
functions, such as calculations and
validation.
• The functions would be defined as
methods of the class.
TURBOMan…what is it?
• A framework for building classes that
encapsulate data and business rules
without sacrificing Delphi’s powerful dataaware controls!
• Implemented as Delphi components that
are easily reused in separate applications.
Building blocks of the business
component
• TField
– One of the most underrated and poorly
understood features in Delphi’s database
architecture
– A very powerful component, created implicitly
when a dataset is opened (or explicitly when
the developer uses the Fields Editor)
Implicitly created fields
• Can only be accessed at runtime using the
Fields array or the FieldByName method
– Employee.Fields[4].AsDateTime := Now;
– Employee.FieldByName(‘HireDate’).AsDateTime := Now;
• Both of these methods return a reference to a TField and
are not one of the descendant classes, so the
AsDateTime conversion property must be used.
Explicitly created fields
• Created using the design time Fields editor by
connecting to the database and identifying field
attributes.
• Can be accessed directly without having to
reference them through a dataset.
– EmployeeHireDate.Value := Now;
• Design time access, more efficient, concise data
type
• Components with properties and events that can
be modified at design time and runtime.
TField details
TField
TBlobField
TGraphicField
TMemoField
TBooleanField
TNumericField
TIntegerField
TSmallIntField
TWordField
TFloatField
TCurrencyField
TDateTimeField
TStringField
TGUIDField
TField Properties
•
•
•
•
•
•
•
•
•
•
•
•
DisplayLabel
DisplayWidth
Index
Visible
DisplayFormat
Currency
DefaultExpression
EditFormat
MinValue
MaxValue
ReadOnly
Required
TField Kinds
• fkData
– Represents a physical field in a table
• fkCalculated
– Calculated in the OnCalcFields event
• fkLookup
– Value is retrieved from another table
Business Component basic
principles
• Fields - maintains a set of field object
references that are linked to a dataset that
are automatically explicitly created and
configured to meet the business rule set
for the data being managed.
• Validation - probably the most important
utilization of the business rules.
• Relationships – detail relationships with
other tables
Creating a Business Component
• SQL Explorer – Create Business Object
– Class Name
– Fields included
– Calculated and Lookup fields
– Save – unit name
– Results – output of PAS unit and INC files
SQL Explorer
Create Business Object
Generated Source
Understanding the Class
• Inherited from TRSBusinessComponent
• Field objects are stored as private field members
of the class
• Each field object supports a read only property
that provides access to the underlying table data
• Special get/set properties to provide easy
access to the field data.
• Include files for easy updating
• SetupDataSet method
• SetupFields – automatically created
• CalcFields method – example code stubbed in
• Validate method – example code commented
Define Business Rules & Methods
• Enhancing the SetupFields method with
custom field properties
• Completing the CalcFields method
• Building the Validate method
– understanding the Error Handler
• Enhancing the class with additional
methods
• Building a relationship with another class
• Registration
Demonstration
• Business Component dot notation for
fields
• Dot notation for relationships
– Customer.ServiceRO.Detail.CloseDate
• Code re-use