Setting a variable in a DAO

R

Rizwan

Hi All,

I am using DAO pattern to retrieve data.

public class Location implements Serializable {
Short locationId;
String locationName;

public Location() {
}
public Location( Short locationId, String locationName) {
this.locationId = locationId;
this.locationName = locationName
}

public Short getLocationId() {
return this.locationId;
}
public void setLocationId( Short locationId ) {
this.locationId = locationId;
}

public String getLocationName() {
return this.locationName;
}
public void setLocationName( String locationName ) {
this.locationName = locationName;
}
}

public interface LocationDAO {
public Location getLocationById( Short locationId ) throws
LocationNotFoundException;
public void optionalProcessing( Location location ) throws
OptionalProcessingFailException;
}

public class LocationDAOSqlServer implements LocationDAO {
public Location getLocationById( Short locationId ) throws
LocationNotFoundException {
Location location = null;
....
return location;
}

public void optionalProcessing( Location location ) throws
OptionalProcessingFailException {
...
}
}


Now I will be calling the getLocationById() method. In some cases, I want
this method to call optionalProcessing() method from inside and in some
cases I dont want it. This will be based on a condition. If condition is
true then call the optionalProcessing() method otherwise dont.

public Location getLocationById( Short locationId ) throws
LocationNotFoundException {
Location location = null;
....
if ( condition ) {
optionalProcessing( location );
}
return location;
}

Now I can see that the condition is a boolean. My question is where to
define the condition variable. And how to set it? I should set it before
calling the getLocationById() method.

LocationDAO aLocationDAO = myDAOFactory.getLocationDAO();
Location location = aLocationDAO.getLocationById( new Short(1) );


Thanks

Rizwan
 
W

Wendy S

Rizwan said:
Now I can see that the condition is a boolean. My question is where to
define the condition variable. And how to set it? I should set it before
calling the getLocationById() method.

It's customary to wait more than two and a half hours before making another
plea for help.

What is the condition? Are you making a decision based on the state of the
Location object (which I assume you retrieve prior to checking the
condition)? Or is the condition based on the state of the DAO class, or
something else entirely?
 
R

Rizwan

Hi Wendy,

Sorry for the reminder post. I will be careful next time.

Condition is checked after the retrieval of Location. It does not depend on
the state of the Location object or the state of the DAO class. Its where I
am calling it.

In my EJB I have 2 methods. Both methods are calling the getLocationById()
method of the DAO class.

In the first method, I want to call the getLocationById() method so that it
did not call the optionalProcessing() method. In the second method, I want
to call the getLocationById() method so that it also call the
optionalProcessing() method.

Thanks
 
W

Wendy Smoak

Sorry for the reminder post. I will be careful next time.

Good. Now stop top posting, and you'll be all set. ;) (Put your reply
below the quoted text, and only quote as much as you need to.)
Condition is checked after the retrieval of Location. It does not depend on
the state of the Location object or the state of the DAO class. Its where I
am calling it.

In my EJB I have 2 methods. Both methods are calling the getLocationById()
method of the DAO class.

In the first method, I want to call the getLocationById() method so that it
did not call the optionalProcessing() method. In the second method, I want
to call the getLocationById() method so that it also call the
optionalProcessing() method.

What about having two different methods in the DAO class?

The "plain" getLocationById(...) method, and the other
getLocationByIdWithOptionalProcessing(...). Obviously you will come up with
a better name for it. Move the common code out to a private helper method
that gets called from both.

This is another option, though I don't recommend it. (Passing flags is
generally frowned on in OOP):
getLocationById(int id, boolean doSpecialProcessing)
 
R

Rizwan

What about having two different methods in the DAO class?
The "plain" getLocationById(...) method, and the other
getLocationByIdWithOptionalProcessing(...). Obviously you will come up with
a better name for it. Move the common code out to a private helper method
that gets called from both.

This is the solution currently I am using but I was thinking of a better
way. The DAO can have lot of getxxx() methods. 2 methods for every such
method is lot of coding.
Now I was thinking if i can set a flag to a DAO class that if its true then
uses optionalProcessing else not. Then i only have 1 variation of each
getxxx() method. I also would like it to be an instance not a static
variable. Is it possible? or is there any better solution?

Thanks
 
W

Wendy S

Rizwan said:
Now I was thinking if i can set a flag to a DAO class that if its true
then
uses optionalProcessing else not. Then i only have 1 variation of each
getxxx() method. I also would like it to be an instance not a static
variable. Is it possible? or is there any better solution?

Sure, it's possible. It's not in my personal concept of a DAO, but if it
works for you, go for it. I think I'd lean towards passing an additional
boolean parameter to the 'read' method though.

The problem I see with having a separate setOptionalProcessingFlag(boolean)
method is that it is, well, separate. :) It's easy to forget to call it, or
forget to change the value, etc. And if you end up with two references to
the same DAO, what if one 'user' needs the flag set one way and one, the
other?

With
getLocationById( int id, boolean optionalProcessingFlag )
everything is there in one place and you're forced to assert the
special-processing-or-not decision every time.
 
R

Rizwan

you are right. better solution is to go with an additional boolean
parameter.

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top