开发者

SwiftSuspenders not working like it should?

开发者 https://www.devze.com 2023-03-02 04:54 出处:网络
I\'m just getting my feet wet with trying to use swift suspenders for an AS3 project but my injections are null when I try to access them. It only works when I use injector.injectInto() explicitly to

I'm just getting my feet wet with trying to use swift suspenders for an AS3 project but my injections are null when I try to access them. It only works when I use injector.injectInto() explicitly to inject into the object that should have a r开发者_开发知识库eference. But that can't be the optimal approach with SwiftSuspenders, right?


Aren't you by any change accessing your injected properties in class constructor? If so, use PostConstruct meta.

public class Example
{
    [Inject] public var foo:Bar;

    public function Example()
    {
        foo.barMe(); // throws null reference error
    }

    [PostConstruct]
    public function _postConstruct():void
    {
        foo.barMe(); // OK
    }
}

PostConstruct: Automatically invoking methods on injection completion

Instances of classes that depend on automatic DI are only ready to be used after the DI has completed. Annotating methods in the injectee class with the [PostConstruct] metadata causes them to be invoked directly after all injections have completed and it is safe to use the instance. Multiple methods can be invoked in a defined order by using the order parameter: [PostConstruct(order=1)].

ref [1]


You do need to call injectInto on every object requiring dependencies. The way around this step is to abstract that step into a dependency injection container ("DI container") that manages that for you. The MVC toolkit, Robotlegs, accomplishes this with its own system for automated dependency injection.

Here's an excerpt from the "ActionScript Developer's Guide to Robotlegs":

The intention behind automated DI containers is to abstract the fulfillment of dependencies from the application itself. Essentially, we split this job out completely, so that the application code no longer has to do it, and instead we ask a third party—the DI container—to get it done.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号