Please Explain How Much Rewriting One Should Do and How You Know What Needs Fixed
Should you split your ASP.NET MVC projection into multiple projects?
"Should I divide my ASP.NET MVC project into multiple projects?" That's a question that I get a lot! Almost every week! The curt answer is: NO!
I'yard not entirely certain how this trend started but I've seen some developers split an ASP.Net MVC projection into multiple projects: a web projection containing the presentation logic, plus two additional class libraries, often named [MyProject].BLL and [MyProject].DLL.
Tiers?
Also, information technology's often incorrectly causeless that this structure makes an awarding multi-tier or 3-tier. What is a multi-tier application? Information technology's an awarding whose parts are physically distributed to unlike computers in a network. Web applications are ofttimes inherently multi-tired. In a spider web application nosotros frequently take the following tiers:
- Customer/Presentation Tier: That's the piece running inside the user'southward browser.
- Centre/Application/Logic Tier: That's the part built with ASP.NET MVC (or other similar server-side frameworks) running in a web server.
- Information Tier: That's the database, file organisation or any other kind of storage.
When nosotros're talking about ASP.Cyberspace MVC, we're merely talking about the application or middle tier. Separating an ASP.NET MVC projection into three projects does not result in addition of new tiers in your compages. You lot don't deploy the DAL course library to a unlike computer! Well-nigh of the fourth dimension (if not always) all these 3 projects (Spider web, BLL and DAL) are compiled and deployed in the same process on i automobile; that is your web server. Then, when someone visits your web site, these three DLLs are loaded inside a procedure (or more accurately an AppDomain) managed by IIS.
Layers vs Tiers
Layers and tiers are used interchangeably by some but they are fundamentally different. Layers are about logical separation, tiers are well-nigh concrete separation: distributing pieces of a software application to unlike computers.
Layer is something conceptual in a developer's head. A class library is not a layer, neither is a folder. Y'all can put classes in a folder or a class library that belong to different layers and be dependent upon each other. This is a sign of bad architecture and coupling. Putting these classes under a folder or a class library like BLL and DAL does non immediately result in software with clean architecture and good separation of concerns.
Despite that, my statement is that these folders (BLL and DAL) can and should reside in the primary web project and moving them into a split up class library does non add together any values. It doesn't magically create layers in your applications.
In that location are 2 cases for splitting a projection into smaller projects: reusability and independently deploying those projects.
Reusability
I reason for separating a project into multiple class libraries is re-usability. I've yet to encounter the BLL or DAL office of a web application re-used in another application. This is what text books from 90s used to tell us! But most if not all mod applications are too specific and even in the same enterprise I've never seen the same BLL or DAL parts re-used across multiple applications. Most of the time what you lot have in those class libraries is purely to serve what the user sees in that particular application, and information technology's non something that can be easily re-used (if at all).
Deployability
Another reason for separating a project into multiple class libraries is about deployability. If you want to independently version and deploy these pieces, it does makes sense to go downwards this path. Simply this is ofttimes a utilize case for frameworks non enterprise applications. Entity Framework is a skilful example. It'south composed of multiple assemblies each focusing on unlike areas of functionality. We have one core assembly which includes the main artefacts, we accept another assembly for talking to a SQL Server database, some other one for SQLite and then on. With this modular architecture, nosotros can reference and download only the parts that nosotros need.
Imagine if Entity Framework was but one assembly! It would be one gigantic assembly with lots of code that we won't need. Also, every time the support squad added a new feature or fixed a problems, the unabridged monolithic assembly would have to be compiled and deployed. This would make this assembly very fragile. If we're using Entity Framework on top of SQL Server, why should an upgrade because of a issues set for SQLite impact our application? It shouldn't! That'south why it'south designed in a modular way.
In about spider web applications out there, nosotros version and deploy all these assemblies (Web, BLL and DAL) together. So, separating a project into 3 projects does not add any values.
Use Cases for Physical Separation
And so, when exercise you actually demand to physically separate a project into multiple projects? Here are a couple of scenarios:
one- Multiple presentation layers:Let's say you've congenital an order processing application. This awarding is a desktop awarding used by staff at your organization. Yous decide to build a spider web interface for this application then the staff can access it remotely. You want to re-use the existing business logic and data admission components. Every bit I explained before, 1 reason for physical separation is re-usability. Then, in this instance, you need to physically dissever this project into iii projects:
- OrderProcessing.Core (contains both the BLL and DAL)
- OrderProcessing.Web
- OrderProcessing.Desktop
Notation that even here I don't accept two projects (BLL and DAL). I have i project, OrderProcessing.Core, that encapsulates both the business and data admission logic for our order processing application.
And then, why didn't I dissever this project into two separate projects (BLL and DAL)? Because the whole purpose of this DAL is to provide persistence for what we have in BLL. It's very unlikely that it'll be used on its own in another projection.
Besides, following the dependency inversion principle of object-oriented design, the dependency should exist from DAL to BLL, non the other way effectually. And so, this means, everywhere you reference the DAL assembly, you lot should too reference the BLL associates. In other words, they're highly cohesive and inseparable. When you divide things that are cohesive, you run into issues later down the track.
two- Multiple applications nether a unmarried portal:Some other use case that one of the readers suggested is where you have multiple modest applications that are hosted in a single portal. So, from the end user'due south point of view these applications are not carve up; they are all unlike domains of the same application. But from development betoken of view, each application is independent from the others. Each application can take its own persistence shop; one can use Excel, another tin can employ SQL Server, and the other can use Oracle.
In this scenario, information technology's likely that these applications are developed by dissimilar developers/teams. They're often independently adult, version and deployed, hence the second reason for concrete separation.
For this scenario, nosotros could have a solution with the following projects:
- OrderProcessing.Cadre (a form library)
- Shipping.Core
- CustomerSupport.Core
- MainPortal (an ASP.NET MVC projection)
Over again, you don't come across the BLL/DAL separation here. Each class library (eg OrderProcessing.Core) includes both the business and information access logic for its own domain.
The Bottom Line
Here are a few things I promise y'all accept away from this article:
- Layers are non tiers.
- Tiers are most physical distribution of software on different computers.
- Layers are conceptual. They don't take a physical representation in lawmaking. Having a folder or an associates called BLL or DAL doesn't mean y'all have properly layered your application, neither does it hateful you lot have improved maintainability.
- Maintainability is about clean code, small methods, pocket-sized classes each having a single responsibleness and limited coupling between these classes. Splitting a projection with with fat classes and fat methods into BLL/DAL projects doesn't improve the maintainability of your software.
- Assemblies are units of versioning and deployment.
- Split a project into multiple projects if yous want to re-use certain parts of that in other projects, or if you want to independently version and deploy each projection.
Equally e'er, keep it simple!
If yous enjoyed this mail service, please share it with your friends.
Tags: asp.cyberspace, asp.net core, asp.cyberspace-mvc, c#
Source: https://programmingwithmosh.com/net/should-you-split-your-asp-net-mvc-project-into-multiple-projects/
0 Response to "Please Explain How Much Rewriting One Should Do and How You Know What Needs Fixed"
Post a Comment