Transact-SQL
Reinforcement Learning
R Programming
React Native
Python Design Patterns
Python Pillow
Python Turtle
Verbal Ability
Company Questions
Artificial Intelligence
Cloud Computing
Data Science
Machine Learning
Data Structures
Operating System
Compiler Design
Computer Organization
Discrete Mathematics
Ethical Hacking
Computer Graphics
Software Engineering
Web Technology
Cyber Security
C Programming
Control System
Data Mining
Data Warehouse
3 min read · September 24, 2022
Clean Architecture is a popular approach to structuring your application.
It's a layered architecture that splits the project into four layers:
Each of the layers is typically one project in your solution.
Here's a visual representation of the Clean Architecture :
How do we create this in our .NET solutions?
The Domain layer sits at the core of the Clean Architecture . Here we define things like: entities, value objects, aggregates, domain events, exceptions, repository interfaces, etc.
Here is the folder structure I like to use:
You can introduce more things here if you think it's required.
One thing to note is that the Domain layer is not allowed to reference other projects in your solution.
The Application layer sits right above the Domain layer . It acts as an orchestrator for the Domain layer , containing the most important use cases in your application.
You can structure your use cases using services or using commands and queries.
I'm a big fan of the CQRS pattern, so I like to use the command and query approach.
In the Abstractions folder, I define the interfaces required for the Application layer . The implementations for these interfaces are in one of the upper layers.
For every entity in the Domain layer , I create one folder with the commands, queries, and events definitions.
The Infrastructure layer contains implementations for external-facing services.
What would fall into this category?
I place my DbContext implementation here if I'm using EF Core .
It's not uncommon to make the Persistence folder its project. I frequently do this to have all database facing-code inside of one project.
The Presentation layer is the entry point to our system. Typically, you would implement this as a Web API project.
The most important part of the Presentation layer is the Controllers , which define the API endpoints in our system.
Sometimes, I will move the Presentation layer away from the actual Web API project. I do this to isolate the Controllers and enforce stricter constraints. You don't have to do this if it is too complicated for you.
You don't have to follow the folder structure I proposed to the T. Clean Architecture is very flexible, and you can experiment with it and structure it the way you like.
Do you like more granularity? Create more specific projects.
Do you dislike a lot of projects? Separate concerns using folders.
I'm here to give you options to explore. But it's up to you to decide what's best.
Whenever you're ready, there are 4 ways I can help you:
Become a Better .NET Software Engineer
Join 53,000+ engineers who are improving their skills every Saturday morning.
Accelerate Your .NET Skills 🚀
Pragmatic Clean Architecture
Modular Monolith Architecture
The .NET Weekly 📧
Join more than 53,000+ engineers, and get practical tips and resources every week to improve your .NET and software architecture skills.
Also known as.
The Layered Architecture design pattern helps organize applications into groups of subtasks at different levels of abstraction, facilitating independent development and maintenance of each layer.
Real-world example
Imagine constructing a modern high-rise building, analogous to using the Layered Architecture design pattern in software development. This pattern allows each software layer, such as the data layer, service layer, and presentation layer, to support seamless interaction while maintaining independence, enhancing maintainability and scalability. Just as a building is divided into layers such as the foundation, structural floors, residential floors, and the rooftop, each with specific functions and built using different materials and techniques, a software application can be similarly structured. In this analogy, the foundation represents the data layer, responsible for managing database operations. The structural floors are akin to the service layer, which contains business logic and rules. The residential floors parallel the presentation layer, which deals with user interfaces and interactions. Finally, the rooftop could be seen as the API layer, allowing external systems to communicate with the application. Just as each floor in a building is constructed to support the layers above and below, each software layer supports seamless interaction with its neighboring layers while maintaining a degree of independence. This structure allows for easy maintenance and updates, such as refurbishing the interiors (presentation layer) without affecting the underlying structures (business logic and data layers).
In plain words
The Layered Architecture pattern organizes software into hierarchical groups of tasks, each encapsulated in distinct layers that interact with each other, facilitating ease of maintenance, scalability, and clear separation of concerns.
Wikipedia says
In software engineering, multitier architecture (often referred to as n-tier architecture) or multilayered architecture is a client–server architecture in which presentation, application processing, and data management functions are physically separated.
On the data layer, we keep our cake building blocks. Cake consist of layers and topping.
The service layer offers CakeBakingService for easy access to different aspects of cakes.
On the top we have our View responsible for rendering the cakes.
This pattern is suitable for structuring applications that can be divided into groups where each group has a specific role or responsibility. Common in enterprise applications, it simplifies dependencies, enhances maintainability, and supports scaling and technology stack segregation.
Use the Layers architecture when
A well-structured and organized solution is crucial for maintainability and scalability in software development. The 3-Tier architecture clarifies concerns, allowing developers to manage the application logic, data access, and presentation layers separately. In this article, we will explore the benefits of this architecture and learn how to implement it in a Visual Studio solution using C#.
There are many types of architectures you can use for your applications. You just have to find the one that suits your needs. I like the 3-tier architecture. It’s small, simple, and easy to create. But in some cases, I might change it because of my needs.
There are 9 known architectures we use these days in C#:
I am not going to explain them all. It will become a very large page. But the top 3 popular architectures are:
This tutorial explains the 3-tier architecture mainly because we use this one the most.
This architecture separates the application into three logical components. These three layers help to maintain the code and separation of concern. But for me, it has a different reason I use this architecture: reusability.
Especially at a time when you might want to create a Windows, macOS, Android, Linux, and iOS app that does the same thing. Do you create the same class over and over again? No, you create a class library with the logic and reference that library in all the projects that need the logic. If something changes in the class library, all other projects get updated.
Each layer has its own responsibility but only talks to one other layer. The presentation layer only talks to the application layer and the application layer only talks to the data layer. That means that if you want to show a list of movies, for example, you need to ask the application layer which will ask the data layer. The application layer will send the information of the data layer to the presentation layer. The presentation layer never talks to the data layer directly.
The first layer I am discussing is the data layer. Maybe the name already tells you what this layer is about: Data. It could be data from a database or a file. Generally speaking, we use this layer to transport data from and to a data source.
This layer does not contain any form of logic. It just sends or retrieves data from the data source. Nothing special. Logic is placed in the application layer.
If you have multiple data sources you need to manage, you can create multiple data layers and bring them together in the application by using dependency injection.
The data layer is a layer that is disappearing a bit. Before we had ORMs, like Entity Framework , we had to write a lot of code to retrieve and send data from and to a database. With Entity Framework, we don’t need that anymore. That is the reason the logic for the database is moving to the application layer.
This layer is the beating heart for all your logic. Here you combine logic with the data from the data layer. This layer is responsible for calculations and decisions before sending the data to your presentation layer.
The application layer is often called the business layer. This is a small heritage from the time we had the DLL (data layer library) and the BLL (business layer library). In my projects, I usually call it the business.
For example, the presentation layer requests a list of movies, ordered by title. The application layer will retrieve all the movies from the data layer and sort them by title.
This layer does nothing with the data source directly. The reason is simple: The application layer should never know what kind of data source you use. If you implement dependency injection, the data source can change. Even the whole data layer can change (except for the interface). But the application layer should always be working without changing it.
You can have multiple application layers. I created a logger once. A simple logger that stores messages in a file. But later I needed a logger that stored the messages in a database. I didn’t want two loggers in the single class library representing my application layer. So I created a second application layer for the logger.
It’s usually this layer we unit test because it contains the most important code of our application.
And last but not least; the presentation layer. This layer is what a user sees. It could be a console application, WinForms, or an API. It’s the one that gives output that a user or a client application can work with. This layer is also referred to as the top-most layer in this architecture.
It is responsible for presenting the data from the application layer in a user-friendly way. Examples of user-friendly are web pages, WinForms, and mobile apps.
A console application or a WinForms application has a clear GUI. You can click on stuff and it works. An API doesn’t have that. But it does expose JSON (or XML if you want). An API is considered a front-end application.
The 3-tier architecture is the most used because it’s easy to understand and implement. I encourage you to use this if you just start or create a small project. If you have a really, really small project; make your life easier, and don’t use layers.
The idea behind these layers is to separate data, logic, and application. I think the names are a bit confusing and most people think the application layer is the front end, but that’s the presentation layer. And that is why I call the application layer the business layer.
Learn C# – Part 9: Basic Structure And A New Project
10 reasons why you should unit test, you may also like, visual studio shortcuts, design patterns and design principles, to savechanges or not to savechanges, top 5 reasons to use c#, how to apply clean code with c#, the c# data types part 2 – int, the c# data types part 1 – string, the use of code comments.
Kens Learning Curve is all about learning C# and all that comes along with it. From short, practical tutorials to learning from A to Z.
All subjects are tried and used by myself and I don’t use ChatGPT to write the texts.
Use the contact form to contact me or send me an e-mail.
The contact form can be found here .
Email: [email protected]
@2023 – All Right Reserved. Designed and Developed by Kens Learning Curve
Like what you are seeing.
Make sure you don’t miss our latest tutorials and courses! Subscribe to our updates. We won’t spam you, promised.
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
My question is about various ways of implementing presentation layer in 3-tier architectures
When we talk about a 3-tier web application, it is assumed that the presentation layer is browser-oriented, and hence communicates with logic tier through HTTP protocol
I'd like to know, how presentation layer is going to communicate with logic tier, in case if the presentation layer is going to be a standalone application with its own GUI, rather than browser-based
For example, Java servlets get HTTP requests from our browser, but what about if I want to design a specific desktop application to communicate with servlets ? How my app is going to communicate with logic tier ? Which protocol is used ?
I guess you're misunderstanding the problems. You can say in this case the presentation layer is splitted in 2 small layers:
Apart from these, you can have your business logic layer (usually Service classes) and your data access layer (DAOs or whatever you feel better to call them).
If you put this in the perspective of creating GUI desktop applications, your presentation will have a similar structure:
In this scenario, it usually happens that these classes are the same , but note that they are for presentation purpose and should relate with your business logic layer.
what about if I want to design a specific desktop application to communicate with servlets?
You probably mean about a client application that consumes Web Services. The Web Services (consumed by XML, JSON or plain text) could be part of a services layer that should be consumed in the business logic layer or in the presentation layer of the application, depending on what the web service returns. Still, I would find better consuming the web service layer from the business logic layer and let the presentation layer to handle its purpose: presentation logic only .
To show an example:
From comments:
Still it's unclear how business logic layer of my application is going to communicate with web service layer.
Posting a very simple skeleton sample from a Web Application Project that will consume a Web Service.
Servlet class (adapted from StackOverflow Servlet wiki ) (part of presentation)
PersonBL class (part of Business Logic Layer)
Now, posting the skeleton of the Web Service:
PersonPort class (the implementor of the Web Service)
PersonWSBL class (business logic layer in Web Service)
PersonDAO class (data access layer)
As you can notice, there's no magic in communicating the presentation with the business logic layer. Of course, this skeleton can be enhanced by using other set of technologies, but it just to illustrate the main idea.
Note: The skeleton of the Web Service was adapted from here Creating a Simple Web Service and Client with JAX-WS .
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Andrea Bizzotto
Updated Sep 21, 2023 11 min read
When writing Flutter apps, separating any business logic from the UI code is very important.
This makes our code more testable and easier to reason about , and is especially important as our apps become more complex.
To accomplish this, we can use design patterns to introduce a separation of concerns between different components in our app.
And for reference, we can adopt a layered app architecture such as the one represented in this diagram:
I have already covered some of the layers above in other articles:
And this time, we will focus on the presentation layer and learn how we can use controllers to:
This kind of controller is the same as the view model that you would use in the MVVM pattern . If you've worked with flutter_bloc before, it has the same role as a cubit .
We will learn about the AsyncNotifier class, which is a replacement for the StateNotifier and the ValueNotifier / ChangeNotifier classes in the Flutter SDK.
And to make this more useful, we will implement a simple authentication flow as an example.
Ready? Let's go!
Let's consider a very simple app that we can use to sign in anonymously and toggle between two screens:
And in this article, we'll focus on how to implement:
Here's a simplified version of the reference architecture for this specific example:
You can find the complete source code for this app on GitHub . For more info about how it is organized, read this: Flutter Project Structure: Feature-first or Layer-first?
As a starting point, we can define a simple abstract class that contains three methods that we'll use to sign in, sign out, and check the authentication state:
In practice, we also need a concrete class that implements AuthRepository . This could be based on Firebase or any other backend. We can even implement it with a fake repository for now. For more details, see this article about the repository pattern .
For completeness, we can also define a simple AppUser model class:
And if we use Riverpod, we also need a Provider that we can use to access our repository:
Next up, let's focus on the sign-in screen.
Suppose we have a simple SignInScreen widget defined like so:
This is just a simple Scaffold with an ElevatedButton in the middle.
Note that since this class extends ConsumerWidget , in the build() method we have an extra ref object that we can use to access providers as needed.
As a next step, we can use the onPressed callback to sign in like so:
This code works by obtaining the AuthRepository with a call to ref.read(authRepositoryProvider) . and calling the signInAnonymously() method on it.
This covers the happy path (sign-in successful). But we should also account for loading and error states by:
One simple way of addressing this is to:
Here's how the resulting code may look like:
For a simple app like this, this is probably ok.
But this approach gets quickly out of hand when we have more complex widgets, as we are mixing business logic and UI code in the same widget class.
And if we want to handle loading in error states consistently across multiple widgets, copy-pasting and tweaking the code above is quite error-prone (and not much fun).
Instead, it would be best to move all these concerns into a separate controller class that can:
So let's see how to implement it in practice.
The first step is to create a AsyncNotifier subclass which looks like this:
Or even better, we can use the new @riverpod syntax and let Riverpod Generator do the heavy lifting for us:
Either way, we need to implement a build method, which returns the initial value that should be used when the controller is first loaded.
If desired, we can use the build method to do some asynchronous initialization (such as loading some data from the network). But if the controller is "ready to go" as soon as it is created (just like in this case), we can leave the body empty and set the return type to Future<void> .
Next up, let's add a method that we can use to sign in:
A few notes:
AsyncValue.guard is a handy alternative to try / catch . For more info, read this: Use AsyncValue.guard rather than try/catch inside your AsyncNotifier subclasses
And as an extra tip, we can use a method tear-off to simplify our code even further:
This completes the implementation of our controller class, in just a few lines of code:
Note that there is a clear relationship between the return type of the build method and the type of the state property:
In fact, using AsyncValue<void> as the state allows us to represent three possible values:
If you're not familiar with AsyncValue and its subclasses, read this: How to handle loading and error states with StateNotifier & AsyncValue in Flutter
Time to get back to our widget class and wire everything up!
Here's an updated version of the SignInScreen that uses our new SignInScreenController class:
Note how in the build() method we watch our provider and rebuild the widget when the state changes.
And in the onPressed callback we read the provider's notifier and call signInAnonymously() . And we can also use the isLoading property to conditionally disable the button while sign-in is in progress.
We're almost done, and there's only one thing left to do.
Right at the top of the build method, we can add this:
We can use this code to call a listener callback whenever the state changes.
This is useful for showing an error alert or a SnackBar if an error occurs when signing in.
The listener code above is quite useful and we may want to reuse it in multiple widgets.
To do that, we can define this AsyncValue extension :
And then, in our widget, we can just import our extension and call this:
By implementing a custom controller class based on AsyncNotifier , we've separated our business logic from the UI code .
As a result, our widget class is now completely stateless and is only concerned with:
Meanwhile, the job of our controller is to:
And since the controller doesn't depend on any UI code, it can be easily unit tested , and this makes it an ideal place to store any widget-specific business logic.
In summary, widgets and controllers belong to the presentation layer in our app architecture:
But there are three additional layers: data , domain , and application , and you can learn about them here:
Or if you want to dive deeper, check out my Flutter Foundations course. 👇
I launched a brand new course that covers Flutter app architecture in great depth, along with other important topics like state management, navigation & routing, testing, and much more:
Learn about State Management, App Architecture, Navigation, Testing, and much more by building a Flutter eCommerce app on iOS, Android, and web.
Invest in yourself with my high-quality Flutter courses.
Learn about Firebase Auth, Cloud Firestore, Cloud Functions, Stripe payments, and much more by building a full-stack eCommerce app with Flutter & Firebase.
Learn Dart Programming in depth. Includes: basic to advanced topics, exercises, and projects. Fully updated to Dart 2.15.
Master Flutter animations and build a completely custom habit tracking application.
COMMENTS
The presentation layer can also handle more complex tasks, such as in a stock trading app where livestock exchange data is retrieved via an API and then presented as graphs or charts. While developers are primarily responsible for building the business and data layers, UI and UX designers typically design the presentation layer.
The Docker daemon. The Docker daemon (dockerd) listens for Docker API requests and manages Docker objects such as images, containers, networks, and volumes.A daemon can also communicate with other daemons to manage Docker services. The Docker client. The Docker client (docker) is the primary way that many Docker users interact with Docker.When you use commands such as docker run, the client ...
The presentation layer ensures the information that the application layer of one system sends out is readable by the application layer of another system. On the sending system it is responsible for conversion to standard, transmittable formats. [ 7] On the receiving system it is responsible for the translation, formatting, and delivery of ...
Each layer in the architecture forms an abstraction around the work that needs to be done to satisfy a particular business request. For example, the presentation layer doesn't need to know or worry about how to get customer data; it only needs to display that information on a screen in particular format. Similarly, the business layer doesn ...
Layered Architecture, also known as the n-tier architecture, is a design pattern that organizes a software system into a set of horizontal layers. ... Presentation Layer: Handles user interactions ...
It is also known as an n-tier architecture and describes an architectural pattern composed of several separate horizontal layers that function together as a single unit of software. A layer is a logical separation of components or code: ... Presentation Layer - responsible for user interactions with the software system;
P.S This article explains the three-layered architecture only from a back-end perspective. Presentation Layer. The presentation layer is the highest layer of the software. It is where the user ...
Three-tier architecture is a well-established software application architecture that organizes applications into three logical and physical computing tiers: the presentation tier, or user interface; the application tier, where data is processed; and the data tier, where application data is stored and managed.
Mainly, this layer is responsible for managing two network characteristics: protocol (set of rules) and architecture. Presentation Layer Protocols : Presentation layer being the 6th layer, but the most important layer in the OSI model performs several types of functionalities, which makes sure that data which is being transferred or received ...
The presentation layer is the lowest layer at which application programmers consider data structure and presentation, instead of simply sending data in the form of datagrams or packets between hosts. This layer deals with issues of string representation - whether they use the Pascal method (an integer length field followed by the specified ...
The architecture is called "layered" because it resembles a layered cake, where each layer is independent of the others but builds on them to form a complete and cohesive application. Presentation Layer. The presentation layer is the topmost layer of the architecture, responsible for handling the user interface and displaying data to the user.
Multitier architecture. In software engineering, multitier architecture (often referred to as n-tier architecture) is a client-server architecture in which presentation, application processing and data management functions are physically separated. The most widespread use of multitier architecture is the three-tier architecture .
Here are five main layers in software architecture: 1. Presentation layer. The presentation layer, also called the UI layer, handles the interactions that users have with the software. It's the most visible layer and defines the application's overall look and presentation to the end-users. This is the tier that's most accessible, which anyone ...
Presentation layer that renders views or responses within the interface. Business layer that executes necessary, functional business logic. ... Each layer within the architecture must provide the right balance of abstraction and operational value. Teams should identify the layer names, their purpose and the relevant entities that will reside ...
One of the most prevalent patterns seen in modern software architecture is the 3-tier (or three-tier) architecture. This model structures an application into three distinct tiers: presentation (user interface), logic (business logic), and data (data storage). The fundamental advantage of 3-tier architecture lies in the clear separation of concerns.
15. There is a big difference between the application layer and the presentation layer from a DDD view point. Although DDD centers around how to model the domain using the DDD building blocks and concepts such as bounded contexts, Ubiquitous language and so, it is still vital to clearly identify and separate the various layers in your app. The ...
web development. One of the most common ways to modularize an information-rich program is to separate it into three broad layers: presentation (UI), domain logic (aka business logic), and data access. So you often see web applications divided into a web layer that knows about handling HTTP requests and rendering HTML, a business logic layer ...
Independent Computing Architecture (ICA): It is a presentation layer protocol in the OSI model, which was formed by Citrix Systems. It is used for transferring data from server to client. It is a very thin protocol as it does not require much overhead in order to transmit data from the server over to the client. It is well-optimized for the WAN.
Clean Architecture is a popular approach to structuring your application. It's a layered architecture that splits the project into four layers: Domain. Application. Infrastructure. Presentation. Each of the layers is typically one project in your solution. Here's a visual representation of the Clean Architecture:
Typically, a layered architecture is classified into four distinct layers: presentation, business, persistence, and database; however, the layered architecture pattern does not specify the number ...
Imagine constructing a modern high-rise building, analogous to using the Layered Architecture design pattern in software development. This pattern allows each software layer, such as the data layer, service layer, and presentation layer, to support seamless interaction while maintaining independence, enhancing maintainability and scalability.
3-tier architecture. This type of architecture separates an application into three logical components: the presentation layer, the application layer, and the data layer. This separation of concerns provides better scalability, maintainability, and ease of development. Model-View-Controller (MVC) architecture. Well-known if you have created web ...
3-Layered Architecture consists of: Presentation layer; Application layer; Data layer; Presentation Layer. Presentation Layer is our connection to the outside world. This is where we handle all ...
You can say in this case the presentation layer is splitted in 2 small layers: Files that handle the view (JSP, Facelets, etc). Files that control the interaction between user and the view (Servlets, @Controller from Spring MVC, @ManagedBean from JSF, etc). Apart from these, you can have your business logic layer (usually Service classes) and ...
Flutter App Architecture: The Domain Model. Flutter App Architecture: The Application Layer. And this time, we will focus on the presentation layer and learn how we can use controllers to: hold business logic. manage the widget state. interact with repositories in the data layer. This kind of controller is the same as the view model that you ...