有没有一种简单的方法可以将 DBIx::Class 结果映射到我的自定义 Moose 类?

发布于 2024-08-22 11:12:09 字数 172 浏览 6 评论 0原文

上我的驼鹿课程似乎有点痛苦。然后使用 DBIx::Class获取结果集..然后手动将我的结果集映射到驼鹿类。

It seems kind of a pain to have my Moose classes. Then to use DBIx::Class to get a result set..then to manually map my result set to the moose classes.

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

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

发布评论

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

评论(3

迟到的我 2024-08-29 11:12:09

如果您必须在 Moose 类和 DBIC 模式之间来回映射,您可能需要查看持久对象存储,例如 KiokuDB

您会失去关系数据库的一些功能,特别是如果您有现有架构,但您获得了很多功能,其中主要的一个是数据存储和对象模型之间的安静映射。 KiokuDB 的 DBI 后端可能是这种权衡的最好例子。该数据库是高度非规范化的,但这是因为它有效地充当密钥存储。

然而,KiokuDB 可以与针对此类数据优化的存储引擎一起使用。它支持当前的一些“NoSQL”名人,包括 CouchDB 和 MongoDB。它还支持老粉丝最喜欢的 BerkelyDB。

Kioku 并不能解决所有问题,但它非常成功地用于停车移动来无缝处理所有数据存储。

If you're having to map back and forth between Moose classes and a DBIC schema, you may want to look at a persistent object store like KiokuDB instead.

You lose some of the features of a Relational Database, especially if you have an existing schema but you gain a lot of features the main one being quiet mapping between the data-store and your Object model. The DBI back-end for KiokuDB is probably the best example of this trade off. The database is highly de-normalized, but that's because it's working as effectively a key-store.

KiokuDB however can work with storage engines that are optimized for this kind of data. It supports several of the current crop of "NoSQL" celebrities including CouchDB, and MongoDB. It also supports the older fan favorite BerkelyDB.

Kioku isn't the answer for every problem, but it is used quite successfully for Parking Mobility to handle all the data storage seamlessly.

涫野音 2024-08-29 11:12:09

您可以将 Moose 与 DBIC 一起使用,没有问题。实际上,我喜欢使用 MooseX::Declare,因为我发现扩展语法在设计可靠的公共 api 时非常有用,例如:

use MooseX::Declare;
class MyApp::Schema::Result::Geo::Division
 extends MyApp::Schema::Result {
    use Locale::Geocode::Division;
 __PACKAGE__->table('division');
 __PACKAGE__->add_columns(
  fk_territory_id => {
   data_type => 'char',
   size => '36',
  },
  division_id => {
   data_type => 'char',
   size => '36',
  },
  code => {
            data_type => 'varchar',
   size => '5',
  },
     created => {
   data_type => 'datetime',
   set_on_create => 1,
  },
 );

 __PACKAGE__->set_primary_key('fk_territory_id','division_id');
 __PACKAGE__->uuid_columns('division_id');
    __PACKAGE__->add_unique_constraint(['fk_territory_id','code']);

 __PACKAGE__->belongs_to(
  territory => 'MyApp::Schema::Result::Geo::Territory',
  {'foreign.territory_id' => 'self.fk_territory_id'},
 );
    method as_geocode_division {
        Locale::Geocode::Division->new($self->code);
    }     
 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
} 1;

You can use Moose with DBIC no problem. Actually I enjoy using MooseX::Declare since I find the extended syntax is very useful when designing solid public apis, for example:

use MooseX::Declare;
class MyApp::Schema::Result::Geo::Division
 extends MyApp::Schema::Result {
    use Locale::Geocode::Division;
 __PACKAGE__->table('division');
 __PACKAGE__->add_columns(
  fk_territory_id => {
   data_type => 'char',
   size => '36',
  },
  division_id => {
   data_type => 'char',
   size => '36',
  },
  code => {
            data_type => 'varchar',
   size => '5',
  },
     created => {
   data_type => 'datetime',
   set_on_create => 1,
  },
 );

 __PACKAGE__->set_primary_key('fk_territory_id','division_id');
 __PACKAGE__->uuid_columns('division_id');
    __PACKAGE__->add_unique_constraint(['fk_territory_id','code']);

 __PACKAGE__->belongs_to(
  territory => 'MyApp::Schema::Result::Geo::Territory',
  {'foreign.territory_id' => 'self.fk_territory_id'},
 );
    method as_geocode_division {
        Locale::Geocode::Division->new($self->code);
    }     
 __PACKAGE__->meta->make_immutable(inline_constructor => 0);
} 1;
黯然#的苍凉 2024-08-29 11:12:09

听起来你正在准确地描述我最近写的内容,以便将 Moose 属性值映射到 Rose::DB::Object 值(私有属性中包含 db 对象和 objectmanager),反之亦然。我最初在每个 Moose 属性周围使用触发器来立即写入 Rose 对象,但后来放弃了这种方法,并仅在需要时才延迟写入值(即在 ->save() 时)手术)。我使用几个角色和一个糖类来实现它,该类会自动安装一个属性特征,指示相关属性的“我是表字段”。

但不要像我一样——直接使用 DBIx::Class !据我所知,下一个主要版本无论如何都会用 Moose 重写。

It sounds like you're describing exactly what I wrote recently in order to map Moose attribute values onto Rose::DB::Object values (with the db object and objectmanager contained in private attributes) and vice versa. I originally used triggers around each Moose attribute to write to the Rose object immediately, but later abandoned that approach and lazily-wrote the values only when needed (i.e. at the time of a ->save() operation). I implemented it using a few roles and a sugar class which automatically installed an attribute trait indicating "I am a table field" for the relevant attributes.

But don't do what I did -- just use DBIx::Class directly! The next major version is being rewritten in Moose anyway, so I hear.

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