Software written in such way might look unconventional, and has a bit of a learning curve. The talk is to show that the advantages it brings make it a viable choice.
33. Standard Approach
class DatabaseService {
func insertObjects(objects: [DBObject], updatedAfter date: NSDate) {
// 1. Filter objects array based on criteria
// 2. For each remainig object
// 2.1 Insert object in DB
}
}
34. Split Approach: Decision
struct DBAction {
enum Mode {
case Insert
case Update
case Delete
}
let objects: [DBObject]
let mode: Mode
}
func persistObjectsAction(objects: [DBObject], updatedAfter updatedDate: NSDate) ->
// 1. Filter object based on date
// 2. Create action value using objects and insert mode
}
45. Functional Core
struct Stuff {
let id: String
let text: String
let number: Int
}
extension Stuff {
init?(json: [String: AnyObject]) { /* ... */ }
}
extension Stuff {
init(realmObject: RealmStuff) { /* ... */ }
}
46. Functional Core
describe("Stuff from JSON dictionary") {
context("when the dictionary contains all the valid keys") {
it("it returns an instance configured with the values in the dictionary") {
let anyId = "any id"
let anyText = "any text"
let anyNumber = 42
let dict: [String: AnyObject] = ["id": anyId, "text": anyText, "number": anyNum
let stuff = Stuff(json: dict)
expect(stuff?.id) == anyId
expect(stuff?.text) == anyText
expect(stuff?.number) == anyNumber
}
}
}
describe("Stuff model from Realm object") {
it("sets its properties based on the Realm object one") {
let realmObject = RealmStuff.test_fixture()
let sut = Stuff(realmObject: realmObject)
expect(sut.id) == realmObject.id
expect(sut.number) == realmObject.number
expect(sut.text) == realmObject.text
}
}
47. Functional Core
extension CellViewModel {
init(stuff: Stuff) {
self.text = "(stuff.id) - (stuff.text) ((stuff.number))"
}
}
describe("CellViewModel") {
context("when initialized with a Stuff model") {
it("sets the text using the stuff properties") {
let stuff = Stuff(id: "123", text: "any text", number: 42)
let sut = CellViewModel(stuff: stuff)
expect(sut.text) == "123 - any text (42)"
}
}
}
48. Side Effect Code
class ViewController: UIViewController {
enum Effect {
case UpdateView(viewModels: [CellViewModel])
case PresentAlert(error: ErrorType)
}
func performEffect(effect: Effect) {
switch effect {
case .UpdateView(let viewModels):
self.viewModels = viewModels
tableView.reloadData()
case .PresentAlert(let error):
presentErrorAlert(error)
}
}
}