Re: MVC pattern

From: Laird Nelson (lairdnelson_at_comcast.net)
Date: 04/26/04


Date: Mon, 26 Apr 2004 11:04:42 -0400

Roedy Green wrote:
> If the persistence writeObject readObject is in the class it is
> persisting, you can in many cases get away with writing almost no
> code, just a bit of initialisation of transient fields.

Right, and then something else (an accessor) has to do the writing (e.g.
has to create an ObjectOutputStream, write to it, handle errors, etc.),
and then you have to be able to live with the file/wire format. This
works in many cases, and falls down in others.

> If you do the persistence in another class, you need a giant long list
> of calls to getters and setters to EVERYTHING that is persistable.
> Every time you add a new field this code has to be adjusted. It is
> hard to tell if you remembered everything.

Yes, that's correct. The computer cannot magically figure out what
fields you want, for example, to map to database columns.

> You have to create duplicate fields in your persister class and make
> sure the declarations stay in perfect sync.

I don't understand this.

Given the following dumb class:

   public class Foo {
     private String id;
     private String bar;
     private String baz;
     public String getID() {
       return this.id;
     }
     public void setID(final String id) {
       this.id = id;
     }
     public String getBar() {
       return this.bar;
     }
     public void setBar(final String bar) {
       this.bar = bar;
     }
   }

...and its associated dumb "accessor":

   public class FooStorage {
      public static final void store(final Foo foo) {
         if (foo != null) {
            final String id = foo.getID();
            if (id != null) {
              somePersistenceSystem.write("foo." + id + ".bar",
                                          foo.getBar());
            }
         }
      }
   }

...where are the duplicate fields in FooStorage? What needs to be kept
in sync?

> It is a heck of a lot of error-prone programmer work with very little
> payback. It for the computer, is only the overhead of each
> getter/setter.

I don't understand your last sentence, though I obviously get the gist
of your statement. I disagree. For many persistence systems, figuring
out what data goes where is at the core of why we write programs. We
have data in X and we need to get it into Y. Plug in any of the
following, respectively, for X and Y:

* an object of type Foo, an object of type Bar
* one company's idea of what a Person is, another company's idea of what
   a Person is
* a tabular representation in a database, a domain model
* a filesystem, a database
* an object of type Foo, a database
* a table named Splatz with a schema of Garf, a filesystem
* a String, a structured object
* a fetid pile of stinking XML, another fetid pile of stinking XML
* a Java serialized object, yet another fetid pile of stinking XML

...and so on.

Cheers,
Laird



Relevant Pages

  • sending data to other application - ASP.NET
    ... I am writing an ASP.NET application, in a particular page I have to update a ... record in database and after the record is entered one of the field value has ... to move to other application as a string ...I am able to store the data as ... Prev by Date: ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: eval() or call_user_func() ???
    ... script (not a string). ... I am writing a simplified 'clone' of crontab, ... tasks - the tasks vbeing stored in a database table - I'm not ...
    (comp.lang.php)
  • Re: Retrieiving Description for Forms and Reports
    ... I am writing a documentation report for a database. ... Function GetDescription(ObjectName As String, ObjectType As String) ...
    (microsoft.public.access.developers.toolkitode)
  • Re: Writing Euro symbol with utf encoding
    ... >> I am writing a string from the database into a text file which ... >> but the text file created has just a square box in place of the Euro ... UTF-8 file i think i can get away with not encoding the string. ...
    (comp.lang.php)
  • Re: If you were developing a database in Forth...
    ... but apparently there is a lack of imagination. ... He specifically asked about persistence, ... "reflexively" reaches for a database engine ... him to "imagine" iterating over the dictionary, outputting Forth ...
    (comp.lang.forth)