开发者

Bread::Board - Injecting parameters with ArrayRef type constraints?

Using Moose and Bread::Board, is it possible to create an object with an attribute that has an ArrayRef[SomeObject] type constraint and have that parameter injected in such a way that:

  • The ArrayRef constraint is maintained,
  • Each object that is a member of that ArrayRef has all of its dependencies met by Bread::Board, and
  • Each object that is a member of that ArrayRef is an object that was created by Bread::Board?

In order to make sure that I'm explaining myself clearly, let's consider an incredibly naive example. Let's say we have a Wheel class:

package Wheel;
use Moose;

has ['size', 'maker'] => (isa => 'Str', is => 'rw', required => 1);

And let's create a Vehicle class where each instance contains a bunch of wheels:

package Vehicle;
use Moose;

has 'wheels' => (
    is => 'rw',
    isa => 'ArrayRef[Wheel]',
    required => 1,
);

Is it then possible to create one or more instances of Wheel and then inject an array reference containing those instances into our new Vehicle instance? This obviously won't work:

my $c = container 'app' => as {
    container 'wheels' => as {
        service 'maker' => "Bob's Tires";
        service 'size' => "195R65/15";
        service 'c开发者_JAVA百科arTires' => (
            class   => 'Wheel',
            dependencies => [ depends_on('maker'), depends_on('size') ],
        )
    };

    container 'vehicles' => as {
        service 'sedan' => (
            class   => 'Vehicle',
            dependencies => {
                # WON'T WORK
                wheels => depends_on('/wheels/carTires'),
            }
        )
    };
};
my $v = $c->resolve(service => 'vehicles/sedan');

Any ideas? Yes, I am aware that it my Vehicles can conceivably have no wheels and that I'm trying to create a one-wheeled sedan, but I think you get my point. :-) This is intended only to be an incredibly trivial example.


You could get the carTires service to return a ArrayRef of Wheels:

container 'wheels' => as {
    service 'carTires' => (
        block => sub {
            return [ map {Wheel->new} 1..4 ];
        },
    )
};

The Vehicle constructor will take care of the rest in terms of the type constraint, since you've already defined it as ArrayRef[Wheel].

So it will die if you did this instead:

container 'wheels' => as {
    service 'carTires' => (
        block => sub {
            return [ map {Window->new} 1..4 ];
        },
    )
};
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜