login to rate this
[.NET7] Design Patterns: Facade in a Web Application
7/28/2024 8:41:52 AM
5
0
|
login to rate this
[.NET7] Design Patterns: Repository in a Web Application
1/6/2023 7:51:12 AM
0
0
|
login to rate this
[.NET7] IP GeoLocation with Dependency Injection and Custom Middleware
1/14/2023 3:15:14 PM
0
0
|
login to rate this
[.NET7] Weather Forecast with Dependency Injection and Custom Middleware
10/3/2023 10:35:11 AM
0
0
|
login to rate this
Enum as String in EF Core
10/9/2023 2:49:39 AM
0
0
|
login to rate this
Esta é para Devs Junior! Uso de fila no ASP.NET Core 7
10/26/2023 10:48:52 AM
0
0
|
login to rate this
Keep Track of your Page Visits in Blazor Server
10/16/2023 10:27:37 AM
0
0
|
nbolt
Hi Folks! In this post I will show you how one of my favorite design patterns work: the facade design pattern. The theory behind them is always more complicated than it actually seems: the facade is meant to reduce the complexity when dealing with other objects. In the sample application, based on the IP GeoLocation with Dependency Injection and Custom Middleware application, we can notice the source-code manipulating the session object directly, which is kinda dangerous (the boxing and unboxing alone can be pretty messy).
Before running the application, you need to register yourself on IpInfoDb to get an API Key.
Aftet that, you need to replace "INSERT YOUR KEY HERE" string inside the Program.cs file in the Location project.
So what if we create a facade class to hold the Location object, save/retrieve the object from the data-source and choose where to store the information, including all the necessary dependency injections for it? But first, let's clone the github repository and checkout a branch called designpattern-facade!
Clone the repository from github here.
But do not forget to branch out to designpattern-facade, otherwise you will not be able to see the right code.
Open the NET7-IPGeolocation.sln solution file and browse to a file called IUserFacade inside the Location website. You will notice that the interface declares a property called Location, from the Services.Location.LocationModel type. The implementation of that interface is done by the Facades/UserFacade class, which is sweetly added in the IOC container in the Program.cs file.
If we decide to change the implementation, we can easily create a new class, implement the interface and change the reference in the Program.cs file.
Now let's take a look at the Facades/UserFacade class. Browse to that directory inside the Location website. In the constructor, there's 2 parameters: one for the memory cache and another one to grant access to the HttpContext object (I need the Session ID to compose a location key - where to store the information in the cache of the application, basically).
But before you run the application, you need to register yourself in the IpInfoDb website to get an API key. Once you do that, replace the "INSERT YOUR KEY HERE" inside the Program.cs file. You should see an output like that:
I would say the Facade design pattern is one of the most useful, because we have to deal with Cache and Session objects all the time. So it helps us organize the source-code of our applications, its benefits are huge.
Hope you guys enjoyed it. See ya!
nbolt
nbolt
nbolt

We use cookies and similar technologies to enhance your browsing experience, analyze site traffic, personalize content, and serve targeted advertisements. By continuing to use our site, you consent to our use of cookies. And click OK to close this pop-up window.