使用Moose
and Bread::Board
,是否可以创建一个具有ArrayRef[SomeObject]
类型约束的属性的对象,并以以下方式注入该参数:
ArrayRef
保持约束,- 作为该 ArrayRef 成员的每个对象的所有依赖项都由
Bread::Board
, 和 - 作为该 ArrayRef 成员的每个对象都是由
Bread::Board
?
为了确保我清楚地解释自己,让我们考虑一个非常天真的例子。假设我们有一个Wheel
类:
package Wheel;
use Moose;
has ['size', 'maker'] => (isa => 'Str', is => 'rw', required => 1);
让我们创建一个Vehicle
类,其中每个实例都包含一堆轮子:
package Vehicle;
use Moose;
has 'wheels' => (
is => 'rw',
isa => 'ArrayRef[Wheel]',
required => 1,
);
那么是否可以创建一个或多个实例,Wheel
然后将包含这些实例的数组引用注入到我们的新Vehicle
实例中?这显然行不通:
my $c = container 'app' => as {
container 'wheels' => as {
service 'maker' => "Bob's Tires";
service 'size' => "195R65/15";
service 'carTires' => (
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');
有任何想法吗?是的,我知道我Vehicle
的 s 可以想象没有轮子,而且我正在尝试制造一款单轮轿车,但我想你明白我的意思。:-) 这只是一个非常微不足道的例子。