Bread::Board - 使用 ArrayRef 类型约束注入参数?
使用 Moose
和 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');
有什么想法吗?是的,我知道我的车辆
可以想象没有轮子,并且我正在尝试创建一辆单轮轿车,但我认为您明白我的意思。 :-) 这只是一个非常简单的例子。
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 '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');
Any ideas? Yes, I am aware that it my Vehicle
s 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以让 carTires 服务返回 Wheels 的 ArrayRef:
Vehicle 构造函数将根据类型约束处理其余部分,因为您已经将其定义为 ArrayRef[Wheel]。
所以如果你这样做的话它就会死掉:
You could get the carTires service to return a ArrayRef of Wheels:
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: