1

使用Bread::Board我有一个A带有访问器的对象/服务$A->foo。对象/服务 B 依赖于$A->Foo它的构造函数。我该怎么做?这是我所拥有的一个例子

use Moose;
use Bread::Board;


has info => ( is => 'ro', lazy => 1, isa => 'Str', default => sub { 'something' } );
extends 'Bread::Board::Container';

sub BUILD {
    my $self = shift;
    container $self => as {
        service info => $self->info;

        service A => (
             class => 'A',
             dependencies => {
                 info => depends_on('info'),
             },
        );
        service B => (
            class => 'B',
            dependencies => {
                foo => depends_on('foo'), # foo could be gotten by
            },                            # ->resolve( service => 'A' )->foo 
        );                                # e.g foo is an accessor on A
    };   
}

我不确定我可以添加或应该添加什么代码来完成这项工作。

4

1 回答 1

0

到目前为止,我发现的最好方法是使用 block 为该访问器添加另一个服务

use Moose;
use Bread::Board;


has info => ( is => 'ro', lazy => 1, isa => 'Str', default => sub { 'something' } );
extends 'Bread::Board::Container';

sub BUILD {
    my $self = shift;
    container $self => as {
        service info => $self->info;

        service A => (
             class => 'A',
             dependencies => {
                 info => depends_on('info'),
             },
        );
        service B => (
            class => 'B',
            dependencies => {
                foo => depends_on('foo'), # foo could be gotten by
            },                            # ->resolve( service => 'A' )->foo 
        );                                # e.g foo is an accessor on A

# ADD SERVICE
        service foo => (
            block => sub {
               my $s = shift;
               return $s->param('A')->foo;
            },
            dependencies => [ 'A' ],
        );
    };   
}

这当然都假设 A 有一个访问器 foo

于 2012-03-29T19:57:45.253 回答