简体   繁体   English

组织此项目的“正确方法”是什么?

[英]What's the “correct way” to organize this project?

I'm working on a project that allows multiple users to submit large data files and perform operations on them. 我正在做一个允许多个用户提交大数据文件并对其执行操作的项目。 The "backend" which performs these operations is written in Perl while the "frontend" uses PHP to load HTML template files and determines which content to deliver. 执行这些操作的“后端”是用Perl编写的,而“前端”则使用PHP加载HTML模板文件并确定要传递的内容。 Data is stored in a database (MySQL, SQLite, Oracle) and while there is data which has not yet been acted upon, Perl adds it to a running queue which delivers data to other threads based on system load. 数据存储在数据库(MySQL,SQLite,Oracle)中,并且当尚未处理数据时,Perl将其添加到正在运行的队列中,该队列根据系统负载将数据传递给其他线程。 In addition, there may be pre- and post-processing of the data before and after the main Perl script operates (the specifications are unclear) so I may want to allow these processors to be user-selectable plugins. 另外,在主要Perl脚本运行之前和之后,可能需要对数据进行预处理和后处理(规格尚不清楚),因此我可能希望允许这些处理器成为用户可选的插件。 I had been writing this project in a more procedural fashion but I am quickly realizing the benefit of separating concerns as to limit the scope one change has on the rest of the project. 我以前以一种更具过程性的方式编写了这个项目,但是我很快意识到将关注点分离的好处是可以限制一项更改对项目其余部分的范围。

I'm quite unexperienced with design patterns and am curious what the best way to proceed is. 我对设计模式完全没有经验,并且对最好的进行方式感到好奇。 I've heard MVC thrown around quite a bit but I am unsure of how to apply it. 我已经听说过MVC了很多,但是我不确定如何应用它。 Specifically, what are some good options to structure this code (in terms of design patterns and folder hierarchy)? 具体来说,有什么好的方法可以构造此代码(就设计模式和文件夹层次而言)? How can I achieve this with both PHP and Perl while minimizing duplicated code between languages? 如何在最小化语言之间的重复代码的同时用PHP和Perl做到这一点? Should I keep my PHP files in the top level so I don't have ugly paths in the URL? 我应该将我的PHP文件放在顶层,以便URL中没有难看的路径吗?

Also, if I want to provide interchangeable databases, does each table need its own DAO implementation? 另外,如果我想提供可互换的数据库,每个表是否需要自己的DAO实现?

This is really several questions, but here goes: 这确实是几个问题,但这里有:

MVC MVC

To implement MVC you'll need to have a clear idea of what the Model, View and Controller sections are responsible for. 要实现MVC,您需要清楚地了解Model,View和Controller部分的职责。 You can read about this for yourself , but in this case: 您可以自己了解一下 ,但是在这种情况下:

  • The model should only contain the code which performs operations on the data files, ie the back-end Perl scripts 该模型应仅包含对数据文件执行操作的代码,即后端Perl脚本
  • The view will be the HTML templates only. 该视图将仅是HTML模板。 No PHP logic should be embedded in them except what's necessary to display the page. 除了显示页面所需的内容外,不应在其中嵌入任何PHP逻辑。
  • The controller will be the rest of the application: the parts that connect the PHP front-end to the Perl back-end, and possibly the Perl scripts that poll for new files. 控制器将是应用程序的其余部分:将PHP前端连接到Perl后端的部分,以及可能轮询新文件的Perl脚本。

For every php, html or perl file you create, it should be absolutely clear to which section it belongs, in its entirety. 对于您创建的每个php,html或perl文件,应该绝对清楚其整体属于哪个部分。 Never mix up model, view or controller code in the same file. 切勿在同一文件中混淆模型,视图或控制器代码。

There should be no reason why you can't continue writing procedurally. 应该没有理由不能继续进行程序编写。 You don't necessarily need a framework either; 您也不一定需要框架。 It may help you to slot things into place, but may also take some time to learn. 它可以帮助您将事情放到适当的位置,但也可能需要一些时间来学习。

MVC is a more of a separation of concerns that you need to keep in mind. MVC更像是您需要牢记的关注点分离。 A good way to think about it is: "Can each of these components work separately from the others", eg: 考虑它的一个好方法是:“这些组件中的每一个都可以与其他组件分开工作吗”,例如:

  • can you write a 'mocked up' (example) data file, and have the Perl scripts process it without running any of the PHP code? 您可以编写一个“模拟的”(示例)数据文件,并让Perl脚本在不运行任何PHP代码的情况下对其进行处理吗?
  • can you request an operation from the front-end, and have it deliver all the parameters to a single place, ready for a single Perl routine to pick up, without running and Perl code? 您可以从前端请求一个操作,并让它将所有参数传递到一个地方,以供单个Perl例程使用,而无需运行和运行Perl代码吗?

You shouldn't need to worry about code duplication if the PHP and Perl scripts are doing totally different things (PHP is only setting up users parameters and input files, and Perl is only taking those parameters and files, and outputting new files). 如果PHP和Perl脚本所做的事情完全不同,则不必担心代码重复(PHP仅设置用户参数和输入文件,而Perl仅采用这些参数和文件并输出新文件)。

As for folder hierarchy, if you're not using any existing framework, the most important thing is just that it makes sense, is consistent, and that you document your decisions, eg in a readme file. 对于文件夹层次结构,如果您不使用任何现有框架,那么最重要的是,它有意义,具有一致性,并且您可以将决策记录在文件中,例如在自述文件中。

Ugly URLs 丑陋的网址

You don't need to put your php files in any particular place. 您无需将php文件放在任何特定位置。 Use Apache rewrite rules to make the URLs pretty afterwards. 使用Apache重写规则来使URL变得更漂亮。 ( rewrite rules generator - see the "301 Redirect File" section). 重写规则生成器 -请参见“ 301重定向文件”部分)。 But a good MVC framework will solve this for you. 但是一个好的MVC框架将为您解决这个问题。

User-selectable plugins 用户可选的插件

Be careful not to optimise too early. 注意不要过早优化。 You may be able to develop the new pre/post-processing steps yourself, and just put them in a list for users to select from. 您也许可以自己开发新的预处理/后处理步骤,然后将它们放在列表中以供用户选择。

MVC frameworks are a great tool, for any web developer, that provides well-defined separation indispensable for keeping your code organized. 对于任何Web开发人员而言,MVC框架都是一个很好的工具,它提供了定义清晰的分隔对于使代码井井有条是必不可少的。

For PHP I recommend Zend Framework 对于PHP,我建议使用Zend Framework

Database schema will change depending on what platform you use. 数据库架构将根据您使用的平台而变化。

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

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