Hi Monks

I have objects that get data from a db, very simple, a few columns, some of which are datetime's.

Now, in my code, I'd like to use a DateTime object wherever I store a datetime field from the DB, this make it easy to always know that if an object attribute is a date/time, i can immediately use all the DateTime methods on it.

Even better for when these objects appear in templates and I can simply send my front-end team to read about DateTime->strftime without having to do anything bespoke.

Unfortunately this can cause a performance hit if i'm getting 100,000 rows from the DB and instantiating an object for each one, if they each have 2 datetime stamps.

So what may you suggest? I'm currently thinking about a private attribute to store the literal datetime value, then a lazily-built attribute to convert to a DateTime if and when needed.

Alternatively, since I may SOMETIMES want to display the literal value (think a basic admin page somewhere), 2 methods, one for $foo->date_added (literal), and one for $foo->date_added_DATETIME.

I'm aiming to use a similar approach for Emails (using Email::Address) and URLs (using URI).

I'm not sure if Moose allows for this sort of approach, I'm nearly thinking Traits? Do I need to go down the route of my own Type, and can that Type have 2 internal representations?

Any input greatly appreciated...


In reply to Object design/structure with sub-objects (Moose) by Cagao

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.