如何在 Moose 中使用单个构建器构建多个属性? [英] How can I build multiple attributes with a single builder in Moose?

查看:47
本文介绍了如何在 Moose 中使用单个构建器构建多个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Moose,是否可以创建一个同时构建多个属性的构建器?

Using Moose, is it possible to create a builder that builds multiple attributes at once?

我有一个项目,其中对象具有多个组"字段 - 如果需要该组的任何成员,我想继续填充它们.我的假设是,如果我需要姓名,我还需要出生日期,而且由于它们在同一个表中,因此在一个查询中同时获取两者会更快.

I have a project in which the object has several 'sets' of fields - if any member of the set is requested, I want to go ahead and populate them all. My assumption is that if I need the name, I'll also need the birthdate, and since they're in the same table, it's faster to get both in one query.

我不确定我的问题是否足够清楚,但希望一些示例代码能够说明问题.

I'm not sure if my question is clear enough, but hopefully some sample code will make it clear.

我有什么:

Package WidgetPerson;
use Moose;

has id => (is => 'ro', isa => 'Int' );
has name => (is => 'ro', lazy => 1, builder => '_build_name');
has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');
has address => (is => 'ro', lazy => 1, builder => '_build_address');

sub _build_name {
 my $self = shift;
 my ($name) = $dbh->selectrow_array("SELECT name FROM people WHERE id = ?", {}, $self->id);
 return $name;
}
sub _build_birthdate {
 my $self = shift;
 my ($date) = $dbh->selectrow_array("SELECT birthdate FROM people WHERE id = ?", {}, $self->id);
 return $date;
}
sub _build_address {
 my $self = shift;
 my ($date) = $dbh->selectrow_array("SELECT address FROM addresses WHERE person_id = ?", {}, $self->id);
 return $date;
}

但我想要的是:

has name => (is => 'ro', isa => 'Str', lazy => 1, builder => '_build_stuff');
has birthdate => (is => 'ro', isa => 'Date', lazy => 1, builder => '_build_stuff');
has address => (is => 'ro', isa => 'Address', lazy => 1, builder => '_build_address');
sub _build_stuff {
 my $self = shift;
 my ($name, $date) = $dbh->selectrow_array("SELECT name, birthdate FROM people WHERE id = ?", {}, $self->id);
 $self->name($name);
 $self->birthdate($date);
}
sub _build_address { 
 #same as before 
}

推荐答案

在这种情况下,当我不想像 Ether 的答案中那样有一个单独的对象时,我所做的是为中间状态设置一个延迟构建的属性.因此,例如:

What I do in this case, when I don't want to have a separate object as in Ether's answer, is have a lazily built attribute for the intermediate state. So, for example:

has raw_row => (is => 'ro', init_arg => undef, lazy => 1, builder => '_build_raw_row');
has birthdate => (is => 'ro', lazy => 1, builder => '_build_birthdate');

sub _build_raw_row {
   $dbh->selectrow_hashref(...);
}

sub _build_birthdate {
    my $self = shift;
    return $self->raw_row->{birthdate};
}

对姓名等重复与 birthdate 相同的模式

Repeat the same pattern as birthdate for name, etc.

读取任何单个属性将尝试从 raw_row 获取数据,其惰性构建器只会运行 SQL 一次.由于您的属性都是只读的,因此您不必担心在其中一个发生更改时更新任何对象状态.

Reading any of the individual attributes will try to get data from raw_row, whose lazy builder will only run the SQL once. Since your attributes are all readonly, you don't have to worry about updating any object state if one of them changes.

这种模式对 XML 文档之类的东西也很有用——你保存的中间状态可以是例如一个 DOM,具有从 XPath 表达式或您拥有的东西懒惰地构建的单个属性.

This pattern is useful for things like XML documents, too -- the intermediate state you save can be e.g. a DOM, with individual attributes being lazily built from XPath expressions or what-have-you.

这篇关于如何在 Moose 中使用单个构建器构建多个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆