Structure Your Golang Service With Layers Like This

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in my previous stream I was talking about that it's very very important to basically decouple your transportation layer with your business layer with your business logic layer and all the people in the chat were basically like huh what the hell is the transportation layer they have no clue and I thought man this is actually very important to understand so that's why I'm going to basically make this small little video emphasizing what uh these things are so before we continue if you're not yet subscribed to the channel consider subscribing give me a thumbs up leave some questions in the comments and of course jump into the Discord Community all the links are in the description all right so basically uh in the previous stream what we were doing there was some kind of an assessment that somebody uh has gotten uh enable to apply for a job and he sent it to me and I want to make on The Stream we basically uh made the first part of the assessment right and I was explaining it was basically a small uh micros service where you needed to add and it needs to return a bit price and ID and yada y y if you want to know the details check out the stream and then you can follow along right so basically um and I said guys listen it's very important to basically decouple your transportation layer from the business layer and people were freaking out what the hell is the transportation layer right so basically in the spectrum of uh I don't know microservices actually in the spectrum of the web We have basically a request and a response right and if you request something that thing needs to be transported right that needs to be hit your endpoint your server whatever you know what I mean and most of the time in in in the web it's going to be HTTP right HTTP and most likely over Json of course uh in the microservices domain in the microservices Spectrum where everything is behind the gateway most of the time you're going to communicate in other protocols right there it could be Json over HTTP but the transportation layer could also be some something like a Proto buffer grpc it could be uh drift or maybe C and whatever right rabbit mq you name it um there is always a protocol available for you so basically that's the transportation layer so each time um let me show you what we have here right um so we decoupled this completely so we have handle add request right uh which is basic what is going on so in this case we have handle add request which is basically uh an method function attached to our add request Handler and what this is going to do is basically once you hit that endpoint um it's going to call this is the mo this don't think about that it's this is the most important thing it's going to call our service and it's going to call the ad function with the ad ID right this is basically just a random stuff normally the user would provide that but hey this is an example right and uh if it's if there's a problem here we're going to just return Json right and otherwise we are going to return an add response uh and fill in the variables that are basically returned here and write that back as Json so very important to take away here is that we could easily return an ad response directly from our ad business Logic the problem is that if you if you don't want to have friends with benefit you know what I mean you want to have a complete separation of concerns right the business logic should return data that is basically not being tied to your transportation right and that's a transportation where you basically go into return a adjacent serialized object of what the business logic has produced right so let us take an uh an example here right uh let's go here boom so basically the ad service is an interface and you can see the only thing it does it's basically it's very specific right we're going to add we're going to get we're going to pass in a uu ID and it's going to return a u uid a float 64 which is the bit price in this case uh and an error right and then we have of course our implementation here and the implementation is here right the ad service of course it's a structure there is no State Y is also very simple high level example right um in a real world example there could there will be more State most of the time right so basically we call uh this ad service and we Implement our add thingy and you can see it has dysfunction signature just like the interface and yeah the business logic guys it's an example we generate a random bit price uh a random ID and we just return it right but very important to take away is that we don't we do not have any instrumentation in our business logic right we don't have any serialization no login nothing it's pure business logic very simple very specific a very separation of concerns so how do we do the the logging because if you going to go into a prodution environment in a company most of the time you need to provide a complete uh prodution the service or micros service doesn't really matter um which should have instrumentation logging it should have the whole the whole Shang you know what I mean and well it's very simple because this is an interface and again that's why I'm emphasizing on interfaces are very very very important to learning goang because it's the bread and butter is what they say this logging middleware is also an ad service right it's also this interface it implements the same function look at that you see the login middleware calls at exactly the same of course we have this function how do you call this return values are captured here named return Val values why because we can do this defer fun and I already have a video about that I'm also teaching this in the fulltime gov especially in the microservice section um this is basically going to rep is a middleware on top of your business logic which is just going to lock out the values that are being produced by your business logic right so you don't have any co-mingling of logic with your business logic your instrumentation logic logging logic but also very important Transportation logic which is this Handler here right in the ideal scenario this Handler would be a little bit better I understand um there are some fancy tricks but just for the sake of demonstration this is this right you need to accept it like it is right now so that's basically it right a very very important topic to understand is that you can use interfaces and you can change you can people going to say maybe it's the middleware pattern I don't know maybe it's going to be hey this is hexagonal architecture hey maybe maybe I don't care about the patterns the only thing I care about is that I know when I need to implement this and it's actually very important if you're doing this in a production environment right um that's basically it guys so very important Transportation layer is the way you're going to communicate with your services most of the time h GTP Json um calization protocol with microservices this most of the time are lying behind the gateway and the communication protocol is a little bit lower level JPC Proto trift Kafka you name it right uh and that's why it's important you can separate the business logic with the transportation logic because then you can provide multiple you can support multiple Transportation Logics for the same business domain for the B for the same business logic if you know what I mean and if you need to change something or you need to add or remove it you do not need to touch your business logic and visa versa I hope it's is a very short video very important check out the stream if you want to learn more about this check out the videos check out the fulltime go whatever you want it's a very important aspect you need to understand not only for goang but for actually in any any language you're going to write um need to have some kind of a separation of concerns thank you for watching I'm looking forward to see you in the next video bye-bye
Info
Channel: Anthony GG
Views: 12,851
Rating: undefined out of 5
Keywords: golang microservices, golang, golang programming language, golang tutorial, golang programming, golang for beginners, golang tutorial for beginners, golang microservices tutorial, golang rest api, programming, golang web development, golang project structure, microservices, hexagonal architecture, building microservices golang, go programming, learn golang, microservices tutorial, golang beginner tutorial, vscode, vim, software engineering
Id: IMqZ9e_MPRI
Channel Id: undefined
Length: 7min 59sec (479 seconds)
Published: Fri Mar 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.