DESIGN.md

This commit is contained in:
Gwendal Roué committed 2015-12-11 19:10:34 +01:00
1 parent 5027504d73
commit e106e5f8ec
1 file changed
-50
-50
View File
@@ -4,56 +4,6 @@ The Design of GRDB.swift
More than caveats or defects, there are a few glitches, or surprises in the GRDB.swift API. We try to explain them here. The interested readers can take them as Swift challenges!
### Why is Record a class, when protocols are all the rage?
Easy: Swift protocols don't provide `super`, and `super` is what turns Record into the flexible base class you need when you implement your specific needs:
```swift
// A Record that makes sure its UUID is set before insertion:
class A : Record {
var UUID: NSString?
override func insert(db: Database) throws {
if UUID == nil {
UUID = NSUUID().UUIDString
}
try super.insert()
}
}
// A Record that validates itself before saving:
class B : Record {
override func insert(db: Database) throws {
try validate()
try super.insert(db)
}
override func update(db: Database) throws {
try validate()
try super.update(db)
}
func validate() throws {
...
}
}
// A Record that deletes an external resource after being deleted from
// the database:
class C : Record {
public func delete(db: Database) throws -> DeletionResult {
switch try super.delete(db) {
case .RowDeleted:
try NSFileManager.defaultManager().removeItemAtPath(...)
case .NoRowDeleted:
break
}
}
}
```
A second reason: the `databaseEdited` flag, which is true when a Record has unsaved changes, is easily provided by the base class Record. Record can manage its internal state *accross method calls*. Protocols could not provide this service without extra complexity.
Yet, don't miss the [RowConvertible](http://cocoadocs.org/docsets/GRDB.swift/0.12.0/Protocols/RowConvertible.html) and [DatabaseMapping](http://cocoadocs.org/docsets/GRDB.swift/0.12.0/Protocols/DatabaseTableMapping.html) protocols: they provide fetching from custom SQL queries and fetching by primary key for free.
### Why must we provide query arguments in an Array, when Swift provides variadic method parameters?
I admit that the array argument below looks odd: