The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Reconstitute an object

Usually,when we create a new object,and save to database,id is provided by database(like IDENTITY in sql 200),so when i get the same object,its id is initialized.Now,suppose user does some modification to this object on a form,now using the data given by user with its id(which i stored in a hidden field,an form),how can i reconstitute the object,since there is no setter for id in my class..
Friday, July 28, 2006
I am thinking of making a factory method that
returns the  object from ID ..
Friday, July 28, 2006
If I am reading you correctly you have a object that you can store and retrieve from a database. Plus the ID field is used  identify that data whether it used as a object or a database row.

If this is a one-off type application and not one that will need to be supported five years from now then just make a subroutine that acts as a factory method.

IF this a long term project then...

First Suggestion is not to rely on IDENTITY in the database for your ID. Since that data is being used both by the database and in the object framework you made I suggest using some type of GUID. That way you have control over the generation of IDs rather than relying on how SQL 2000 works. It doesn't matter what you use to generate the GUID as long as the ID is unique.

Plus I would make a interface between the framework you created and the database. So that if you ever changed databases (or the database changes from upgrading) all you worry about is how the interface works the database. Along with this interface would be a factory method that would take a GUID and return the object filled with data from the database.
Rob Conley Send private email
Friday, July 28, 2006
Rob's advice is probably good, but it seems way too intensive for my liking.  I've been using SQL Server with identity fields and writing web apps for quite a while now, without any problems.

A factory seems like a lot of work.  Why not just put a setter method on the object for the identity.  That seems like the easiest and most sensible solution, since you have a very valid need for setting that value.
Clay Dowling Send private email
Friday, July 28, 2006
A factory can be a simple as function. It gets complex only when you are dealing more than one type of object or rather more than one type of object implementing the same interface. Which is the purpose behind a factory.

For example you have  IReport interface and multiple report object reprensenting different type of reports. Then it is simplier to have a function that takes a couple of parameters and returns a IReport object pointing to the right report object. Everything about report creation is in one spot.

As for the original requrest I think it really hinges on whether this is a short term throwaway project or a long term project. If you are maintaining this over the couple of years then it always pays off to do this type of design upfront. Because most of your cost will be in mantaining the software.
Rob Conley Send private email
Friday, July 28, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz