From e106e5f8ec3735c5f89ff51a860e6ebf93ea180b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 11 Dec 2015 19:10:34 +0100 Subject: [PATCH] DESIGN.md --- DESIGN.md | 50 -------------------------------------------------- 1 file changed, 50 deletions(-) diff --git a/DESIGN.md b/DESIGN.md index 4605f05f2..5b0ee3598 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -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: