Search code examples
perlinversion-of-controlmoosebread-board

Bread::Board service B depends on accessor on service A object


Using Bread::Board I have an object/service A with with accessor $A->foo. Object/service B depends on $A->Foo for it's contstructor. How would I do this? Here's an example of what I have

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

I am not sure what code I could add or should have to make this work.


Solution

  • The best way I've found so far is to add another service using block for just that accessor

    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' ],
            );
        };   
    }
    

    This of course all assumes that A has an accessor foo