Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling


by httptech (Chaplain)
on May 16, 2000 at 21:15 UTC ( #11892=perlquestion: print w/replies, xml ) Need Help??
httptech has asked for the wisdom of the Perl Monks concerning the following question:

I'm curious to know if anyone here is using Tangram in their database apps, and if so, why? I've read the introductory article, but I don't see the clear advantages to abandoning standard DBI code in favor of Tangram. Yes, it's object-oriented, but I don't always feel object-oriented is the way to go. The fact that Perl doesn't force me to use objects if I don't want to is one of the things I love about Perl.

I'd just like to know if there are any real-world examples that demostrate its advantages convincingly enough to make me change the way I design my database applications.

Replies are listed 'Best First'.
Re: Tangram
by btrott (Parson) on May 16, 2000 at 21:25 UTC
    The point of Tangram isn't that it's "object-oriented"--the point, I think, is that it lets you have persistent objects w/o worrying about how to make them persistent. So rather than you having to worry about how to structure your database to hold a bunch of your objects, and you writing the SQL to store them, you can concentrate on how to use the objects, in Perl. Tangram takes care of the storage in the database.

    For example, say you have data about a bunch of people. You know their name, their age, and their hair color. You create a simple Perl object to hold this data, and you can instantiate it at will:

    my $me = new Person; $me->age(22);
    and so on.

    Now, what if you want to make these objects persistent? In other words, between runs of your program, you need to store this data somewhere. A database is as good a place as any.

    The problem, though, is that placing your database code into your object (into a save method, for example) would make your code very specific. Objects should be more flexible about how they're stored--they shouldn't be tied in to only one storage method.

    This is where Tangram comes in: it provides the methods that let you store your object in a database. So you can create it, store it, quit your program, then start it up again--and load your object from the database magically, as if it had never left.

    So it's not a matter of you being forced/cajoled/etc. into writing object-oriented code. The point here is that you're *already* writing OO code, and now you need a way to store your objects. You need object persistence, and that's what Tangram gives you.

    I've never used it myself, though. :)

      Let me see if I got this straight...

      Suppose I have an online store where I sell goods to several different countries each using its own currency. I could say (for Brazil's case):
      my $real = new Currency; $real->name('Real'); $real->country('BR'); $real->dolarValue(1.81);
      And repeat this as many times necessary for each country. Then use set everything up, and use DBI to get the items from a shopping cart as usual (name, price, other attributes). Then my app would work as if my currency variables had been declared at startup, so I wouldn't have to do a 'select' myself to find out what the price of an item was in Reals?

      That's a bit confusing... Let me try this instead.
      Instead of doing two selects (one for the product, and one for the currency), I could have the currency loaded as an object and do my select on the product as if the currency information had always been there?

      # Trust no1!
        Well... I'm not sure I know quite what you mean. In your example, you could create all of those Currency objects, then use Tangram to insert them into a database. That would be one script.

        Then, in another script, you could tell Tangram to go and get you a particular Currency object. It would load the data from the database and hand you back a Currency object, just like the one you had created in the first script.

        You're not actually eliminating select statements, because *someone* still has to do a select--even if it's not you, it's Tangram. :) But you don't have to worry about that select; you can just act as if you had created the Currency object in the same script.

        Does that make sense?

        There's a good introduction to using Tangram in WebTechniques.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11892]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2019-04-25 23:42 GMT
Find Nodes?
    Voting Booth?
    I am most likely to install a new module from CPAN if:

    Results (127 votes). Check out past polls.

    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!