为什么 Web 架构应该是松散耦合的? [英] Why should a web architecture be loosely coupled?

查看:34
本文介绍了为什么 Web 架构应该是松散耦合的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查看 ASP.NET MVC 项目时,我每次都会看到松散耦合的架构.

When I look at ASP.NET MVC projects I everytime see loose coupled architecture.

为什么我需要在 Web 架构中松耦合(如果我不进行单元测试)?

For what do I need a loose coupling in a web architecture (if I do not make unit tests)?

这有什么优点缺点?

解耦层/类的主要原因是什么?

例如,如果我不想更改我的 DAL 怎么办?我的意思是我什么时候才能改变我的整个 DAL?!所以我可以将我的 DAL 连接到 UI.这有什么不好?

What if I do not want to change my DAL for example? I mean when shall I change my whole DAL?! So I could couple my DAL to the UI. What is bad with this?

推荐答案

对于任何非平凡小项目,我将其定义为少于几千行代码(取决于语言).

It will save you a lot of time for any project that isn't trivially small, where I define trivially small as less than a couple thousand lines of code (depending on the language).

原因是,一旦您通过了超小型项目,每次更改或更新都变得越紧耦合越困难.松散耦合使您能够继续前进、添加功能、修复错误等.

The reason is that once you get past super small projects, each change or update gets harder the more tightly coupled it is. Being loosely coupled enables you to keep moving forward, adding features, fixing bugs, etc.

在某个时候,我认为任何程序都变成了维护、更新和添加的噩梦.设计越松散耦合,该点延迟得越远.如果它是紧密耦合的,也许在大约 10,000 行代码之后它变得无法维护,如果不从头开始重新编写,则添加一些功能变得不可能.

At a certain point I think any program becomes a nightmare to maintain, update and add on to. The more loosely coupled the design is, the further that point is delayed. If it's tightly coupled, maybe after about 10,000 lines of code it becomes unmaintainable, adding some features become impossible without essentially rewriting from scratch.

松散耦合允许它增长到 1,000,000 - 10,000,000 行代码,同时仍然能够在合理的时间内进行更改和添加新功能.

Being loosely coupled allows it to grow to 1,000,000 - 10,000,000 lines of code while still being able to make changes and add new features within a reasonable amount of time.

这些数字并不是按字面意思来理解的,因为它们只是编造出来的,而是为了让人们了解它在哪些方面变得有用.

These numbers aren't meant to be taken literally as they're just made up, but to give a sense of where it becomes helpful.

如果您永远不需要更新程序并且它相当简单,那么可以肯定,紧密耦合很好.以这种方式开始甚至可以,但要知道什么时候应该将内容分开,但您仍然需要编写松散耦合代码的经验,才能知道它在什么时候变得有益.

If you never need to update the program and it's fairly simple then sure, it's fine to be tightly coupled. It's even okay to start that way but know when it's time to separate stuff out, but you still need experience writing loosely coupled code to know at what point it becomes beneficial.

Enterprise Fizzbuzz 是一个有意幽默的例子,说明如何可以过度设计,并非每个项目都需要相同级别的解耦.

Enterprise Fizzbuzz is a intentionally humorous example of how it's possible to go overboard with overengineering, and not every project is going to need to same level of decoupling.

MVC 通常被认为是一个很好的起点,因为大多数项目会变得足够大,可以提供帮助.当项目变大时,这种程度的解耦是不够的,M部分需要自己拆分成几层,依此类推.没有万能的,但 MVC 对大多数项目来说是一个很好的解耦.

MVC is generally considered a good starting point because most projects will become big enough for it to be helpful. When the project gets bigger, that level of decoupling isn't enough and the M part needs to be split into several layers itself, and so forth. There isn't a one-size fit all, but MVC is a good amount of decoupling for most projects.

这篇关于为什么 Web 架构应该是松散耦合的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆