如何改进从基于 JSF 的前端调用第三方方法的设计
我想要有关设计的意见和专家建议。
我正在使用 JSF2 和 EJB3 开发应用程序,并且不使用 CDI。
我从其他团队获得了一个 API(jar),其中包含三个主要方法。一个搜索数据库并返回汽车列表,一个返回手推自行车列表,一个返回船列表。
所以基本上我们有不同车辆的三个不同列表。
在用户界面一侧,用户有一个搜索表单,其中包含三个单选按钮,每个单选按钮对应用户想要搜索的每种类型的车辆,即汽车、自行车或船的无线电。然后在同一页面上,我们有三个单独的部分,所有部分都可见用户可以提供有关要搜索的内容的详细信息。例如,如果用户想要汽车列表,则可以选择提供注册号或制造型号等,如果用户想要搜索船只,输入字段可以提供名称详细信息船或码头的等等。
有一个搜索按钮。现在我想了解如何最好地放置各层来访问此 jar 进行调用并显示结果。
以下是我的处理方法
我有一个 SearchBean
,它是 JSF 托管 bean。
我有三个类 CarSearchRequest
、 PushBikeSearchRequest
和 BoatSearchRequest
它们每个都有一个 search() 方法。
然后我有三个无状态会话 bean CarDAO
、 PushBikeDAO
和 BoatDAO
各一个这些 DAO 之一会调用给定 jar 中的方法,例如 CarDAO
将调用返回 car 等的 API。
上面的三个 Request 类中的每一个都有一个私有的默认构造函数,并且仅公共构造函数采用相应的 DAO 作为参数。
然后,在我的 SearchBean
中注入三个 EJB DAO 后,在 @PostConstruct
方法中,我初始化所有三个 Request 对象并将 DAO 传递给它们的构造函数。
屏幕上的搜索按钮然后调用 searchBean 的 search() 方法,就像这样
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 ...
}
我讨厌这个搜索中的 if 语句,一个好的设计不应该需要这个检查。
我不确定,但从请求对象调用 dao search 听起来是反模式。这是一个好方法吗?
我似乎正在创建 6 个类(3 个请求对象和 3 个 DAO),这听起来又不对。似乎无法摆脱泛型,因为每个搜索实现都是不同的。
有人可以给我更好的方法或任何相关建议吗?
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的建议与 CDI 有关。您为什么决定不使用它?如果不使用 CDI,您将失去 JEE6 在 JSF Web 开发中提供的两项最基本的新服务:上下文和依赖注入。您可以专门使用 EJB 3.1,但最终不会得到最简单的解决方案。我会从 CDI 开始,然后如果我需要其他需求,如事务、jmx、池化等,那么 EJB 就会来拯救你
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