面包:: Board服务B取决于服务A对象的访问者

时间:2012-03-28 21:30:51

标签: perl inversion-of-control moose bread-board

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

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
    };   
}

我不确定我可以添加或应该使用哪些代码。

1 个答案:

答案 0 :(得分:0)

到目前为止,我发现的最好的方法是使用块添加另一个服务,仅用于访问者

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