### Import Macaroni and define protocol and implementation Source: https://github.com/bealex/macaroni/blob/main/README.md Imports the Macaroni library and declares a protocol `MyService` with a conforming class `MyServiceImplementation`. This is the initial setup before configuring the container. ```swift import Macaroni protocol MyService {} class MyServiceImplementation: MyService {} ``` -------------------------------- ### 30-second tutorial: Container setup, registration, resolution, and @Injected usage Source: https://github.com/bealex/macaroni/blob/main/README.md Shows the basic Macaroni workflow: create a container, set it as a singleton lookup policy, register a service implementation, resolve it manually, and use @Injected in a class. Note that @Injected can only be used in reference types because properties are lazily initialized. ```swift // Create the container. let container = Container() // Set it as a singleton for the simplest service-locator style resolution. Container.lookupPolicy = .singleton(container) // Add service implementations into the container. let myService = MyServiceImplementation() container.register { () -> MyService in myService } // Use it in code. let myService: MyService = container.resolve() // Or use it with property wrapper. class MyClass { @Injected var service: MyService } ``` -------------------------------- ### Per-module container setup with Container.lookupPolicy Source: https://github.com/bealex/macaroni/blob/main/README.md Configure Macaroni to use a per-module container by setting the lookup policy to EnclosingTypeContainer and conforming to ModuleDI. Each module defines its own container and conforms to the protocol to enable injection from that container. ```swift protocol ModuleDI: Containerable {} Container.lookupPolicy = EnclosingTypeContainer() ``` ```swift private var moduleContainer: Container! extension ModuleDI { var container: Container! { moduleContainer } // now each module does have its own container } class MyClass: ModuleDI { @Inject var service: MyService // will be injected from the `moduleContainer` } ``` -------------------------------- ### Set up container with singleton lookup policy Source: https://github.com/bealex/macaroni/blob/main/README.md Creates a `Container` instance and sets `Container.lookupPolicy` to `.singleton(container)`, using a service locator pattern. This tells Macaroni where to find objects for injection. ```swift let container = Container() Container.lookupPolicy = .singleton(container) ``` -------------------------------- ### Function Parameter Injection with @Injected Source: https://github.com/bealex/macaroni/blob/main/README.md Demonstrates using @Injected as a property wrapper for function parameters (available from Swift 5.5). Shows the function declaration and two ways to call it: using the default instance or an alternative instance. ```swift func foo(@Injected service: MyService) { /* Use service here */ } ``` ```swift foo($service: container.resolved()) ``` ```swift foo($service: container.resolved(alternative: .another)) ``` -------------------------------- ### Resolving Several Objects with the Same Type Source: https://github.com/bealex/macaroni/blob/main/README.md Shows how to define an alternative registration identifier, register a service with that alternative, and inject it using the @Injected(alternative:) parameter. The alternative identifier must be a string and must be unique for different types. ```swift // - create alternative identifier. Strings must be different for different types. extension RegistrationAlternative { static let another: RegistrationAlternative = "another" } // - registration container.register(alternative: .another) { () -> MyService in anotherInstance } // - injection @Injected(alternative: .another) var myServiceAlternative: MyService ``` -------------------------------- ### Custom MacaroniLogger implementation Source: https://github.com/bealex/macaroni/blob/main/README.md Implement MacaroniLogger to customize or disable logging. The log method handles logging events, and die() is called on fatal errors. Assign an instance to Macaroni.logger to activate it. ```swift class MyMacaroniLogger: MacaroniLogger { func log(/* Parameters */) { /* Logging code */ } func die() -> Never { /* Log and crash */ } } Macaroni.logger = MyMacaroniLogger() ``` -------------------------------- ### Lazy initialization wrapper and registration Source: https://github.com/bealex/macaroni/blob/main/README.md Defines a `LazilyInitialized` wrapper that defers object creation until first access, and registers a resolver that returns the lazy value. The implementation is created only when the injected property is first accessed. ```swift class LazilyInitialized { lazy var value: Type = { resolver() }() private let resolver: () -> Type init(resolver: @escaping () -> Type) { self.resolver = resolver } } let willBeInstantiatedOnFirstAccess = LazilyInitialized { MyServiceImplementation() } container.register { () -> MyService in willBeInstantiatedOnFirstAccess.value } ``` -------------------------------- ### Register resolver with protocol type using two options Source: https://github.com/bealex/macaroni/blob/main/README.md Shows two ways to register a resolver that returns a protocol type `MyService` instead of the concrete implementation. The first uses a type annotation on the variable; the second uses an explicit closure return type. Both allow injection as `MyService`. ```swift // 1. // Now myService is of type `MyService` and registration will be // typed as `() -> MyService` instead of `() -> MyServiceImplementation` let myService: MyService /* <- Magic happens here */ = MyServiceImplementation() container.register { myService } // 2. // or like this (I prefer this option): let myService = MyServiceImplementation() container.register { () -> MyService /* <- Magic happens here */ in myService } ``` -------------------------------- ### Class Property Injection with @Injected Source: https://github.com/bealex/macaroni/blob/main/README.md Shows the five ways to use the @Injected property wrapper for class properties: lazy injection from the container captured on initialization (default or specified), lazy capturing and resolving, and eager resolving (from default or specified container). Note that parametrized injection only works with lazy resolution, and lazy injection cannot be used in structs. ```swift // 1. // Lazy injection from the container that is captured on initialization, determined by `Container.policy`: @Injected var property: Type // 2. // Lazy injection from the container that is captured on initialization (you specify it): @Injected(.capturingContainerOnInit(from: container)) var property: Type // 3. // Lazy capturing of the container and resolving: @Injected(.lazily) var property: Type // 4. // Eager resolving, during the initialization, from the container from `Container.policy`: @Injected(.resolvingOnInit()) var property: Type // 5. // Eager resolving, during the initialization, from the specified container: @Injected(.resolvingOnInit(from: container)) var property: Type ``` -------------------------------- ### Register a resolver that returns a shared instance Source: https://github.com/bealex/macaroni/blob/main/README.md Registers a resolver closure that returns the same `MyServiceImplementation` instance every time. The resolver is stored in the container for later injection. ```swift let myService = MyServiceImplementation() container.register { myService } ``` -------------------------------- ### Disable logging with DisabledMacaroniLogger Source: https://github.com/bealex/macaroni/blob/main/README.md Set Macaroni.logger to DisabledMacaroniLogger to completely disable logging. ```swift Macaroni.logger = DisabledMacaroniLogger() ``` -------------------------------- ### Parametrized Injection: Accessing Enclosing Object Source: https://github.com/bealex/macaroni/blob/main/README.md Shows how to access the object that contains the injected property from inside a registration closure. This resolver is only available for lazy injections. ```swift container.register { enclosing -> String in String(describing: enclosing) } ``` -------------------------------- ### Weak injection with @InjectedWeakly Source: https://github.com/bealex/macaroni/blob/main/README.md Use @InjectedWeakly to inject a weak reference to a service, avoiding strong reference cycles. The property must be optional. ```swift @InjectedWeakly var myService: MyService? ``` -------------------------------- ### Inject dependency using @Injected property wrapper Source: https://github.com/bealex/macaroni/blob/main/README.md Declares a class `MyClass` with an `@Injected` property of type `MyServiceImplementation`. The injection happens lazily when the property is first accessed, not during initialization. ```swift class MyClass { @Injected var myService: MyServiceImplementation } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.