从应用程序中提取框架的最佳实践是什么?

时间:2021-10-20 08:46:45

I've been working on an application for a little over two years and have developed a lot of useful helpers, utilities, features, setup conventions, etc.

我已经在应用程序上工作了两年多,并且已经开发了许多有用的帮助程序,实用程序,功能,设置约定等。

I want to start building a similar application and think I could reuse a lot of the functionality built into the existing application with the new application.

我想开始构建一个类似的应用程序,并认为我可以使用新的应用程序重用现有应用程序中内置的许多功能。

I've always heard is it best to extract a framework from an application but not heard much about best practices on going about it. It's even better knowing changes to the framework could benefit all the similar applications I will be building (there could be a dozen).

我一直听说最好是从应用程序中提取一个框架,但没有听说过关于它的最佳实践。更好的是,了解框架的更改可能会使我将要构建的所有类似应用程序受益(可能有十几个)。

Where do I start? Any best practices? Any pitfalls or areas to watch out for?

我从哪里开始?任何最佳做法?是否有任何陷阱或需要注意的方面?

EDIT: I should clarify a bit, I'm not doing this for anyone but myself. I'm developing similar applications for different industries so the core is 90% the same, the differences are in the details.

编辑:我应该澄清一下,除了我自己,我不会为任何人这样做。我正在为不同行业开发类似的应用程序,因此核心是90%相同,不同之处在于细节。

4 个解决方案

#1


Here's what we at Starling Software have learned from doing this for QAM and QWeb.

以下是我们Starling Software从QAM和QWeb中学到的内容。

Our approach is to treat this as a refactoring job spread across all the projects using the framework or proto-framework. We separate the framework code within each project into something that's built separately, so that, e.g., src/myapp contains the application-specific code, and src/qam contains the framework itself. Each project has its own copy of the application-specific code, and there's also a separate project that contains just the latest version of the framework itself. When we identify something within a specific project that wants to be in the framework, we:

我们的方法是将此作为使用框架或原型框架分布在所有项目中的重构工作。我们将每个项目中的框架代码分成单独构建的内容,以便例如src / myapp包含特定于应用程序的代码,而src / qam包含框架本身。每个项目都有自己的特定于应用程序的代码副本,还有一个单独的项目,它只包含框架本身的最新版本。当我们在特定项目中识别想要在框架中的某些内容时,我们:

  1. refactor within the project to generalize the code (based on our understanding of both this application and all the others using the framework);
  2. 项目中的重构来概括代码(基于我们对此应用程序和使用框架的所有其他人的理解);

  3. refactor within the project to move the genealized code from the application-specific part to the framework part;
  4. 项目内的重构,将基因化代码从特定于应用程序的部分移动到框架部分;

  5. apply this update to the project that contains the "master copy" of the framework;
  6. 将此更新应用于包含框架“主副本”的项目;

  7. merge the changes from that master copy into other projects that are also using the framework; and then
  8. 将来自该主副本的更改合并到也使用该框架的其他项目中;然后

  9. refactor within the other projects to use that part of the framework rather than their similar application-specific code.
  10. 其他项目中的重构使用框架的该部分而不是类似的特定于应用程序的代码。

This requires a fair amount of discipline to move the changes around quickly. If you've just finished developing a new feature and are immediately bringing it into other projects, the integration is easy. Projects that don't get updated for some time become much more difficult to update to use the latest version of the framework. If you don't bring changes into the master copy quickly, you may end up with different (and worse yet, conflicting) changes in the framework copies within two projects, and the merge can become quite a pain. You definitely want to update any particular project to the latest version of the framework before you start changing the framework.

这需要相当多的纪律来快速推动变革。如果您刚刚完成了一项新功能的开发并立即将其引入其他项目,那么集成很容易。一段时间内未更新的项目变得更难以更新以使用最新版本的框架。如果您没有快速地将更改带入主副本,您可能最终会在两个项目中的框架副本中发生不同(更糟糕的是,相互冲突的)更改,并且合并可能会变得非常痛苦。在开始更改框架之前,您肯定希望将任何特定项目更新到最新版本的框架。

Doing this needs a certain amount of practical support in terms of tools and so on. You'll want a way to move changes back and forth between the various copies of the framework easily and quickly. We have a custom tool to do this (qu), but I would imagine that one could also use a revision control system, particularly a distributed one that works by moving patches around, to help with this.

这样做需要在工具等方面提供一定的实际支持。您将需要一种方法,可以轻松快速地在框架的各个副本之间来回移动更改。我们有一个自定义工具来执行此操作(qu),但我想也可以使用修订控制系统,特别是通过移动补丁来实现的分布式控制系统,以帮助实现此目的。

Having a comprehensive test suite for each application helps greatly; I'm not sure I'd want to attempt this without that.

为每个应用程序提供全面的测试套件有很大帮助;我不确定如果没有那个我想要尝试这个。

For sanity, it's important to keep the changes small. Again, it's all a matter of how hard it is to merge and move updates. If it's always quick, and something you've been working on very recently, things stay easy. The large the changes are, and the longer it's been since you worked on that bit of the framework, the harder things become.

为了理智,保持变化很小很重要。再一次,合并和移动更新的难易程度。如果它总是很快,而且你最近一直在做的事情,事情就变得容易了。这些变化越大,自从你开始研究框架之后的时间越长,事情就变得越来越困难。

#2


See Extracting Rails from Basecamp for David Heinemeier Hansson's take on how Rails came to be, from its origins as the framework used in Basecamp. Given the popularity of Rails, you may learn a thing or two from how DHH pulled this off. :-)

请参阅从Basecamp中提取Rails,以了解David Heinemeier Hansson对Rails如何形成的看法,从它作为Basecamp中使用的框架的起源。考虑到Rails的普及,你可以从DHH如何解决这个问题中学到一两件事。 :-)

(Well, maybe not so much a how, as a why. But still entertaining. :-))

(好吧,也许不是那么多,为什么。但仍然很有趣。:-))

#3


Put all your code in a pile next to your desk. Start from a blank page, writing the code you wish you could write using your desired framework. Then use your existing code for components, and write the framework that would let you use your desired code.

将所有代码放在桌子旁边。从空白页开始,编写您希望使用所需框架编写的代码。然后使用现有的代码组件,编写可以让您使用所需代码的框架。

#4


I've done this before, while it was a fun and rewarding experiance I would have to say I would NOT do it again. Here are my reasons...

我以前做过这个,虽然这是一个有趣而有益的经历我不得不说我不会再做了。这是我的理由......

1) people expect you to support the framework. This was the number 1 challange I faced. Poeple came to me for enahancements, questions and bug fixes. Well this was really tough because I had moved on to other projects and did NOT have time to support them.

1)人们希望你支持这个框架。这是我面临的第一个挑战。人们来找我进行改进,问题和修复错误。这真的很难,因为我已经转移到其他项目,没有时间支持他们。

So make sure you have a clear support plan in place for the future. How will the application be maintained? Who will maintain it? How much time will be required to maintain it? And MOST important... who is going to pay for this maintenance?

因此,请确保您有一个明确的支持计划,以便将来使用。如何维护申请?谁来维护它?维持它需要多长时间?最重要的是......谁来支付这笔费用呢?

2) developers hate to be told what to do or constrained. The framework I wrote simplified Data Access Layer for VB developers. It created a nice easy to use set of routines that would work no matter what MS did to change out the DB access routines. Well certain developers didn't like to be told to use a framework so they would bitch and moan. Frankly I got sick of hearing it and it caused some ill will towards co-workers.

2)开发人员讨厌被告知做什么或受限制。我为VB开发人员编写了简化数据访问层的框架。它创建了一组易于使用的例程,无论MS如何更改数据库访问例程,它都能正常工作。好吧,某些开发人员不喜欢被告知使用框架,所以他们会婊子和呻吟。坦率地说,我厌倦了听到它,这给同事带来了一些恶意。

So make sure you got some thick skin and can market WHY people should use your framework.

因此,请确保你有一些厚厚的皮肤,并可以推销为什么人们应该使用你的框架。

#1


Here's what we at Starling Software have learned from doing this for QAM and QWeb.

以下是我们Starling Software从QAM和QWeb中学到的内容。

Our approach is to treat this as a refactoring job spread across all the projects using the framework or proto-framework. We separate the framework code within each project into something that's built separately, so that, e.g., src/myapp contains the application-specific code, and src/qam contains the framework itself. Each project has its own copy of the application-specific code, and there's also a separate project that contains just the latest version of the framework itself. When we identify something within a specific project that wants to be in the framework, we:

我们的方法是将此作为使用框架或原型框架分布在所有项目中的重构工作。我们将每个项目中的框架代码分成单独构建的内容,以便例如src / myapp包含特定于应用程序的代码,而src / qam包含框架本身。每个项目都有自己的特定于应用程序的代码副本,还有一个单独的项目,它只包含框架本身的最新版本。当我们在特定项目中识别想要在框架中的某些内容时,我们:

  1. refactor within the project to generalize the code (based on our understanding of both this application and all the others using the framework);
  2. 项目中的重构来概括代码(基于我们对此应用程序和使用框架的所有其他人的理解);

  3. refactor within the project to move the genealized code from the application-specific part to the framework part;
  4. 项目内的重构,将基因化代码从特定于应用程序的部分移动到框架部分;

  5. apply this update to the project that contains the "master copy" of the framework;
  6. 将此更新应用于包含框架“主副本”的项目;

  7. merge the changes from that master copy into other projects that are also using the framework; and then
  8. 将来自该主副本的更改合并到也使用该框架的其他项目中;然后

  9. refactor within the other projects to use that part of the framework rather than their similar application-specific code.
  10. 其他项目中的重构使用框架的该部分而不是类似的特定于应用程序的代码。

This requires a fair amount of discipline to move the changes around quickly. If you've just finished developing a new feature and are immediately bringing it into other projects, the integration is easy. Projects that don't get updated for some time become much more difficult to update to use the latest version of the framework. If you don't bring changes into the master copy quickly, you may end up with different (and worse yet, conflicting) changes in the framework copies within two projects, and the merge can become quite a pain. You definitely want to update any particular project to the latest version of the framework before you start changing the framework.

这需要相当多的纪律来快速推动变革。如果您刚刚完成了一项新功能的开发并立即将其引入其他项目,那么集成很容易。一段时间内未更新的项目变得更难以更新以使用最新版本的框架。如果您没有快速地将更改带入主副本,您可能最终会在两个项目中的框架副本中发生不同(更糟糕的是,相互冲突的)更改,并且合并可能会变得非常痛苦。在开始更改框架之前,您肯定希望将任何特定项目更新到最新版本的框架。

Doing this needs a certain amount of practical support in terms of tools and so on. You'll want a way to move changes back and forth between the various copies of the framework easily and quickly. We have a custom tool to do this (qu), but I would imagine that one could also use a revision control system, particularly a distributed one that works by moving patches around, to help with this.

这样做需要在工具等方面提供一定的实际支持。您将需要一种方法,可以轻松快速地在框架的各个副本之间来回移动更改。我们有一个自定义工具来执行此操作(qu),但我想也可以使用修订控制系统,特别是通过移动补丁来实现的分布式控制系统,以帮助实现此目的。

Having a comprehensive test suite for each application helps greatly; I'm not sure I'd want to attempt this without that.

为每个应用程序提供全面的测试套件有很大帮助;我不确定如果没有那个我想要尝试这个。

For sanity, it's important to keep the changes small. Again, it's all a matter of how hard it is to merge and move updates. If it's always quick, and something you've been working on very recently, things stay easy. The large the changes are, and the longer it's been since you worked on that bit of the framework, the harder things become.

为了理智,保持变化很小很重要。再一次,合并和移动更新的难易程度。如果它总是很快,而且你最近一直在做的事情,事情就变得容易了。这些变化越大,自从你开始研究框架之后的时间越长,事情就变得越来越困难。

#2


See Extracting Rails from Basecamp for David Heinemeier Hansson's take on how Rails came to be, from its origins as the framework used in Basecamp. Given the popularity of Rails, you may learn a thing or two from how DHH pulled this off. :-)

请参阅从Basecamp中提取Rails,以了解David Heinemeier Hansson对Rails如何形成的看法,从它作为Basecamp中使用的框架的起源。考虑到Rails的普及,你可以从DHH如何解决这个问题中学到一两件事。 :-)

(Well, maybe not so much a how, as a why. But still entertaining. :-))

(好吧,也许不是那么多,为什么。但仍然很有趣。:-))

#3


Put all your code in a pile next to your desk. Start from a blank page, writing the code you wish you could write using your desired framework. Then use your existing code for components, and write the framework that would let you use your desired code.

将所有代码放在桌子旁边。从空白页开始,编写您希望使用所需框架编写的代码。然后使用现有的代码组件,编写可以让您使用所需代码的框架。

#4


I've done this before, while it was a fun and rewarding experiance I would have to say I would NOT do it again. Here are my reasons...

我以前做过这个,虽然这是一个有趣而有益的经历我不得不说我不会再做了。这是我的理由......

1) people expect you to support the framework. This was the number 1 challange I faced. Poeple came to me for enahancements, questions and bug fixes. Well this was really tough because I had moved on to other projects and did NOT have time to support them.

1)人们希望你支持这个框架。这是我面临的第一个挑战。人们来找我进行改进,问题和修复错误。这真的很难,因为我已经转移到其他项目,没有时间支持他们。

So make sure you have a clear support plan in place for the future. How will the application be maintained? Who will maintain it? How much time will be required to maintain it? And MOST important... who is going to pay for this maintenance?

因此,请确保您有一个明确的支持计划,以便将来使用。如何维护申请?谁来维护它?维持它需要多长时间?最重要的是......谁来支付这笔费用呢?

2) developers hate to be told what to do or constrained. The framework I wrote simplified Data Access Layer for VB developers. It created a nice easy to use set of routines that would work no matter what MS did to change out the DB access routines. Well certain developers didn't like to be told to use a framework so they would bitch and moan. Frankly I got sick of hearing it and it caused some ill will towards co-workers.

2)开发人员讨厌被告知做什么或受限制。我为VB开发人员编写了简化数据访问层的框架。它创建了一组易于使用的例程,无论MS如何更改数据库访问例程,它都能正常工作。好吧,某些开发人员不喜欢被告知使用框架,所以他们会婊子和呻吟。坦率地说,我厌倦了听到它,这给同事带来了一些恶意。

So make sure you got some thick skin and can market WHY people should use your framework.

因此,请确保你有一些厚厚的皮肤,并可以推销为什么人们应该使用你的框架。