Bread::Board - 使用 ArrayRef 类型约束注入参数?

发布于 2024-11-15 16:14:10 字数 1725 浏览 3 评论 0原文

使用 MooseBread::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 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2024-11-22 16:14:10

您可以让 carTires 服务返回 Wheels 的 ArrayRef:

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

Vehicle 构造函数将根据类型约束处理其余部分,因为您已经将其定义为 ArrayRef[Wheel]。

所以如果你这样做的话它就会死掉:

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

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 ];
        },
    )
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文