Attempting to use Dependency injection

Hi,

I have a class called Box that I use in production.

The initializer takes in no arguments.

However for some SwiftUI previews / test cases I would like that class to be initialized with some values.

I don't want that initializer (with values) to be available in production, it would be only used in SwiftUI previews and test cases.

My attempt:

I used #if DEBUG it solves my problem with minimal code change but not sure if I was missing a better way.

Given below is a simplified example.

@Observable
class Box {
    private(set) var chocolates = [String]()
    
    @ObservationIgnored
    private var isProcessed = false

    #if DEBUG
    init(chocolates: [String]) {
        self.chocolates = chocolates
    }
    #endif
    
    // contains more complex logic using chocolates
}

Questions

  1. Is the above a reasonable approach using #if DEBUG?
  2. Or is there a more elegant way to do it?

Problems I faced

  1. Trying to use a convenience initializer but that exposed a designated initializer to production defeating the purpose
  2. This code contains some macros so was a bit concerned using it in protocols

Thanks

Can you say a bit more about the kind of logic you want using chocolates? Where do they come from and how are they transformed? Because from the example per se, it's not really clear to me if Dependency Injection (DI) is what is needed.

The standard DI pattern would be: initiliazing Box takes a type that conforms to a ChocolateStorage protocol (or something). And then you construct one like:

// production
let box = Box(chocolateStorage: RealChocolateStorage())

// preview
let box = Box(chocolateStorage: MockupChocolateStorage())

But if this really helps, depends a lot on what kind of behavior you want in the // contains more complex logic using chocolates and/or where the chocolates come from.

Honestly, I'd not take the init(initialChocolates: [String] = [] off the table immediately unless the complex logic has real risks with a setup like that.

2 Likes

Thanks a lot @maartene

Chocolates represent available purchases that would be fetched from an API. May be dependency injection is not the best approach.

Your approach is a good one, the only place where I got stuck with your solution was passing a mock storage where chocolates would need to mutable inside the box but shouldn't be modified outside the box.

In order to make mutable I couldn't avoid making RealChocolateStorage expose (internal) chocolates which could be modified from the outside.

I don't want to introduce a loop hole in production where accidentally I leave an option to modify the chocolates outside the Box.

If chocolates are a value type (like String in your example), the private(set) already makes them impossible to mutate outside of the Box. Even if you pass them in the init. For example:

var chocolates = ["Pure", "Milk", "White"]
let box = Box(chocolates: chocolates)

// mutate the chocolates array
chocolates.append("Caramel-Seasalt")

// Box of chocolates is not changed, because it has its own chocolate storage
print(box.chocolates) // ["Pure", "Milk", "White"]

// Direct modification of the chocolates in the box are not allowed
box.chocolates.append("Pecan") // ❌ does not compile

Would that cover your needs?

Thanks @maartene, yes chocolates is a value type.

May be I will explain where I am stuck.

Based on your inputs I have built something, see the code below:

Problem

  • RealChocolateStorage.chocolate is modifiable and can be modified outside of the Box
  • I don't want RealChocolateStorage.chocolate to be modifiable outside the Box class.

Code

protocol ChocolateStorage: AnyObject {
    // would need to be settable so that it can be used inside the box
    var chocolates: [String] { get set }
}

class RealChocolateStorage: ChocolateStorage {
    var chocolates: [String] = []
}

class MockupChocolateStorage: ChocolateStorage {
    var chocolates: [String]
    
    init(chocolates: [String]) {
        self.chocolates = chocolates
    }
}

@Observable
class Box {
    private(set) var chocolateStorage: ChocolateStorage
    
    @ObservationIgnored
    private var isProcessed = false
    
    init(chocolateStorage: ChocolateStorage) {
        self.chocolateStorage = chocolateStorage
    }
    
    func fetchCholates() {
        chocolateStorage.chocolates = ["aaa", "bbb"]
    }
}

// production
let realStorage = RealChocolateStorage()
// I am still able to modify here,
// so potentially in production it could get modified
// outside the box
realStorage.chocolates = ["A1", "A2"]
let box1 = Box(chocolateStorage: realStorage)

// preview
let box2 = Box(chocolateStorage: MockupChocolateStorage(chocolates: ["test1", "test2"]))

ah ok. my point would be: do you still need the ChocolateStorage protocol?

Or is this enough?

@Observable
class Box {
    private(set) var chocolates: [String]
    
    @ObservationIgnored
    private var isProcessed = false
    
    init(initialChocolates: [String] = []) {
        self.chocolates = initialChocolates
    }
    
    func fetchChocolates() async {
        // API call here
    }
}

// production
let box1 = Box()
await box1.fetchChocolates() // retrieve ["aaa", "bbb"] from API

// preview
let box2 = Box(initialChocolates: ["test1", "test2"])

Note: if you want to practice DI, then you are on the right track. The trick would be to make the chocolateStorage.chocolates array immutable from the outside and provide operations to interact with it:

protocol ChocolateStorage: AnyObject {
    var chocolates: [String] { get }
    func store(_ chocolate: String) 
    func remove(_ chocolate: String)
}
1 Like

Thanks a lot @maartene

Yeah your last solution of initializing with chocolates is what I have settled for.

And I have just wrapped it in a #if DEBUG to protect it from being used in production.

Even if we add store and remove functions still would be exposed as anyone conforming could still modify it outside box.

Not sure if I am overthinking it :)

Thanks a lot @maartene, your guidance helped me refactor my code to extract the common bits into a separately.

That way as you had suggested I could pass it as a mock / real object and works beautifully, thanks to you!!

1 Like

You’re welcome!

I find protocols work for this type of thing.

protocol Box {
  var chocolates : [String] { get }
  // other stuff 
}

@Observable
class RealBox : Box {
  private(set) var chocolates = [String]()
  // other stuff
}

// this goes in Swift file in Development Assets folder to be available only to debug builds
@Observable
class PreviewBox : Box {
  // no need for a private setter. Allow direct access to set preview data
  var chocolates = [String]()
  // other stuff
}

let box1 : Box = RealBox()

let previewBox = PreviewBox()
previewBox.chocolates = [“red”, “blue”]
let box2 : Box = previewBox 
1 Like

Thanks @SwiftDeveloper

This is similar to what I had done, except for me the Box was the same and the box accepted a protocol.

1 Like

FYI in class, async funcs need to be static and return a result otherwise they will corrupt the properties if called multiple times because of re-entrance. Better to put them in a value type instead and then set the result on your object.

For SwiftUI loading of preview data look into PreviewModifier.

This is no longer true with nonisolated(nonsending) (the default of new SwiftPM packages and Xcode projects, and someday the default of Swift). Non-sendable classes with async functions are perfectly fine, and Swift will alert you of any problems at compile time as long as you are compiling in Swift 6 mode.

2 Likes

Will it alert about the re-entrancy problem I was referring to?

You will have to sketch some code that demonstrates the reentrancy problem you are referring to, but reentrancy alone is not disallowed in Swift. If reentrancy leads to mere race conditions, then that is totally fine and will not be diagnosed. If reentrancy somehow leads to data races, then that is the kind of thing the compiler will/should catch.

All nonisolated(nonsending) does is allow you to invoke async methods without sending the object off to the global executor, which of course is never OK for a non-Sendable type.

Yes, race conditions. That’s why the pattern of moving async work to a value type and writing the result once is safer—it makes re-entrancy impossible by design. There’s nothing to interleave because the value type does the suspension, and the instance method writes the finished result exactly once.

But value types with async functions cannot do everything classes with async functions can do, and so it really depends on your needs. For example, if you need something like isLoading = true; defer { isLoading = false } wrapping your async work, then value types are not capable of doing that.

You really just need to use the correct tool for the job, and many times classes are the correct tool. But I do want to stress that there is no "property corruption" in classes with async methods, as mentioned previously, because that sounds like there are data races. And there are not.

You’re right, and I appreciate the clarification on data races. The pattern I was describing—moving async work to a value type—does trade off some capabilities. If you need isLoading = true; defer { isLoading = false } wrapping the work, a class is the right tool.

But the key is that the function setting isLoading needs to be synchronous and create its own Task. That way the suspension point is inside the task, not inside the method itself, so there’s no re-entrancy window where a second call can interleave writes to isLoading. The method returns immediately after starting the task, and observers see the state changes through @Observable.

E.g. in a store class load() is sync, it sets isLoading = true, creates a Task that awaits the fetcher, and clears isLoading inside the task when done. A second call while the task is running hits the guard loadTask == nil else { return } and does nothing. No interleaving, no logical bugs, and no data races.