如何使架构和代码动态化?

发布于 2024-10-09 16:06:58 字数 248 浏览 0 评论 0原文

我希望使我的数据库架构和应用程序代码尽可能动态,以处理“未知”用例和更改。使用 PHP 和 MySQL 进行开发。现在我不得不两次更改整个架构,包括表和列名称,这意味着开发人员必须返回应用程序代码并修改所有 SQL 查询和表/列名称。因此,为了防止这种情况,我想如果就像我们在具有页面内容、标题栏等动态的页面上所做的那样,例如%variable%,我们可以以某种方式为架构甚至PHP代码函数和类做到这一点吗?重新做所有这样的改变需要几周的时间,而如果是动态的,则可以在一天之内完成。

I want to make my database schema and application code as dynamic as possible to handle "unknown" use cases and changes. Developing in PHP and MySQL. Twice now I have had to change my entire schema including table and column names and this means the developers have to go back to the application code and modify all the SQL queries and table/columns names. So to prevent this I want to if just like we do on pages where we have page content, title bar etc dynamic like a %variable%, can we do it for the schema and maybe even for the php code functions and classes somehow? It takes weeks to re-do all changes like this vs if it is dynamic it can be done in under a day.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞旧 2024-10-16 16:06:58

首先,祝您新年快乐(无论您的编码问题如何:))。现在,我要向您提出的建议几乎适用于所有开发语言,但我认为 PHP 由于缺乏 OO 开发而受到的影响最大。

更改架构和功能的主要问题是架构的定义。为了拥有一个健壮的架构,您应该:

  • 识别组成应用程序的类
  • 创建与这些类通信的接口
  • 从数据类中抽象出您的业务(域)类

考虑使用(如果您尚未使用)ORM 框架映射您的数据库,因为您使用的是 PHP,所以我建议 Propel学说,非常棒。请注意不要过度依赖您的 ORM 框架并将其转换为您的业务模型,您可以查看 这篇博文 我就这个主题进行了讨论。

希望我能帮忙,
大卫

First of all, may you have a happy new year (regardless of your coding issues :) ). Now, what I'm going to proposse to you applies to nearly any development language, but I pressume that PHP suffers this most due to the lack of OO development.

The main issue with changin schemas and functions is the definition of your architecture. To have a robust architecture, you should:

  • Identify the classes that compose your application
  • Create the interfaces to comunicate with these clases
  • Abstract your business(domain) classes from your data classes

Consider using (if you are not using it already) an ORM framework for mapping your database, since you are using PHP, I would suggest Propel or Doctrine, which are wonderful. Beware of depending too much on your ORM framework and converting it into your business model, you can check out this blog post I made talking on the subject.

Hope I can help,
David

牵你手 2024-10-16 16:06:58

您处于哪个发展阶段?您是在谈论重新设计还是只是重构名称等等?一些想法:

  1. 如果您处于开发的早期阶段,也许只是想了解设计的具体形式,那么重写您的概念验证应用程序是很自然的事情。

  2. 如果您正在生产中,并且您的设计确实发生了变化(随着用户需求的成熟 - 这在现实生活中发生),那么您的旧应用程序现在肯定已经过时了,无论如何都必须重写。

  3. 如果您只是重命名事物,而不是真正重新设计,那么您真的需要这样做吗?表、列、类和变量等内部事物的名称不需要仅仅因为用户可见的术语发生变化而更新。

基本上我是说,像你设想的那样的动态设计让我觉得构建的基础很差。

但这并不是说您不能拥有灵活的设计。最简单的情况:将数据库规范化为第三范式通常会带来灵活性,因为您可以添加列、创建新关系等。对于具有明确定义的接口的小类也是如此。

假设您想要一个强大的设计来构建您的系统,我建议您进行灵活的设计(艰苦的工作)并且不要寻找按钮式解决方案。 (我不知道我是否理解了你的问题......?)

What stage of development are you at? And are you talking about a redesign or just refactoring names and so on? Some thoughts:

  1. If you're in the early stages of development, perhaps playing around to get a feel for how the design might shape up, then it's natural to rewrite your proof-of-concept application.

  2. If you're in production, and your design really has changed (as user requirements mature - it happens in real life) then surely your old application is now out of date and has to be rewritten anyway.

  3. If you're just renaming things, and not really redesigning, then do you really need to do it at all? Names of internal things like tables and columns and classes and variables don't need to be updated just because the user-visible terminology changes.

Basically I'm saying that a dynamic design such as you envisage strikes me as a poor foundation to build upon.

But that's not to say that you can't have a flexible design. Simplest case: normalising your database to about third normal form generally makes for flexibility, because you can add columns, create new relationships, and so on. Same goes for small classes with well-defined interfaces.

Assuming you want a strong design to build your system on, I recommend that you make a flexible design (hard work) and don't look for push-button solutions. (I wonder whether I've understood your question...?)

柒夜笙歌凉 2024-10-16 16:06:58

您可以在所有脚本的开头包含一个特殊的 PHP 文件,并且该文件可以包含您在整个代码中使用的预定义常量,以代替所有表名称和列名称。然后,每当您需要更改表或列的名称时,您只需在这 1 个 PHP 文件内的 1 个位置进行更改即可。

请参阅此处有关常量的 PHP 文档:
http://php.net/manual/en/language.constants.php

正如您所看到的,常量存在于全局范围内。这意味着它们可以在所有函数和所有类中使用,没有任何问题。

您可能已经了解“include”的使用(用于在所有脚本的开头加载特殊的 PHP 文件):
http://php.net/manual/en/function.include.php

因此,对于例如,如果您想使用动态表和列名称编写一些 SQL,您可以这样做:

在 Constants.php 中:

define("USER_TABLE",  "Users");
define("NAME_COLUMN", "FirstName");

在您的脚本中执行以下操作:

include "constants.php";
...
$sql = "select " . NAME_COLUMN . " from " . USER_TABLE . " where id = ...(etc)";

上面的内容翻译为:

$sql = "select FirstName from Users where id = ...(etc)";

如果您有一天想要更改表和列,您只需要更改constants.php文件。

define("USER_TABLE",  "DifferentUsers");
define("NAME_COLUMN", "DifferentFirstName");

这将自动导致使用此包含文件的所有脚本发生更改。

新的翻译结果现在如下所示:

$sql = "select DifferentFirstName from DifferentUsers where id = ...(etc)";

You could include a special PHP file at the beginning of all your scripts, and this file can contain predefined constants that you use throughout your code in place of all table and column names. Then, whenever you need to change the name of a table or column, you only need to change it in 1 place inside of this 1 PHP file.

See PHP documentation on constants here:
http://php.net/manual/en/language.constants.php

As you can see, constants exist in a global scope. That means they can be used inside of all functions and all classes without any problems.

You probably already know about the use of "include" (for loading the special PHP file at the beginning of all your scripts):
http://php.net/manual/en/function.include.php

So, for example, if you wanted to write some SQL using dynamic table and column names, you can do this:

Inside constants.php:

define("USER_TABLE",  "Users");
define("NAME_COLUMN", "FirstName");

Inside your scripts you do this:

include "constants.php";
...
$sql = "select " . NAME_COLUMN . " from " . USER_TABLE . " where id = ...(etc)";

The above translates to this:

$sql = "select FirstName from Users where id = ...(etc)";

If you someday later want to change the name of the table and column, you only need to change the constants.php file.

define("USER_TABLE",  "DifferentUsers");
define("NAME_COLUMN", "DifferentFirstName");

That will automatically cause the changes throughout all of your scripts that use this include file.

The new translated result would now look like:

$sql = "select DifferentFirstName from DifferentUsers where id = ...(etc)";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文