简体   繁体   English

当两个不同的检票口应用程序共享通用功能时,我如何重用代码

[英]How can I reuse code when two different wicket applications share common functionality

I have a Wicket AuthenticatedWebApplication which has several pages and features that need to be reused in a new AuthenticatedWebApplication that I have to develop.我有一个 Wicket AuthenticatedWebApplication,它有几个页面和功能需要在我必须开发的新 AuthenticatedWebApplication 中重用。

I am using Wicket 1.4, Spring and Hibernate.我正在使用 Wicket 1.4、Spring 和 Hibernate。

Both applications will even share the same look (except for Application logo) which is now implemented in a base page.两个应用程序甚至将共享相同的外观(应用程序徽标除外),现在在基本页面中实现。

Has anyone had a similar experience?有没有人有类似的经历? I definitely don't want to recur to copy-paste code because the common functionality implements a workflow process which can and will change.我绝对不想重复复制粘贴代码,因为通用功能实现了一个可以并且将会改变的工作流程。

What can I do to keep my applications modular, and achieve my goal?我可以做些什么来保持我的应用程序模块化并实现我的目标?

This is the main point of component based frameworks.这是基于组件的框架的要点。 Put the common code (components, behaviors, base classes as session, application, ...) in a separate java project (.jar).将公共代码(组件、行为、基类作为 session、应用程序等)放在单独的 java 项目 (.jar) 中。 Later depend on this project in the specific.war projects (put the.jar in WEB-INF/lib).以后在具体的.war项目中依赖这个项目(把.jar放在WEB-INF/lib中)。 Voila!瞧!

My company does this all the time.我的公司一直这样做。 We have a core package that holds the base UserApplication, User accounts, login, authentication, etc. Then, every project we develop extends this base package.我们有一个核心 package,它包含基本的 UserApplication、用户帐户、登录、身份验证等。然后,我们开发的每个项目都扩展了这个基本 package。 There is some duplication - eg almost all of the configuration files look identical in each - but each one has it's own theme directory that supplies the markup, customized to the look and feel of the application.有一些重复——例如几乎所有的配置文件看起来都一样——但每个都有它自己的theme目录来提供标记,根据应用程序的外观和感觉进行定制。

Some suggestions as you do this:执行此操作时的一些建议:

  1. The core application should have a fair number of getXPanel() methods that each sub-application overrides.核心应用程序应该有相当数量的getXPanel()方法,每个子应用程序都会覆盖这些方法。 For example, getHeaderPanel()例如, getHeaderPanel()
  2. Use a "BasePage" class that everyone extends.使用每个人都扩展的“BasePage”class。 This is where you set up your overall look-and-feel, overridden in sub-application theme folders, and make heavy use of <wicket:extend> features.这是您设置整体外观、在子应用程序主题文件夹中覆盖并大量使用<wicket:extend>功能的地方。 Also a good place to put your jQuery import, ec.也是放置 jQuery 进口等的好地方。
  3. Keep in mind that markup is easily overridden.请记住,标记很容易被覆盖。 Your sub-application doesn't need to create java extensions of pages in order to change the logos.您的子应用程序不需要创建页面的 java 扩展来更改徽标。 Just use different markup.只需使用不同的标记。

Each of our applications is divided into at least 4 modules.我们的每个应用程序都至少分为 4 个模块。 For example:例如:

  1. base - Wicket dependency, basic event logging base - Wicket 依赖项,基本事件记录
  2. data - UserApplication, AdminPage, User hibernate obect.数据 - UserApplication、AdminPage、用户 hibernate 对象。 Each page has its own markup, but is usually overridden.每个页面都有自己的标记,但通常会被覆盖。
  3. science - A core project with a lot of code for displaying a science textbook. science - 一个包含大量代码的核心项目,用于显示科学教科书。 ScienceApplication extends UserAppication . ScienceApplication extends UserAppication
  4. foundations - A theme specific implementation for elementary students FoundationsApplication extends ScienceApplication Foundations - 针对小学生的主题特定实现FoundationsApplication extends ScienceApplication
  5. inquiry - A different theme specific implementation for high school students InquiryApplication extends ScienceApplication查询 - 针对高中生的不同主题特定实现InquiryApplication extends ScienceApplication

Our two science applications have different headers and even a few different pages, but ScienceApplication has a those methods I described above.我们的两个科学应用程序有不同的标题,甚至有几个不同的页面,但ScienceApplication有我上面描述的那些方法。

Judging from your other comments and answers:从您的其他评论和答案来看:

  1. Refactor your application and push all common code to (abstract) base classes.重构您的应用程序并将所有通用代码推送到(抽象)基类。
  2. Move these to a new project and set the BuildPath of your 2 new application-projects to require the base package.将这些移动到一个新项目并将您的 2 个新应用程序项目的 BuildPath 设置为需要基本 package。
  3. Extend your base classes to implement the changing functionality.扩展您的基类以实现不断变化的功能。

Depending on your current implementation, you can change your logo by implementing two different imageresources or by providing different models from your new projects to your basebasepage or you could put the image-url into your properties and supply different propertyfiles in your applications.根据您当前的实现,您可以通过实现两个不同的图像资源或通过将新项目中的不同模型提供给基本页面来更改您的徽标,或者您可以将图像 URL 放入您的属性并在应用程序中提供不同的属性文件。 The same is valid for databases or tables... For example with JPA you colud push all the global used entities to your base and implement two different user-entities using different table names.这同样适用于数据库或表......例如,使用 JPA 您可以将所有全局使用的实体推送到您的基础并使用不同的表名实现两个不同的用户实体。 You could even use a shared abstract baseUser-entity to reduce code duplication there.您甚至可以使用共享的抽象 baseUser-entity 来减少那里的代码重复。

From your question I guess your main concern is about pages.根据您的问题,我猜您主要关心的是页面。 Then this is my suggestion: First, you should specify which parts of the pages can change from one application to another application.那么这是我的建议:首先,您应该指定页面的哪些部分可以从一个应用程序更改为另一个应用程序。 Then you have to take out the data of these parts to get the templates.然后你必须取出这些部分的数据才能得到模板。 Now you should decide (based on your requirement) how you want to store data (eg, in xml files, DB).现在您应该(根据您的要求)决定如何存储数据(例如,在 xml 文件、DB 中)。 Now you can compile your pages from templates online or offline based on your needs.现在,您可以根据需要从在线或离线模板编译您的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM