Skip to content

Commit ff0a2d1

Browse files
committed
feat: Graph Instance
1 parent e6adf37 commit ff0a2d1

20 files changed

+484
-297
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ A lightweight and flexible dependency injection container for Swift.
66
[![License](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)
77

88
## Overview
9-
Astroject is designed to simplify dependency management in your Swift projects. It provides a clean and intuitive API for registering and resolving dependencies, supporting both synchronous and asynchronous factories, various instance scopes (singleton, prototype, weak), and extensible behaviors. Many ideas came from a Sister Library [Swinject](https://github.com/Swinject/Swinject)
9+
Astroject is designed to simplify dependency management in your Swift projects. It provides a clean and intuitive API for registering and resolving dependencies, supporting both synchronous and asynchronous factories, various instance scopes (singleton, transient, weak), and extensible behaviors. Many ideas came from a Sister Library [Swinject](https://github.com/Swinject/Swinject)
1010

1111
## API Documentation
1212
Coming Soon...
1313
[DocC]()
1414

1515
## Features
1616
- **Synchronous and Asynchronous Registrations:** Register dependencies with both synchronous and asynchronous factory closures.
17-
- **Instance Scopes:** Supports singleton, prototype, and weak instance scopes.
17+
- **Instance Scopes:** Supports singleton, transient, and weak instance scopes.
1818
- **Named Registrations:** Register dependencies with optional names for disambiguation.
1919
- **Circular Dependency Detection:** Prevents and reports circular dependency issues.
2020
- **Extensible Behaviors:** Add custom behaviors to the container's registration process.
@@ -173,17 +173,17 @@ class MyClass {
173173

174174
let container = Container()
175175
```
176-
- Prototype(default) - A new instance is generated through every resolve for that object.
176+
- Transient(default) - A new instance is generated through every resolve for that object.
177177
```swift
178-
try container.register(MyClass.self, name: "prototype") { _ in
178+
try container.register(MyClass.self, name: "transient") { _ in
179179
MyClass()
180180
}
181181

182182
// Output: MyClass initialized
183-
let prototypeInstance1: MyClass = try container.resolve(MyClass.self, name: "prototype")
183+
let transientInstance1: MyClass = try container.resolve(MyClass.self, name: "transient")
184184

185185
// Output: MyClass initialized (new instance)
186-
let prototypeInstance2: MyClass = try container.resolve(MyClass.self, name: "prototype")
186+
let transientInstance2: MyClass = try container.resolve(MyClass.self, name: "transient")
187187
```
188188
- Singleton - Instance maintained and kep throughout the life time of the container.
189189
```swift

Sources/AstrojectCore/AstrojectError.swift

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ public enum AstrojectError: LocalizedError {
1313
/// This case indicates that a dependency registration with the same type and
1414
/// optional name has already been performed.
1515
case alreadyRegistered(type: String, name: String? = nil)
16-
16+
1717
/// A dependency is requested but no corresponding registration is found in the container.
1818
///
1919
/// This case occurs when attempting to resolve a dependency that has not been registered.
2020
case noRegistrationFound
21-
21+
2222
/// A circular dependency is detected during the resolution process.
2323
///
2424
/// This case indicates that a chain of dependencies forms a loop, preventing successful resolution.
2525
case circularDependencyDetected(type: String, name: String? = nil)
26-
26+
2727
/// An error occurred within the factory closure during dependency resolution.
2828
///
2929
/// This case wraps an underlying error that occurred while executing the factory
@@ -37,7 +37,7 @@ public enum AstrojectError: LocalizedError {
3737
/// Instance implementation is incorrect, or if there is a mismatch
3838
/// between the registered type and the actual type of the resolved instance.
3939
case invalidInstance
40-
40+
4141
/// Provides a user-friendly description of the error.
4242
public var errorDescription: String? {
4343
switch self {
@@ -61,7 +61,7 @@ public enum AstrojectError: LocalizedError {
6161
return "The resolved instance is invalid or of an unexpected type."
6262
}
6363
}
64-
64+
6565
/// Provides a reason for the error, explaining why it occurred.
6666
public var failureReason: String? {
6767
switch self {
@@ -77,7 +77,7 @@ public enum AstrojectError: LocalizedError {
7777
return "The resolved instance did not match the expected type or was invalid."
7878
}
7979
}
80-
80+
8181
/// Provides a suggestion for recovering from the error.
8282
public var recoverySuggestion: String? {
8383
switch self {
@@ -91,6 +91,7 @@ public enum AstrojectError: LocalizedError {
9191
case .underlyingError:
9292
return "Check the factory closure for errors and ensure that it's correctly implemented."
9393
case .invalidInstance:
94+
// swiftlint:disable:next line_length
9495
return "Ensure that the Instance implementation is correct and that the registered type matches the actual type of the resolved instance."
9596
}
9697
}
@@ -108,12 +109,12 @@ extension AstrojectError: Equatable {
108109
public static func == (lhs: AstrojectError, rhs: AstrojectError) -> Bool {
109110
switch (lhs, rhs) {
110111
case (.alreadyRegistered(let lhsType, let lhsName), .alreadyRegistered(let rhsType, let rhsName)),
111-
(.circularDependencyDetected(let lhsType, let lhsName),
112-
.circularDependencyDetected(let rhsType, let rhsName)):
112+
(.circularDependencyDetected(let lhsType, let lhsName),
113+
.circularDependencyDetected(let rhsType, let rhsName)):
113114
// Compare the associated types and names for alreadyRegistered and circularDependencyDetected errors.
114115
return lhsType == rhsType && lhsName == rhsName
115116
case (.noRegistrationFound, .noRegistrationFound),
116-
(.invalidInstance, .invalidInstance):
117+
(.invalidInstance, .invalidInstance):
117118
// noRegistrationFound and invalidInstance errors are equal if they are the same case.
118119
return true
119120
case (.underlyingError(let lhsError), .underlyingError(let rhsError)):

0 commit comments

Comments
 (0)