Setting a variable in a DAO

Discussion in 'Java' started by Rizwan, Apr 20, 2005.

  1. Rizwan

    Rizwan Guest

    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
    Rizwan, Apr 20, 2005
    #1
    1. Advertising

  2. Rizwan

    Rizwan Guest

    Anyone please?

    Thanks

    Rizwan

    "Rizwan" <> wrote in message
    news:Fmv9e.4954$...
    > 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
    >
    >
    Rizwan, Apr 21, 2005
    #2
    1. Advertising

  3. Rizwan

    Wendy S Guest

    "Rizwan" <> wrote

    > 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?

    --
    Wendy
    Wendy S, Apr 22, 2005
    #3
  4. Rizwan

    Rizwan Guest

    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



    "Wendy S" <> wrote in message
    news:Sm_9e.28743$lv1.10610@fed1read06...
    > "Rizwan" <> wrote
    >
    > > 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?
    >
    > --
    > Wendy
    >
    >
    >
    >
    >
    Rizwan, Apr 22, 2005
    #4
  5. Rizwan

    Wendy Smoak Guest

    > 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)

    --
    Wendy
    Wendy Smoak, Apr 22, 2005
    #5
  6. Rizwan

    Rizwan Guest

    > 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

    "Wendy Smoak" <> wrote in message
    news:d4bl8n$itp$...
    > > 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)
    >
    > --
    > Wendy
    >
    >
    Rizwan, Apr 26, 2005
    #6
  7. Rizwan

    Wendy S Guest

    "Rizwan" <> wrote
    > 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.

    --
    Wendy
    Wendy S, Apr 27, 2005
    #7
  8. Rizwan

    Rizwan Guest

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

    Thanks

    "Wendy S" <> wrote in message
    news:kJDbe.33111$lv1.10447@fed1read06...
    > "Rizwan" <> wrote
    > > 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.
    >
    > --
    > Wendy
    >
    >
    Rizwan, Apr 27, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mirek

    asp.net and dao?

    mirek, Aug 4, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    2,267
    Mirek Rewak
    Aug 6, 2003
  2. SKG

    DAO and RDO and ADO

    SKG, Aug 6, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,538
    Steve C. Orr [MVP, MCSD]
    Aug 6, 2004
  3. Andy Grove
    Replies:
    0
    Views:
    416
    Andy Grove
    Sep 2, 2003
  4. Ed Thompson

    Java Data Access Object Design (DAO)

    Ed Thompson, Sep 17, 2003, in forum: Java
    Replies:
    2
    Views:
    3,081
  5. Andy Grove
    Replies:
    0
    Views:
    426
    Andy Grove
    Nov 4, 2003
Loading...

Share This Page