Constructor injection

1 view
Skip to first unread message

Simen

unread,
Apr 17, 2009, 10:28:02 AM4/17/09
to Smartypants IOC
I've managed to hack constructor injection into SmartyPants by
examining the [Inject] metatag on the class itself instead of the
constructor function. It works for recursive injection but it presents
a host of other problems, including:

- Using differently named providers for the constructor arguments.
- Using live injection for one or many of the constructor arguments.
- Reusing the injected constructor arguments in later rules.

Right now it's somewhat of a mess since I hacked in Module support,
injector.bind(IFoo).toInstance(Foo) as well as injector.getInstance
(IFoo, "fooNamed") but here's the patch anyway (for experimental
purposes, it's unoptimized and pretty crude)

http://pastie.org/449817

Josh McDonald

unread,
Apr 17, 2009, 10:53:26 PM4/17/09
to smartyp...@googlegroups.com
I haven't looked at it in detail, but I will and let you know what I think. I definitely like the Module idea, I was planning on doing something similar to that as part of scoping.

I'm not sure about where we can/should put the annotations for constructor injection, as there's a bug in Player that DescribeType(MyClass) returns more information *after* construction of an instance of MyClass than it does before an instace has been constructed. I'm not sure and haven't tested it, but I hope and assume that it's instantiation rather than construction that is required. If so, we can have a class with a constructor like this:

function MyClass(one:Number, two:Number) { //... }

and do:

try
{
  new MyClass();
}
catch (expected:Error) {;}

And hopefully this will be enough for the AVM to populate the missing information. That way we can call describeType() again, and get the complete constructor profile.

-Josh

2009/4/18 Simen <simen....@gmail.com>



--
"Therefore, send not to know For whom the bell tolls. It tolls for thee."

Josh 'G-Funk' McDonald
  -  jo...@joshmcdonald.info
  -  http://twitter.com/sophistifunk
  -  http://flex.joshmcdonald.info/

Simen

unread,
Apr 18, 2009, 4:17:17 AM4/18/09
to Smartypants IOC
That's exactly what I'm doing, there's a createNullInstance
(type:Class, parameterCount:uint) in InjectorImpl. Not only is it
hacky as hell but what if you create an instance of something that
ends up firing off other processes? Ideally one would be able to
specify constructor parameters in more detail, while it removes a lot
of flexibility it opens up for better configurability.

[Inject(apple="model::Apple", orange="model::BloodOrange,live",
pear="model::Pear")]
public class MyClass {
public function MyClass[apple:IFruit, orange:ICitrusFruit,
pear:Pear):void
...
}

While it might seem to double up the sugar it does give you a chance
to clearly specify what you want injected.
Reply all
Reply to author
Forward
0 new messages