Golang Microservices: Repository Pattern, Dependency Injection and Services.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello my name is mario welcome to another video in today's episode i will share with you another tip for building microservices in go specifically the repository pattern services and dependency injection this video is using as a reference the previous one i recorded that was covered in domain driven design and the project layout the link to that one will be in the description if you haven't seen that one yet one of the things that i want to just mention right away is that regarding dependency injection i won't be discussing the two packages i have on the agenda which are the google wire and uber go dig i like not using those kind of packages i like to define it the dependencies that i need when instantiating all the types that i need to instantiate explicitly or as explicit as possible so i just want to get that out of the way we're going to be discussing like i said the repository pattern the services specifically the domain service and the application service and all of these with a concrete example that involves the previous project that is called that is discussed in the previous video the to-do application with all of that being said let me show you how the repository pattern works the idea of having a repository or using the repository pattern is to have a layer between a datastore whatever that is and the domain that you are defining the domain that we are going to be using is obviously a to-do list application kind of uh implementation there are a few types that are defined on the the internal package that basically build the type called the task type and more importantly or the important thing about this type is that some of these types that are used as fields have their own logic or business logic and is basically explicitly indicated by a by a method called validate so a task type has a validate that happens to be also calling the priority type and the date dates type their validate func they their validate methods as well and not only that but also uh depending on the documentation some of them some of them indicate some other rules that we have to follow i want to call that out right now because in the future with the videos i will be adding those will be also implemented as well with all of that being said let's jump into the repository pattern that actually uses sqlc which is one of the one of the packages that i was discussing as well in my previous video if you haven't seen those three specific videos again i will have the link in the description so you can check them out later specifically what is happening right here is that i'm defining three three different uh functions uh that are allow that allow us to create tasks and select tasks and update tasks register forward and with sql c after running the code generate that it happens to be in this file it will allow it will build all the code that we need for actually interacting with the database in postgres which is right here so all of this is auto generated for us we don't have to do it what is important about the repository pattern is to define concrete a concrete interface or concrete methods that are going to be receiving types that live in the domain package nothing here outside of the public interface is using sql related types if you notice that create method for example is using the strings the internal type priority and then internal type dates and happens to return the task similar situation happens with the find that it receives a string which is id returns a task and returns an error and a similar situation with the third method which is update which receives again use domain types nothing outside of that this is pretty straightforward works sort of like crowd like uh interaction with the database what is important is with the second thing that i wanted to discuss which is which is the services in this case in the context of domain driven design and more specifically with the layout that we're using in go the service defines the interaction between the business logic the business rules that we have and the repository layer and the repositories and therefore the data stores and the way it works is that the service itself receives as a way to doing to be instantiated a repository that is defined as an interface type and this is the important bit of the layer that we're implementing implementing in the in through the or via the repository pattern through the postgres package postgresql package is that it allows to separate those two layers explicitly not only is useful for testing purpose purposes but also for explicitly uh allowing interacting different data different repositories with the domain logic that we need to define in their service right now this is a really pretty forward implementation we are literally just calling the function and that is defined in the repository for create we are calling create for find for task we're calling find for update we're calling update but think in terms of what happens if we have an application service that has this domain this logic that happens to be interacting with different repositories for with different data stores different data stores this is the way to connect all of that now how is this being used in in in the end when we are instantiating all of this we need to jump and look at the final uh binary that i have right here that is literally connecting all of that we are instantiating a data store from the postgres package called task which is doing the crot like functions or methods is the one inserting selecting and updating in sql that one is passed in as an argument that is where the dependency injection actually happens and we use that through the example it's a pre-stair for example we are creating a new task where we are updating it and then we are finding it and they print out the result if we run that i want to show you the database first there is nothing in the database and it's empty if we run the example you will see that it's actually doing what i was just telling you it creates the record right here and it updates the record so right here if you notice is new task the description and it's changed to change task the priority is one and it's changed to priority three which if i believe this is low and this is high and the due date also changes if you notice that here so this is because again these are the things that we decided to change and these are the things that we're passing down from the interface that we have right here which is a cli passing that down to the implementation in the application service which is the service package and then passing that down to the actual repository layer that is defined in the postgres sql package now this is the beginning of the actual implementation all of this is really simple and it will start to make sense when again when the things when things get complicated you might be thinking oh all of this is way too much work but all of the all of these will pay off in the long run in the long run you you have to trust me on that and with all of that being said um i will be continuously updating the code with more videos updating what i was describing you adding more layers more changes more changes to the original project layout that i was discussing previously and i will keep you updated with obviously with the changes and whatnot with all of that being said i hope all of this is more or less clear if if it's not just leave a comment i will do my best to answer your questions and as usual i didn't mention this but the code will be also linked in the description as well with all of that being said thank you for watching and keep it up i will talk to you next time until then take care
Info
Channel: Mario Carrion
Views: 25,903
Rating: undefined out of 5
Keywords: golang, microservices, golang microservices, golang web development, building microservices go, building microservices golang, golang rest api, golang ddd, golang domain driven design, build microservices golang, golang ddd layout, golang repository pattern, golang repository, golang dependency injection, golang domain services, golang application services, golang wire, golang dig, golang uber dig, golang google wire, golang domain model, golang datastore, golang database
Id: Z89UU4vSayY
Channel Id: undefined
Length: 9min 8sec (548 seconds)
Published: Fri Feb 19 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.