Domanda

I'm relatively new to developing on JavaEE. I'm wondering what the best format is in terms of passing parameters into a session bean for object creation or deletion calls.

For my backend, I use a DTO with a DAO to run create, update, delete and read operations on the database

public class BusinessObject {
    public String name;
    public int id;

    // Assume constructor to init these 2 fields.
}

I have a simple DAO interface:

public interface BusinessDAO {
    public void createBusinessObject(BusinessObject bo);
    public void deleteBusinessObject(BusinessObject bo);
    public void updateBusinessObject(BusinessObject bo);
    public void findBusinessObject(BusinessObject bo);
}

(I believe the implementation of BusinessDAO at this stage is irrelevant)

So on to my actual question - If I have a remote (stateless session bean) EJB interface, should I define a method inside the remote EJB interface:

public void addBusinessObject(String name, int id);

Or something along the lines of:

public void addBusinessObject(BusinessObject bo);

I have a simple client program that would call either of these methods.

My thoughts is that for object creation the BusinessObject bo definition will work better, however I seem to think there is a pattern I can follow for other definitions in the remote interface.
I don't see how I can incorporate the same style of method definition for something like findObject(BusinessObject bo)

Is the standard practice to create the BusinessObject and only provide say the ID field in that object, then the DAO implementation returns the filled in object back to the client (via the session bean)?
Or is it better to pass an int id into the findObject method instead and just return a BusinessObject?

È stato utile?

Soluzione

  • As for findObject(): Just pass the ID. You are designing a remote interface. Anyway if your DTO has many fields, this would waste bandwidth.

  • As for addBusinessObject(): Pass the DTO. This "scales" better, if you have a large number of fields in your DTO: It will simplify maintenance later, if you add/remove fields.

On the other hand, if you have something like changeOneFieldOnly(), you can pass the ID, and the new value (and not the complete DTO). Especially in case of remote interfaces, tailor your methods to the use cases (as a rule of thumb: for each user action only one call to the remote service layer).

So have a look at your remote interface from the use case perspective (instead of create/add/remove).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top