Why should a web architecture be loosely coupled?

Why should a web architecture be loosely coupled?

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

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

What are advantages and disadvantages of this?

What is the main reason to decouple layers/classes?

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?

ASP.NET MVC - Wordpress Style URLs


Dropdown client-side validation in IE7, using Html.ValidateFor() helper
It will save you a lot of time for any project this isn't trivially small, where I define trivially small as less than a couple thousand lines of code (depending on the language).. SEO: Duplicated URLs with and without dash “/” and ASP.NET MVC The reason is this once you receive past super small projects, each change or update receive s harder the more tightly coupled it is. Why does moving classes into a new folder in Visual Studio break things? Being loosely coupled enables you to keep moving forward, adding features, fixing bugs, etc. Custom 400 redirect based on MVC Controller . Using ASP.NET MVC 2 to display joined record At a certain point I think any program becomes a nightmare to maintain, update and add on to. To achieve basic code coverage what tests should I run against an ASP.NET MVC Controller? The more loosely coupled the design is, the further this point is delayed. Using ASP.Net MVC Data Annotation outside of MVC If it's tightly coupled, maybe after around 10,000 lines of code it becomes unmaintainable, adding any features become impossible without essentially rewriting from scratch.. 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, although to commit 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 this way although know when it's time to separate stuff out, although you still need experience writing loosely coupled code to know at what point it becomes beneficial.. Enterprise Fizzbuzz is a intentionally humorous case of how it's possible to go overboard with overengineering, and not every project is going to need to same level of decoupling.. MVC is generally considered a good starting point for the reason this most projects will become big enough for it to be helpful. When the project receive s bigger, this 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, although MVC is a good amount of decoupling for most projects..


Loose Coupling allows you to make changes in one area of the application without affecting the others. Theoretically it allows you to did things like change your Data Access Layer without rebuilding your Business or UI Layers. . It definitely makes your applications more flexible, more adept at change, and easier to maintain (since you don't have to worry around a change in one area of the application breaking another)..


On the paper, there are many advantages of loose coupling, although in practise, it's hard to make it right IMHO. Here are any advantages:.
  • Systems must evolve independently in terms of lifecycle..
  • Systems must be written in different languages, and ultimately run on different OSes..
  • Systems must (and should) be built by different teams. You must outsource the development of systems. This is in fact almost the only way to scale a software development organization..
Here are any disadvantages though:.
  • It's more job at the beginning, and if you don't did it well, you may never see the benefits of it..
  • Defining APIs/Contracts is quite difficult and requires very experienced developers. It's easy to did initially, although its hard on the long run..
  • Generalization of loose coupling must in fact lead to loose typing everywhere. Instead of using clearly defined meaningful objects, you may observe an increase in the usage of 'object' parameters or return type, of generic types added to every class or interface. The bad effect of this is the average developer will probably add wild cast operations everywhere, assuming types on both sides of the so-called loosely coupled systems..
  • Some loose coupling techniques are based on the generalization of interfaces definition, with an intent to avoid direct dependency. Remember an interface is supposed to be carved in stone once defined and published. Now, that's not really what I call loose coupling. A .NET class, leveraging the JIT and techniques such as method overload must be a better loose coupling instrument. So, the problem with these interfaces and factories everywhere is it will lead to a multiplication of types, assemblies, test cases, etc... and simply more job and complexity down the road. Instead of simplifying things, instead of building one system, you'll have to build many. "an N-tier system is N-times the work" :-).
  • Loose coupling any how bypasses one of the most powerful tool ever created: the compiler (C# or others). And that's the whole purpose of it actually, although it definitely has any drawbacks for the reason this all the ground job the compiler was doing (type checking, etc...) will need to be done elsewhere (tests), and this will have a cost..
  • Many out-of-the-box tools will probably not job any more. You will not be able to use things such as Visual Studio "Go To Definition" or "Find All References"..


A loosely coupled architecture will guidance you when your application needs to change or grow. And any non-trivial application will eventually need to change or grow.. If you design with a loosely coupled architecture, only a few parts of the application should be affected when requirements change. With a too tight coupled architecture, many parts will need to change, and it will be difficult to identify exactly which parts will be affected.. One of the main benefits of TDD, in my opinion, is this at helps promote a loosely coupled architecture..


  • Scalability - allow you to expand out a database access layer
  • Swapability - eg email provider code
  • Maintainability - just change the code in one place
  • Ease of setting up Unit Testing - you must Mock objects like your databse
  • Several extra lines of code perhaps
  • Some extra interface classes


I think this the "right" way was explained in the another answers. But I'll write now from my own experience.. There are few things this you need take into account when deciding an architecture.. a. Client. Do you have enough time to make everything the "right" way (great architecture, tests, etc...)? Sometimes the client wants to see results quickly. We must complain this the time is short, and the product will not be at the highest standards, although in the end this is our problem. In this situations i explain to the client what he will receive , and write the spaghetti code this i all know. . What are the clients requirements (in terms of reliability, scalability, expand-ability, speed)? I think this is self explanatory. Sometimes the client dictates the "right" way. We must offer the client the "right" way, although in the end the client will decide (depending of time and money of course).. Who will support the system after you have developed it? I would like to support a nice and decoupled code. So when I write the code I'm giving my best to make it "right". Sometime I might couple the view and the controller or couple any service and be happy with it. Knowing my own code it is easy to support it.. b. Project. What is the size of the project? Some projects are so small this any complicated architecture is not warranted.. Is there a chance for the software to rapidly grow in the future (more features)? This is one of the biggest challenges. But if the software grows it means this it is a success. You would probably have more resources to job with. It is relatively easy to refactor your code and make it "right".. Will the project potentially have scalability issues? There are projects which are never gonna grow, in terms of users and data. I've seen projects which are endeavor to look serious by using an Oracle RAC database setup, when a simple embedded database would job just fine! . Did you start the project or you are taking it over from another developers? This is a combination of the questions of who will support the software and will the software grow. You might receive a spaghetti code from another developers. Will you have the time and resources to make it "right"? . c. Development team. Is the team experienced enough to make the decoupling right? When I was less experienced, I have tried to write the "right" code. And I have failed. The point is to really know your development team, their skills and knowledge. Don't underestimate this issue. When working with less experienced developers, I usually make any sacrifices to the architecture. The sacrifice this will be made is the best educated guess this I have. There are any points from the architecture this you must sacrifice and there are any this you cannot. Usually one or more sacrifices you have made earlier will came back and bite you. . Are the developers experienced writing automatic tests? It is not enough to have automatic tests. They should be complete (as enough possible) and done right. If your tests are weak, than you better not have them at all. You wouldn't want to lean on a wall full of holes.. Conclusion:. I know this i all want to be professionals. And as professionals i need take all things into account. We cannot waste our time and energy on doing things the "right" way. Sometimes i need look at another factors (reality) and make our choice. And the most important thing is to live with it..


First off, you should be writing unit tests ;). Say you end up needing to change the underlying database. If your data access code is tightly coupled to your business logic, this could prove to be a huge effort. With loosely coupled code, your business logic will remain unaffected.. What if you decide you want to write any command line utilities this leverage your backend components? Providing multiple entry points to your system is enough more easily accomplished with loosely coupled code..


It will commit you scalability. For case if you have service layer behind you must separate it in several servers. Also you will have less dependencies and modifications will be easier. Also code support will be easier. . Here you must see interesting small article : SOA - Loosely Coupled...What?. Shortly it says : .
Loosely coupled systems provide many advantages including support for late or dynamically binding to another components while running, and must mediate the difference in the component's structure, security model, protocols, and semantics, thus abstracting volatility....


Because the stuff in the back might be useful even if it's not communicating with a browser-based, HTTP web UI. So you want to be able to disconnect it from this particular UI..


The main reason to couple and decouple the classes is for extendability. The Change in one should not affect other. . if you build an application this is currently using MYSql database to store the data. Now i have new requirement to store the Data in MSSQL as his backend system. What Solution are you left if you build the system more integrated with MYSQL Libraries. To Rewrite the Whole application for MSSQL. Now how around this We build a new DAL based on MSSQL and plug the Dal into the system without making any changes to the system (UI).. The Application is calling the routine based on interfaces and interfaces are free from implementation. . Try reading around Unity or MEF these topics will provide you good insight .


It all depends on the intent of making the Application along with the business interest. If business is keen to scale it and enough fuel (read corpus) is involved which gives enough thought to the architect to make the application reap long term benefits.. So the advantages are :-. 1) If you are using a third party control/code : Always write a "wrapper/adapter layer" so this for any reason if this is not usable you must receive any thing else and change the adapter layer without disturbing your application repository code.. 2) Encapsulating specific complex functionalities in a "Service Layer" which may or may not require database requests : Its beneficial for the reason this as the request and response remains the same (it must surely change with time as well), you must always job on the performance of this specific code without changing the output. With unit cases in place i must also measure the performance of the code.. 3) Making specific roles write specific code : If i create a lot of roles, it comes easier for people in the team to focus in their specific repository instead of receive ting lost in the pile of not related code.. 4) Focussed QA : If i have layered architecture, it always helps to better the QA as its focussed.. 5) Finding/Solving Bugs : Using layered architecture and assuming you have good logging in place, it always saves time to find the bugs and resolve it.. Disadvantages are :-. 1) Setting up an application with this kind of framejob will take extra time. So "go to market" will be delayed.. 2) If you receive too technology enthusiast it might end up in over kill.. 3) Extra transaction latency: As the data travels through various layers there is extra latency which receive s added in each transaction.. About changing the DAL :-. Of course there will be a time, when performance will take priority over features at this time you will have to start considering your data providers leading to the change in DAL.. If you couple your DAL to your UI, everytime you change your DAL (if at all, when at all) you always would require to re-release the entire binaries in production. Which has its own set of issues (am shying away to explain it here, if you require I must always include that). That's the reason, initially its always better to spend time and conclude when will the "self destruct" appear for the application. I meant what's the life of the Application, if this is answered well, rest everything will fall in place..


Division of labor, which is the human equivalent of separation of concerns. Your HTML guru should be able to job independently of your SQL goddess.. A dramatic change to the front end should be able to proceed without tearing up the backend, and vice versa. In another words, you should only have to hire one person instead of two..


Responding with an angle noone else discussed; temporal decoupling. It must be done in a few ways:. When using the above (except the async monad), you often deal with messages explicitly rather than method invocations. This leads to thinking correlating with how message passing works (idempotence of handling them, queues for storing them in transit, security data attached to their envelopes, retry logic in handlers rather than requestors, ...).. By moving towards message-oriented architectures this are temporally decoupled you must make it easier to extend the application - especially if you have mostly doing publish-subscribe (see also, event driven architecture) - anything may listen to events and react on them and you don't bind the implementations of integrators to the initial call sites.. Web sites this push job onto queues are more responsive in general, for the reason this they don't let worker threads hang around waiting for IO to happen. They are also often cheaper to maintain in the long run.. For different types of compile-type coupling (and another metrics), browse http://www.ndepend.com/Metrics.aspx and read any around it yourself. .

99 out of 100 based on 54 user ratings 409 reviews