Obviously, "Hello World" doesn't require a separated, modular front-end and back-end. But any sort of Enterprise-grade project does.
显然,“Hello World”不需要分离的模块化前端和后端。但任何类型的企业级项目都可以。
Assuming some sort of spectrum between these points, at which stage should an application be (conceptually, or at a design level) multi-layered? When a database, or some external resource is introduced? When you find that the you're anticipating spaghetti code in your methods/functions?
假设这些点之间存在某种频谱,应用程序应该在哪个阶段(概念上或在设计层面)进行多层划分?当引入数据库或某些外部资源时?当你发现你在方法/功能中预期意大利面条代码时?
9 个解决方案
#1
when a database, or some external resource is introduced.
何时引入数据库或某些外部资源。
but also:
always (except for the most trivial of apps) separate AT LEAST presentation tier and application tier
总是(除了最简单的应用程序)分离AT LEAST表示层和应用程序层
see:
#2
Layers are a mean to keep a design loosely coupled and highly cohesive.
层是保持设计松散耦合和高度凝聚力的意思。
When you start to have a few classes (either implemented or just sketched with UML), they can be grouped logically, into layers - or more generally packages, or modules. This is called the art of separating the concerns.
当你开始有几个类(实现或只是用UML草绘)时,它们可以按逻辑分组,分层 - 或者更一般地包或模块。这被称为分离关注点的艺术。
The sooner the better: if you do not start layering early enough, then you risk to have never do it as the effort can be too important.
越快越好:如果你没有尽早开始分层,那么你就有可能永远不会这样做,因为努力可能太重要了。
#3
Here are some criteria of when to...
以下是何时......的一些标准
- Any time you anticipate the need to replace one part of it with a different part.
- Any time you find yourself need to divide work amongst parallel team.
任何时候你预计需要用不同的部分替换它的一部分。
任何时候你发现自己需要在并行团队之间划分工作。
#4
There is no real answer to this question. It depends largely on your application's needs, and numerous other factors. I'd suggest reading some books on design patterns and enterprise application architecture. These two are invaluable:
这个问题没有真正的答案。这在很大程度上取决于您的应用程序的需求,以及许多其他因素。我建议阅读一些有关设计模式和企业应用程序架构的书籍。这两个是非常宝贵的:
Design Patterns: Elements of Reusable Object-Oriented Software
设计模式:可重用面向对象软件的元素
Patterns of Enterprise Application Architecture
企业应用架构模式
Some other books that I highly recommend are:
我强烈推荐的其他一些书籍是:
The Pragmatic Programmer: From Journeyman to Master
务实的程序员:从熟练工到硕士
Refactoring: Improving the Design of Existing Code
重构:改进现有规范的设计
No matter your skill level, reading these will really open your eyes to a world of possibilities.
无论你的技术水平如何,阅读这些都会让你的眼睛真正开启一个充满无限可能的世界。
#5
I'd say in most cases dealing with multiple distinct levels of abstraction in the concepts your code deals with would be a strong signal to mirror this with levels of abstraction in your implementation.
我会说在大多数情况下,处理代码处理的概念中的多个不同抽象级别将是一个强有力的信号,可以在您的实现中用抽象级别来反映这一点。
This does not override the scenarios that others have highlighted already though.
但这并没有覆盖其他人已经强调过的场景。
#6
I think once you ask yourself "hmm should I layer this" the answer is yes.
我想一旦你问自己“嗯我应该分层”,答案是肯定的。
I've worked on too many projects that probably started off as proof of concept/prototype that ended up being full projects used in production, which are horribly written and just wreak of "get it done quick, we'll fix it later." Trust me, you wont fix it later.
我参与过太多项目,这些项目可能最初是作为概念/原型的证明,最终成为生产中使用的完整项目,这些项目写得非常糟糕,只是“快速完成,我们稍后会修复它”。相信我,你以后不会修复它。
The Pragmatic Programmer lists this as the Broken Window Theory.
实用程序员将此列为破窗理论。
Try and always do it right from the start. Separate your concerns. Build it with modularity in mind.
尝试并始终从一开始就做到这一点。分开你的顾虑。在构建它时考虑到模块化。
And of course try and think of the poor maintenance programmer who might take over when you're done!
当然,试着想想那些可能在你完成后接管的维护程序员!
#7
Thinking of it in terms of layers is a little limiting. It's what you see in whitepapers about a product, but it's not how products really work. They have "boxes" that depend on each other in various ways, and you can make it look like they fit into layers but you can do this in several different configurations, depending on what information you're leaving out of the diagram.
从层面来考虑它是一个有点限制。这是你在产品的白皮书中看到的,但产品并不是真正起作用的。它们具有以各种方式相互依赖的“盒子”,并且您可以使它们看起来像适合图层,但您可以在几种不同的配置中执行此操作,具体取决于您从图表中遗漏的信息。
And in a really well-designed application, the boxes get very small. They are down to the level of individual interfaces and classes.
在一个设计精良的应用程序中,盒子变得非常小。它们可以达到各个接口和类的级别。
This is important because whenever you change a line of code, you need to have some understanding of the impact your change will have, which means you have to understand exactly what the code currently does, what its responsibilities are, which means it has to be a small chunk that has a single responsibility, implementing an interface that doesn't cause clients to be dependent on things they don't need (the S and the I of SOLID).
这很重要,因为每当您更改一行代码时,您需要了解您的更改将产生的影响,这意味着您必须准确了解代码当前的作用,它的职责是什么,这意味着它必须是一个具有单一责任的小块,实现一个不会导致客户端依赖于他们不需要的东西的接口(S和I的SOLID)。
You may find that your application can look like it has two or three simple layers, if you narrow your eyes, but it may not. That isn't really a problem. Of course, a disastrously badly designed application can look like it has layers tiers if you squint as hard as you can. So those "high level" diagrams of an "architecture" can hide a multitude of sins.
如果你眯起眼睛,你可能会发现你的应用程序看起来有两到三个简单的层,但它可能没有。这不是一个真正的问题。当然,如果你尽可能地眯眼,那么设计糟糕的应用程序可能看起来像层层。因此,“架构”的那些“高级”图表可以隐藏许多罪恶。
#8
My generic rule of thumb is to at least to separate the problem into a model and view layer, and throw in a controller if there is a possibility of more than one ways of handling the model or piping data to the view.
我的通用经验法则是至少将问题分成模型和视图层,如果有可能有多种方法处理模型或管道数据到视图,则抛出控制器。
(Or as the first answer, at least the presentation tier and the application tier).
(或者作为第一个答案,至少是表示层和应用程序层)。
#9
Loose coupling is all about minimising dependencies, so I would say 'layer' when a dependency is introduced. i.e. a database, third party application, etc.
松散耦合是关于最小化依赖性,所以我会说引入依赖时的“层”。即数据库,第三方应用程序等
Although 'layer' is probably the wrong term these days. Most of the time I use Dependency Injection (DI) through an Inversion of Control container such as Castle Windsor. This means that I can code on one part of my system without worrying about the rest. It has the side effect of ensuring loose coupling.
虽然现在“层”可能是错误的术语。大多数时候,我通过Inversion of Control容器(例如Castle Windsor)使用依赖注入(DI)。这意味着我可以在我的系统的一部分上编码,而不必担心其余部分。它具有确保松耦合的副作用。
I would recommend DI as a general programming principle all of the time so that you have the choice on how to 'layer' your application later.
我会一直推荐DI作为一般编程原则,以便您可以选择如何在以后“分层”您的应用程序。
Give it a look.
看看吧。
R
#1
when a database, or some external resource is introduced.
何时引入数据库或某些外部资源。
but also:
always (except for the most trivial of apps) separate AT LEAST presentation tier and application tier
总是(除了最简单的应用程序)分离AT LEAST表示层和应用程序层
see:
#2
Layers are a mean to keep a design loosely coupled and highly cohesive.
层是保持设计松散耦合和高度凝聚力的意思。
When you start to have a few classes (either implemented or just sketched with UML), they can be grouped logically, into layers - or more generally packages, or modules. This is called the art of separating the concerns.
当你开始有几个类(实现或只是用UML草绘)时,它们可以按逻辑分组,分层 - 或者更一般地包或模块。这被称为分离关注点的艺术。
The sooner the better: if you do not start layering early enough, then you risk to have never do it as the effort can be too important.
越快越好:如果你没有尽早开始分层,那么你就有可能永远不会这样做,因为努力可能太重要了。
#3
Here are some criteria of when to...
以下是何时......的一些标准
- Any time you anticipate the need to replace one part of it with a different part.
- Any time you find yourself need to divide work amongst parallel team.
任何时候你预计需要用不同的部分替换它的一部分。
任何时候你发现自己需要在并行团队之间划分工作。
#4
There is no real answer to this question. It depends largely on your application's needs, and numerous other factors. I'd suggest reading some books on design patterns and enterprise application architecture. These two are invaluable:
这个问题没有真正的答案。这在很大程度上取决于您的应用程序的需求,以及许多其他因素。我建议阅读一些有关设计模式和企业应用程序架构的书籍。这两个是非常宝贵的:
Design Patterns: Elements of Reusable Object-Oriented Software
设计模式:可重用面向对象软件的元素
Patterns of Enterprise Application Architecture
企业应用架构模式
Some other books that I highly recommend are:
我强烈推荐的其他一些书籍是:
The Pragmatic Programmer: From Journeyman to Master
务实的程序员:从熟练工到硕士
Refactoring: Improving the Design of Existing Code
重构:改进现有规范的设计
No matter your skill level, reading these will really open your eyes to a world of possibilities.
无论你的技术水平如何,阅读这些都会让你的眼睛真正开启一个充满无限可能的世界。
#5
I'd say in most cases dealing with multiple distinct levels of abstraction in the concepts your code deals with would be a strong signal to mirror this with levels of abstraction in your implementation.
我会说在大多数情况下,处理代码处理的概念中的多个不同抽象级别将是一个强有力的信号,可以在您的实现中用抽象级别来反映这一点。
This does not override the scenarios that others have highlighted already though.
但这并没有覆盖其他人已经强调过的场景。
#6
I think once you ask yourself "hmm should I layer this" the answer is yes.
我想一旦你问自己“嗯我应该分层”,答案是肯定的。
I've worked on too many projects that probably started off as proof of concept/prototype that ended up being full projects used in production, which are horribly written and just wreak of "get it done quick, we'll fix it later." Trust me, you wont fix it later.
我参与过太多项目,这些项目可能最初是作为概念/原型的证明,最终成为生产中使用的完整项目,这些项目写得非常糟糕,只是“快速完成,我们稍后会修复它”。相信我,你以后不会修复它。
The Pragmatic Programmer lists this as the Broken Window Theory.
实用程序员将此列为破窗理论。
Try and always do it right from the start. Separate your concerns. Build it with modularity in mind.
尝试并始终从一开始就做到这一点。分开你的顾虑。在构建它时考虑到模块化。
And of course try and think of the poor maintenance programmer who might take over when you're done!
当然,试着想想那些可能在你完成后接管的维护程序员!
#7
Thinking of it in terms of layers is a little limiting. It's what you see in whitepapers about a product, but it's not how products really work. They have "boxes" that depend on each other in various ways, and you can make it look like they fit into layers but you can do this in several different configurations, depending on what information you're leaving out of the diagram.
从层面来考虑它是一个有点限制。这是你在产品的白皮书中看到的,但产品并不是真正起作用的。它们具有以各种方式相互依赖的“盒子”,并且您可以使它们看起来像适合图层,但您可以在几种不同的配置中执行此操作,具体取决于您从图表中遗漏的信息。
And in a really well-designed application, the boxes get very small. They are down to the level of individual interfaces and classes.
在一个设计精良的应用程序中,盒子变得非常小。它们可以达到各个接口和类的级别。
This is important because whenever you change a line of code, you need to have some understanding of the impact your change will have, which means you have to understand exactly what the code currently does, what its responsibilities are, which means it has to be a small chunk that has a single responsibility, implementing an interface that doesn't cause clients to be dependent on things they don't need (the S and the I of SOLID).
这很重要,因为每当您更改一行代码时,您需要了解您的更改将产生的影响,这意味着您必须准确了解代码当前的作用,它的职责是什么,这意味着它必须是一个具有单一责任的小块,实现一个不会导致客户端依赖于他们不需要的东西的接口(S和I的SOLID)。
You may find that your application can look like it has two or three simple layers, if you narrow your eyes, but it may not. That isn't really a problem. Of course, a disastrously badly designed application can look like it has layers tiers if you squint as hard as you can. So those "high level" diagrams of an "architecture" can hide a multitude of sins.
如果你眯起眼睛,你可能会发现你的应用程序看起来有两到三个简单的层,但它可能没有。这不是一个真正的问题。当然,如果你尽可能地眯眼,那么设计糟糕的应用程序可能看起来像层层。因此,“架构”的那些“高级”图表可以隐藏许多罪恶。
#8
My generic rule of thumb is to at least to separate the problem into a model and view layer, and throw in a controller if there is a possibility of more than one ways of handling the model or piping data to the view.
我的通用经验法则是至少将问题分成模型和视图层,如果有可能有多种方法处理模型或管道数据到视图,则抛出控制器。
(Or as the first answer, at least the presentation tier and the application tier).
(或者作为第一个答案,至少是表示层和应用程序层)。
#9
Loose coupling is all about minimising dependencies, so I would say 'layer' when a dependency is introduced. i.e. a database, third party application, etc.
松散耦合是关于最小化依赖性,所以我会说引入依赖时的“层”。即数据库,第三方应用程序等
Although 'layer' is probably the wrong term these days. Most of the time I use Dependency Injection (DI) through an Inversion of Control container such as Castle Windsor. This means that I can code on one part of my system without worrying about the rest. It has the side effect of ensuring loose coupling.
虽然现在“层”可能是错误的术语。大多数时候,我通过Inversion of Control容器(例如Castle Windsor)使用依赖注入(DI)。这意味着我可以在我的系统的一部分上编码,而不必担心其余部分。它具有确保松耦合的副作用。
I would recommend DI as a general programming principle all of the time so that you have the choice on how to 'layer' your application later.
我会一直推荐DI作为一般编程原则,以便您可以选择如何在以后“分层”您的应用程序。
Give it a look.
看看吧。
R