How can I improve this design that calls third par

2019-07-31 00:57发布

问题:

I want opinions and expert advice on design .

I am developing an application using JSF2 and EJB3 and I am not using CDI.

I have been given an API (jar) from an other team and there are three main methods in it. One searches the database and returns a list of cars and one returns a list of pushbikes and one returns list of boats.
So basically we have three different lists of different vehicles.

At the ui side user has a search form with three radio buttons one for each type of vehicle that user wants to search for i.e Radio for car,pushbike or boat.Then one the same page we have three separate sections all visible to user where user can provide details as to what is to be searched.e.g if user wants a list of cars there is option to provide registration number or make model etc and if user wants to search for boats input fields are there to provide details for name of boat or marina etc.
There is one search button.Now I want advice of how to best put the layers to access this jar make the call and show results.
Following is how I am approaching it
I have a SearchBean which is JSF managed bean.
I have three classes CarSearchRequest , PushBikeSearchRequest and BoatSearchRequest each of them has a search() method.
Then I have three stateless session beans CarDAO , PushBikeDAO and BoatDAO each one of these DAO makes the call to method in given jar e.g. CarDAO will make call to API that returns car and so on .
Each of the three Request classes above has a private default constructor and only public constructor takes a respective DAO as an argument .
Then in my SearchBean after I am injecting three DAO which are EJBs and in @PostConstruct method I am initialising all three Request objects and passing the DAO to their constructors.
The search button on Screen then calls the search() Method of searchBean which is something like this

public void search()
{
  //Vehicle is an enum whose value is selected by radi button
  if(this.vehicle.isCar())
  {
    carRequest.search();
  }
  else if(this.vehicle.isPushBike())
  {
    pushBikeRequest.search();
  }
  //boat checks and calls here ...

}

I hate this if statements in this search a good design should not need this check.
I am not sure but calling dao search from request object sound anti-pattern.Is this a good approach at all?
I seem to be creating 6 classes (3 Request Objects and 3 DAO)which again does not sound right.Can not seem to get away with generics since each search implementation is different.

Can someone please give me a better approach or any relevant advice.

回答1:

am developing an application using JSF2 and EJB3 and I am not using CDI.

My advice is related to CDI. Why have you decided not to use it? No using CDI you are lossing two most fundamental new services provided by JEE6 in JSF web development : Contexts and Dependency injection. You could work exclusively with EJB 3.1, but you will not end up with the simplest possible solution. I would begin with CDI then if I need other requirements like transactions, jmx, pooling, etc.. then EJB comes to rescue you