Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a single dependency resolver, and everytime you build, it adds t… #1

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ jobs:
# Set timeout to prevent overcharging
timeout-minutes: 20
steps:
- uses: maxim-lobanov/setup-xcode@v1.2.3
- uses: maxim-lobanov/setup-xcode@v1
with:
xcode-version: 12.4
xcode-version: latest
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
- name: Test
run: swift test --parallel
18 changes: 11 additions & 7 deletions Sources/Biodag/Biodag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,40 @@ import Foundation

/// A dependency collection that provides resolutions for object instances.
final public class DependencyResolver {
private let moduleArray: [Module]
/// Stored object instance factories.
private var modules = [String: Module]()
private var instances = [String: Any]()

/// Construct dependency resolutions.
public init(@ModuleBuilder _ modules: () -> [Module]) {
modules().forEach { add(module: $0) }
self.moduleArray = modules()
}

/// Construct dependency resolution.
public init(@ModuleBuilder _ module: () -> Module) {
add(module: module())
self.moduleArray = [module()]
}

/// Assigns the current container to the composition root.
public func build() {
Self.root = self
for module in moduleArray {
Self.root.add(module: module)
}
}

fileprivate init() {}
deinit { modules.removeAll() }
fileprivate init() {
self.moduleArray = []
}
}

private extension DependencyResolver {
/// Composition root container of dependencies.
static var root = DependencyResolver()
static let root = DependencyResolver()

/// Registers a specific type and its instantiating factory.
func add(module: Module) {
modules[module.name] = module
self.modules[module.name] = module
}

/// Resolves through inference and returns an instance of the given type from the current default container.
Expand Down
18 changes: 18 additions & 0 deletions Tests/BiodagTests/BiodagTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ extension DependencyTests {
// The singletons are the same instance
XCTAssertTrue(singletonModule === helperSingleton)
}

func testAddingDependencies() {
struct Added { }
struct NotAdded { }

let newResolver = DependencyResolver({
Module { Added() as Added }
})
newResolver.build()


@Inject var widgetModule: WidgetModuleType
@Inject var added: Added
@Inject var notAdded: NotAdded

XCTAssertNotNil(widgetModule)
XCTAssertNotNil(added)
}
}

// MARK: - Subtypes
Expand Down
Loading