Implementing AggregateRoot, Entity, ValueObject | CA & DDD From Scratch Tutorial | Part 12

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so hi everyone amihai here today finally we're going back to our code base and starting to write some code again if you're like me and you much prefer coding then you're going to enjoy this one and the upcoming videos we're we're basically going to write a ton of code implementing much of the theory that we talked about up until now so what I'm going to do is I'm going to go to The Domain project and inside the comment folder create a new folder that's called models and inside here what I'm going to do is create the base classes for the different domain objects that we're going to have later so for the value object for the aggregate root and for the entity so let's start with the value object so let's create here a new file it's called value object okay what we're going to have over here is basically the base class that's all the value objects in our project will inherit from now if you remember value objects are considered equal if their values are the same so for that that we have some inspiration let's just put over here some class that we can use for reference just for us to have something for reference while we're implementing so let's say we have here the price value object let's say here that we have the amount yes and we have the currency and we have a Constructor that receives both of them and initializes it now two price objects are considered equal if they have the same values in these two properties so the way this looks from the value object Base Class perspective let's create it over here so we have our public abstract class value object okay and what we're going to have over here is public abstract in numeral of object and let's cut let's call this yes get equality components and basically what this is is a method that each one of the value objects in our code base will have to implement so let's say value object and the way each one of the derived value objects is going to use this method is basically returning the properties in a defined order so let's say for example yield return amount and then the currency okay now we're going to override the equals method in the base class and basically what we're going to do is we're going to called the get equality components on the two objects and make sure that they return the same value that way we're comparing the property values so let's override our equal methods so let's say override and let's start with equals and basically what we want to do over here is we want to say if the object yes let's say if the object is null or that the type of the object if the types aren't equal then we want to return false otherwise let's cast it to a value object and then this is also the right thing let's just put this on a little lines okay so again what we have here is basically over here we're making sure that the two objects are from the same type then we're casting it to a value object that the value object has this method over here that will return the properties in a specific order then over here we basically check the values of the properties in both of the objects if it's the same then it's equal if it's not then it's not equal next let's move on to overriding the equals and not equals operators okay so over here we're going to have public static rule operator equals equals and we're going to have value object left value object right and simply what we're going to do over here is call our equals method that we already defined so we can basically say return equals pass it left and right let's also add the not equals which is going to be not equals the next thing we want to do is overwrite the get hashcode method because we want two value objects with the same property values to return the same hash code so for that let's say override and let's say the get hash code yes and what we're going to have over here is basically each one of the properties we're going to call its get hash code and then we're going to do sore between them and return that value so let's say get the quality components yes almost and over here we're going to say if da X is not null get its hash code otherwise return 0 and then let's simply do soar between them so let's say Aggregate and yes like so so all we did is we overrode what we need for the two value objects to be equal and have the same hash code when they have the same property values okay another thing we might want to do is override the I equatable interface with passing a value object and then we can implement the method that they want us to implement which is equals and here we can simply do the same thing that we did before as so now what you're actually going to do is simply go online search for Value object based class and download the first one that you see this is written more or less the same in all the implementations so this is just for us to drive the points home but you definitely don't need to remember this or write this from scratch every time that you need a value object okay so we got rid of this we have the value object now let's move on to the entity what we want over here is similar to the value object luckily it's a bit less code and what we want to do is basically say that two entities are equal only when their ID is equal so [Music] almost let's say public abstract class entity each one of the entities is going to have some ID which is a t ID and we want to say that the tid is not null let's say not no okay what we're going to have over here is our ID so yes like this but we're not pointing this because we're going to set it in the Constructor so let's say Protect it and let's say entity this receives the ID and sets it like this now what we want to do is we want to say that two entities are equal if their ID is equal so again let's override the equals method and let's say that this returns true only when the other entity is from the same type and what that their IDs are equal so yes next thing we want to do is override the equals and not equals operators like GitHub copilot suggested so yes let's also override that not equals right so same drill this is returns true when they don't equal and also over here what we might want to do is we might want to implement the I Equitable interface so I equatable and let's pass it entity of tid like so let's implement the interface and what we want to do over here is basically the same thing so return and let's Let It complete what we want yes and last but not least is overriding the get hash code so this is what we want again we want two entities to return the same hash code if their ID is the same that's it for now we still haven't learned about domain events and other things that we're going to need in this Base Class so for now this is enough for this stage the next thing we want to do is create the aggregate root so let's say average root what we want over here is almost let's get rid of the unneeded parts and let's explain it right so basically the average root is also an entity it's sort of like a wrapper around the entity and all we're doing is we're calling the Base Class okay so this is our Agri fruit alongside our entity and our value object now when we start implementing our address or entities all we need to do is derived from the correct Base Class and we're going to have the equality the guest hash code whatever we need for it to follow the domain driven design principles that we talked about so that's it for this video I really hope you enjoyed it and you learned something new if you are doing this for the first time if not then let me know how this is compared to the way you do it in your company or in your project so that's it I really hope you enjoyed it and I'll see you in the next one
Info
Channel: Amichai Mantinband
Views: 29,138
Rating: undefined out of 5
Keywords:
Id: weGLBPky43U
Channel Id: undefined
Length: 8min 1sec (481 seconds)
Published: Wed Oct 26 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.