A REAL Benefit of DDD in Go! ~ Domain Driven Design in Golang Example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so in this video I want to go over a real quick benefit of DDD in go and obviously this would apply to other languages but hey let's jump straight into it so if you have a Endo like you all would have in your go projects such as this one here you would have seen some code like this right probably in your lifetime which sets fields on some type of object so um setting a creator that field for example or um you know any other logic in here that's related to some type of objects right and you've probably also seen some type of database code or some type of data layer which does could have the same thing right so potentially you might have might have had developers set Fields like a created that in here or maybe some other fields they set in the data layer right um and that gets hard to maintain so if you've got a mixture of objects being updated or Logic on objects in your endpoints and in your data layer then obviously firstly if you change data layer you then have to migrate that logic into your next um implementation of your data layer and likewise say you have a grpc uh you need you now need a grpc implementation of a controller then obviously again like you have to Port that logic across and even just aside for that developers have to guess and sometimes work out why and where fields are updated and it becomes a bit of a pain to maintain um so this is why I really like DDD and this is why I wanted to make this video over a very quick benefit of that so uh this is all related to updating and saing and creating objects basically essentially so firstly you'll notice that my endpoint calls through to a service anyway so if we just jump to this service method now obviously like I said this is all DDD so I'm firstly just kind of initializing all of my value object types so I'm just moving stuff from like The Primitives on the transport layer over to Value objects um which I then use to create a new profile entity so that's quite simple it just accepts value objects and it turns them back to The Primitives onto a entity type so this profile one now you've all probably seen a type similar you've all got them in your application they all you know it's quite common stuff right but here's the nice thing about DDD so this is a call to my data layer here and we know that in this command this service method I'm creating a new profile right and we don't want to set any field related to creating a profile in the endpoint we don't want to set those in the data layer we just want a centralized place that can be recalled which is kind of objectoriented programming right that is kind of what DDD is quote unquote um so you'll see here that I call this create method now this create method will simply go and set any Fields required uh to create a profile so it will go and populate a new uid it will set the create up uh create that field as well and likewise you know you might have other methods for different Logic for example I've got one here to uh subscribe to a profile so this just goes and sets like a bunch of subscription related fields um and you'll also notice that you fire off events after these methods have been um set or called uh but obviously I'll make another video on events in the future um yeah so you'll notice here the profile is now created and now obviously I can pass this object as entity over to my data layer and it can just go and insert that object with the updated Fields now this is in my opinion anyway a really nice pattern and a really nice way to manage uh setting fields for your layers between transport layers um and also you know there's a lot of benefits in terms of you want to set up a grpc implementation of stuff you might want HTTP one you might have a requirement for a Lambda and again if you just call your service method you know that these fields will be created and you don't need to worry about you know having this created out field being set in your Lambda and aside from even just like your implementation detail like that just for developers in my opinion to know where to look to update Fields is a really good timesaver I've worked on projects where there's a real mixture between logic of setting fields and just general business logic in controllers and data layers um and it becomes hard to maintain and actually it becomes hard to test as well because there's not really any consistency over where things are set and what logic is where um and the nice thing about this of course as well is you can do a complete test you can firstly test all your value objects and then you can test correting your entity and set and also have a test for actually this exact method on What fields are set as well um and then likewise you can test that events are fired and things like that right so if you want a free and easy way to learn programming some of the top topics I talk about on my channel may sound complicated but there's actually a fun and easy way to learn about them so if you want a way to learn programming that doesn't cost thousands and thousands of dollars or take years of schooling brilliant is the best way to learn maths data science and computer science interactively it's a fun and interactive way of learning with thousands of lessons from basic to Advanced topics and they add new lessons every month so for example I've recently been looking into their llms course now obviously as we all know AI is a very Hot Topic at the moment so if you want an introduction to language models predicting the the next word uh or calculating word probabilities definitely check out the lm's course on brilliant I say it's a great way to get up to date with AI um in general so to try everything brilliant has to offer for free for a full 30 days visit brilliant.org V codes or click the link in the description the first 200 of you will get 20% off brilliant's annual subscription plan so yeah a very quick video uh but it's a real benefit of DDD um and I personally really enjoy working with this and go uh just with the Simplicity of kind of structs in go and receivers and just all the fun stuff that go has right um so definitely check out this pattern if you're looking to build maintainable software in my opinion it will help you um and of course please drop a like and a comment if this has helped and I'll see you all in my next one
Info
Channel: samvcodes
Views: 4,217
Rating: undefined out of 5
Keywords:
Id: UqyORsD4oWA
Channel Id: undefined
Length: 6min 2sec (362 seconds)
Published: Mon Feb 12 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.