Christian Pesch wrote:

Gus Heck wrote:

[..]


/**
* The actual data that needs to be persisted for a location.
*
* @ojb.class * @jdo.persistence-capable
*/
public class LocationBase implements Location {
/**
* @ojb.field primarykey="true"
*/
Integer id;
/**
* @ojb.field length="40"
*/
String country;



If you want to write a setter method for this field:

public void setCountry(String newCountry) {
 if(newCountry != null && newCountry.length() > 40)
   a) newCountry = newCountry.substring(0, 40);
   b) throw new StringTooLongException("Length of country
              has to be less or equals than 40 characters");
 this.country = newCountry;
}

I havn't yet tried to do this... But I am aware that it is a problem that I will need to solve. I currently am using struts, and setting the max length on the form to be the same as in the database, but when I get around to it I will try to take out the form side restrictions and do something more like what you are suggesting here, because it is a violation of DRY :) It also violates MVC to depend on setting the field size in the view as I am because the view is now aware of the feild size parameters of the model/database, and thus the model and the view become linked.



Regardless, if you want to reduce the size of the string or let the caller do it: you replicate the size of the string (here: 40) in the code.


This puts the information in one place. which is very nice and easy to maintain.


How do you solve the problem above?

I havn't yet. Post your solution if you find it, I'll post mine when I find one :)

-Gus


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to