[Golang] Dependency injection with Fx

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone I'm happy to have you back on my channel so today it's going to be dependency injection with the FX framework from Uber it's just the basics so nothing Advanced so maybe that will be part two um so to start with I have a question for you so uh answer to that question in the comment if you can so which dependency framework do you use on your day-to-day job or do you just use a natively go wiring all right so the reason I'm asking that question is obviously the topic of the day so in a real life project we have a lot a lot of services and often they are complying to interfaces and because we want to have scalable and easy easy testable components in isolation often binding those Services together is you end up with a big file or sometimes some tricky situations so that's why um we often use a framework a dependency injection framework so today we will have an overview of the framework FX so just the basics to get you up and running all right so first we will have a really short presentation of the FX framework I just want to show you the website Uber website and the GitHub then we will go through an example of my own so um you will see later but the Uber example is an HTTP server example which is not friendly at all to understand the gist of dependency injection and how the framework s so we will go through a much simpler example step by step together hopefully it will be easier for you to digest then you know I like to do some quick discussion to discuss pros and cons about that format so we'll do that in a third part and then finally I will just um display the link to the code from the example so you will have the code if you want to explore yourself alright don't forget to like the video it helped me to reach more people because it's raised the popularity of the channel or the video so your kind of help yourself as well so Spread spread the video if you like it and don't forget to subscribe as well so then you never miss a video from that channel and ultimately just for me to understand that you enjoy this kind of content and I can make more otherwise I won't have muchbody in doing those so thank you very much so I hope you will enjoy that video and happy coding Okay so FX is a dependency injection framework made by Uber so yeah like uber AIDS are Uber them drivers service so yeah I don't know if you're new but obviously they have some software themselves and they developed a framework to smooth a bit the dependency injection when they create microservices right so I'm on the GitHub page at the moment so it's very lightweight obviously the dogs are on their website there if you want extensive documentations but the as far as it stands here so the promises are removing globals so often in the main some main programs you can see like nasty Global for a Singleton and stuff so effects framework promise is you won't have that also called reuse so that's the the implemented some shareable module but we won't see that because it's too advanced so the purpose of that first video is just to get you up and running with that framework for very easy cases and then the third point is better tested obviously um they they use it for their own systems so they've been using they've been using it a lot I guess right so then you can see some installation directive there and um the number of stars and stuff but the most interesting thing is there on their website as I said so feel free to have a look so you can see so the introduction just um repeating what they have on their GitHub the GitHub get started is just a walkthrough example and they decided to go through an HTTP example which is not the easiest if you just want to play around with um what's dependent suggestion basically so we'll go through together a much much simpler example which will be helpful I hope but once you're comfortable have a look because it does explain the philosophy of their life cycle and what the effects what an fx application does and also all the fancy features like annotation and value group and this kind of thing so um yeah application life cycle as I mentioned is there and so on and you can even subscribe everything and contribute and keep up to date so they are running on 1.19 at the moment so you can keep an eye on it and that's it for the effects introduction all right so now let's try to implement uh use that framework FX um so you can see that I created an empty folder um and I'm trying to instantiate a go mode so there you go so you have a go mode so the path is tutorial slash FX then I'm installing that go get go uber.org FX at latest comment that was in there instructions then we can just create a main program main application go along so so far so good so we should be able able to use their Library where it's called FX so here I'm just like you know simple routine check like our program is running I just printed hello so to start with really quickly just um I just want to do a refresher on the dependency injection so you can see here I'm creating a struct called Main Service then we will create a receiver method and that Main Service called run and it's just printing something like that so then back to our main we can invoke that so um so it's a receiver method so I can't invoke that on just a struct so we can just put Main Service into a variable and then invoke run on it like that so we should see main program there you go so it's running as intended so here I can just use my ID shortcut to create a Constructor which will be useful for the next steps so you can see there and it runs still spitting out main programs all good now let's complexify things a little bit so let's say that our main service has a dependency so we can create a struct that struck can be uh we can find some publish something so we're gonna create a publisher so um that publisher can have a publish method as a receiver method so like that so you can see it's like a public publish method and we will just do some log line to start with to make sure everything is running smoothly so our main service will have a publisher then ID shortcut a Constructor for the publisher I did good just making things bigger for you to see so publisher so the Constructor need to be changed so you can see my ID shortcut is quite useful there and then my compiler is on spot and he's telling me that my Constructor needs something else so we can declare a publisher and then send that publisher to the Constructor so here I'm doing a sneaky change so I'm changing it needs to be a pointer publisher not a publisher like that so all being good you should see publisher and Main program so publisher from the publisher and Main program from the main service okay so let's go even further to make things more interesting so here I'm just gonna declare a type called title but it's like an alias first string so then I will publisher will have uh title so then we can print something and make sure that things are properly wired so you'll see it's going to be useful for the next step so publisher and then the title basically so the title is I declared it as a pointer line 29 oh so let's just regenerate The Constructor so ID shortcut there you go so the log line 36 is probably gonna print me an address from that printer because data is a pointer as you can see so I'm not expecting to see much really oh okay so my compiler complained because publisher The Constructor needs a title so you can just cast since it's a string you can just cast the string to tighter essentially and then he needs a pointer so let's try that right it's compiling there you go publisher and then a pointer address that's fine so we can just de-reference that pointer it's not a problem um it might be a problem if it's Nils so you would not do that that in production system you would write test to cater for that but now you can see hello and if I change t as goodbye you can see now it's saying goodbye so really easy example so you have a main service it needs a publisher and the publisher needs a title so that's how you would do it without any framework it's like dependency injection 101 obviously I haven't introduced any um interfaces but um it would be pretty similar because you would have to comply to the interface right so I just checked that my FX framework was available and it was so you can see what I wrote right now FX dot new DOT run and it will run an fx application so properly installed and installed and it's running an fx framework there right so um the way it works is you gonna run a program but you need to tell what to invoke otherwise just dangling and not doing anything so here we we really invoked we will invoke a function which take a main service and just invoke Doctrine so not in the Run function like I did but inside the new new is where you it's like a registry where you register your um providers and construct basically so there you go so then it's it's really not happy with me because to run a main service I need other components so you see it's got good it's not gonna let me run that program so let's start step by step as we did with the um by hand injection so it needs a main service that's fine we have a Constructor so you have FX provide and then you're sending the construction you need then we need a publisher that's fine so we're sending main service and then the publisher Constructor and then we need a Constructor for title so that's fine I'll show you a trick so instead of you could just create a Constructor which spitting back um puncher title but here you can just provide an anonymous function so I'm just declaring unflight a function and we will send back that pointer okay so I can't do that so I will just going the long way around so assigning a variable and is then just send it back a pointer of it yeah we'll change into hello and hopefully it will run there you go so you just have your FX application running and the publisher send back the correct string you were expecting so that's really good okay so far so good so the application is running so you can see this is how you inject your providers and run an application basically so that's the equivalent of what we just did by hand so it's just automating the wiring and the the injection all right so here let's go further so you know that publisher usually we don't rely on planes track like that from the main service perspective so we Define that I publisher interface publisher and it has a method called publish and then our main service will depend on the abstraction well rather than the um implementation so here instead of the pointer I publisher we can just rely on that interface so there's no need of the pointer because it's an interface so if we run that it's probably not gonna run because it doesn't know which interface which implements implementation you want for that um interface so you can see here it's complaining said it can't find um don't really know what to use so that's fine so for that the FX framework created um annotation so we will take we will use that so I'll show you how so basically in your each sentence of FX provide you can annotate your provider so it's just FX annotate so we will annotate our Constructor so there you go so it's just a wrapper basically and we will annotate as and then FX dot as is simply an interface casting so we're going to tell the dependency injection framework that the new publisher will give you a pointer publisher and it will stand as that interface essentially so this is how you buy an interface to implementation at runtime so if we run that program again there you go so it's not really complaining conceal the line above at the beginning it it does say which annotation you use I can just change that to make sure it run it runs properly so if you change hello hello to goodbye and there you go so now you know how to buy an interface basically because most of your programs are probably depending on interfaces and not um brutal um structure structure I mean if you if you do things correctly at least it should be like that all right so let's go further and complexify a little bit the use case so now I'm publisher doesn't have only one title let's say it takes a slice of titles you go so I'm just doing some grammatical changes so you can see a variadic function so if you don't remember what's a variety function you just um function with the where the arguments are not the number of arguments are not set so you can have as many as data as you want in that Constructor now and you can see here um I'm just doing a for Loop where I'm going through the titles so then you can print Edge title so nothing really um magical or anything so [Music] um so it's a little bit tricky there because we have a provider function which gives you a title goodbye and if I naively create another title so let's just put hello let's see how the framework will handle it because it's expecting a slice of title so you can see that it's not really happy because it doesn't know what to do because it was expecting your don't really know how to inject those dependency so effects develop something called um parameters tags and result tags so let's have a look together so all right let's just do some indentation cleanup for you to see it better so essentially your publisher is requesting multiple titles and titles are required by those publisher so it goes that way so the requester will be um expose some parameter param tags so Pam text you can just give a string so we're gonna call it group titles group is important there is a feature from the FX framework so the workers to a new publisher will have param tags and then the provider so I need to add that annotation layer and then the provider will have result tags meaning that they will supply for those param tags so it's got wizard tags and then you can see I'm giving the exact same group name titles and then I need to do exactly the same with the other title provider as you can see it's getting a little bit hairy with the indentation and it's getting quite complex but we'll refactor and we'll discuss that later so bear with me all right let's run that so hopefully it should be running smoothly so you can see there the hello and goodbye injections so that works like a charm assuming what you know what you're doing it works pretty well with the result tags and the parameters all right so as I said it's uh it's not really easy to read those providers and stuff so you can just create some helper function for yourself so I'll show you how some those annotations are just a block of Provider there you go so it's Loosely um type so we can just create a very generic annotation provider so in our case it's going to be providing components for the Titan titles group so let's just call it title component so it can take any and then it's gonna render any other to start with there you go so it's actually taking just a string so I could have just put title string as argument oh yeah there you go let's just change it yeah all right so title so it's gonna cast that string into the title tab so then here we can just invoke that title component so the second one was goodbye and the first one was oh yeah let's just write it out they can see it's running as it used to all good and then the first one was hello there you go so really cheap and clean refactoring to smooth a little bit that wearing part that wiring structure okay so you can see that it's highly scalable then reusable so if we just introduce another one so you can see how the dependency framework just inject what you want and it does it really well there you go a quick observation so here title the type title is just an alias for string but it could be an um interface as well so you do exactly as we did with the publisher interface so you would just have to annotate those provider annotation with the FX dot as and then new interface so it's highly composable essentially all right let's have a look to pros and cons for that framework so obviously just my um personal opinion from what I've seen and what I use on a day-to-day basis so if you want to challenge any of those points please feel free to do it in the comments and actually I really encourage you to um discuss with me if you disagree of you if you have a different point of view because this is how you can sharpen your um the the way you express yourself and so on on the technical point so it's really good for you all right so let's start with Pros so it was an easy wiring as you saw it just it just work as intended so you just provide a a Constructor and then your receiver just wiring it so you don't have to um you don't have to think about it it's just doing it for you and it doing it well it can handle all the basic cases we needed as in to start with is is casting to interface depending on interface and also wearing multiple um of a Kind multiple of a type so it does it really well and in most services or microservices that you will have to wire it will handle them really well so that's good um as you saw multiple times when I was not doing things correctly in the example the framework feedback it did speed a lot of Errors so we didn't spend much time just reading them but if you stuck it's really helpful because it does telling you what's not injected and what's missing or if there's multiple injections for the same type so it's really good and the rear stability so you saw the quick refactoring edit for the group so you can imagine you can end up with a pretty neat and comprehensive writing section if you need right so I have an um explored modules with you but I invite you to have a look on the GitHub page about those reusable module all right now comes so it does introduce more in direction as in if you're just using a native go wiring it's like it's really easy you declare one thing and this thing goes there and then you declare another thing and goes there and so on it's like really simple to read right but it's just when it's getting large it's complicated and has a lot of wiring in every direction but that framework does introduce an extra layer of connected cognitive load as in you have to understand how dependency injection works and how the framework itself Works in order to make it work as you want otherwise you end up with a lot of errors and it's not running so it can be frustrating yeah so it does make it not big enough friendly so if I would have to and Border Junior I would for its first go online program I would not show him her that framework because there's a lot to take on board um I briefly mentioned it at the beginning of the video um if FX is like running an application for you and it has the application lifecycle management it does add complexity and it it's not solely a dependency injection framework as is and it's a bit of shame because as you know we software Engineers like to have um Atomic things like framework does one thing this remark does one thing so that's why the example with the HTTP server is bothering me and it's not beginner friendly because it's not the easiest thing to understand so um if I will have to do one uh dependency injection framework was totally separate it but if you read the documentation and the design process of that library that framework it does kind of make sense but it just as in um not all the program or not all the software you write are applications like runnable applications some of them might just be like um pure Library like the Siri we um you saw about the ASCII train so you might need a lot of dependency injection there but you might not have something to run but just to build which is um not annoying but I guess I guess modules are part of that answer so we can explore that in another video and then it's not compiled them as you saw it was crushing it was trying to run and crash so that's a problem if you doing any container technology and you're not really careful you could just put some server on downtime without uh really noticing because because if it would be compile time you can run the compilation during your tests phases and then it would not compile so then you will not deploy a faulty application in your Protection Service which is um got bad okay so as promised here is the link for the GitHub code from the very example you just saw so then it's easier for you if you just want to copy paste or if you want to have a look by yourself but I really encourage you to just try it from scratch by yourself it's way much easier for you so that's why I've never done it but I've received commands from one of you some of you asking for the code so there it is hope you enjoyed that video about the dependency framework FX and I'll see you next week happy coding bye [Music] thank you
Info
Channel: Panpito
Views: 6,823
Rating: undefined out of 5
Keywords: Uber, Golang, FX Framework, Dependency Injection, Code Modularity, Testability, Maintainability, Learning Golang, Programming
Id: UnrAF8FwfXU
Channel Id: undefined
Length: 33min 9sec (1989 seconds)
Published: Mon May 08 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.