Domain Modeling for Humans and AI

Modeling Plant Placement

Domain Modeling for Humans and AI

Lesson Description

The "Modeling Plant Placement" Lesson is part of the full, Domain Modeling for Humans and AI course featured in this preview video. Here's what you'd learn in this lesson:

Mike models plants by linking seed packets, plants, and their positions in a bed. He stresses accurate domain modeling, updates code to reflect this, disables redundant code, adjusts relationships, and ensures proper data persistence.

Preview
Close

Transcript from the "Modeling Plant Placement" Lesson

[00:00:00]
>> Mike North.: So now let's work on the plant. We need to model this as well. Or we already have a plant here so we can look at this. We've got a seed packet, accent, color, an icon, path, bunch of good stuff here. And it turns out that when this app boots, if you look at the logs, we create a plant for every seed packet here.

[00:00:27]
So, you could think of these as a little different than the way we've sort of set up our domain model, right? Here we've said a plant has a position. Really what I've done here is I've said, all right, seed packet refers to a plant kind of like this, and then we've got a plant placement here.

[00:00:50]
Sort of what this lets us do is if we wanted to, and I think I'll continue with this course since I've got the beginnings of the solution anyway. What it would let you do is say seed packet can be seed packet. We have a freestanding domain model here where, sorry, it's zero to many.

[00:01:15]
Where this is, sort of, entirely self contained, where we could say there's a chance plant, we might need to model different things here. It's very similar, but we're gonna have a little bit of duplication here, if I'm honest. The key difference is gonna be we have something that represents the core data about the plant and then we've got something that refers to it as being in an XY position.

[00:01:44]
And what this lets you do is you can kind of like clone the plant and you end up with another plant. But this still represents what we were describing before. Like there are many plants that refer to a seed packet, so we can carry that through. If we look at plant, it still refers to a seed packet, we will just need something that it's like a placement.

[00:02:14]
>> Mike North.: And we're just gonna grab bed as a starting point. And this will be plant placement.
>> Mike North.: And this once, we want to refer to a plant.
>> Mike North.: So we're saying on plant, I'm going to introduce this concept of placements. We don't need width and height, so let's finish that relationship there.

[00:03:05]
Over here we're gonna say, no, I think we actually got it here. Presentation, we've already got the many to one with the seed packet. You know what, we can just use plant here. I'm gonna back this up. I like your domain models a little bit better. So we still have the concept of a seed packet referring to many plants.

[00:03:30]
We're just going to have to get this right as we feed it up to the UI that wants to see this in terms of item placements. What we're missing on plant though is some concept of a coordinate. I don't see something here yet. All right, no problem, we already have a value object for this column, xy coordinate.

[00:04:01]
>> Mike North.: Position is xy coordinate. So that's the xy. I don't see anything here.
>> Mike North.: Sorry, I'm going to get rid of this too. I don't see anything here that relates to like this plate belonging to a bed, so we need to add that. That's more of a relationship.

[00:04:27]
I actually see everything's column, column, column, except for this, so we'll add it here. And this is similar, it's also gonna be. Let's think about this. Is this many to one, one bed, many plants. Yep.
>> Mike North.: Bed, bed, bed. And then we'll also call it plants.
>> Seth.: Isn't that a one to many?

[00:05:08]
>> Mike North.: So this is the other side of a one to many. That's why we said many to one. Sorry Seth, I take your suggestion right to heart here. There's going to be a one to many on the other side. So when we do bed, we gotta do OneToMany,
>> Mike North.: Plant.

[00:05:39]
>> Mike North.: And how do we fetch that relationship bed?
>> Mike North.: Kind of like that. So we've got a many to one because the garden above this bed has many of me the beds, and then I, the bed have many plants below me. So the first word here is always like what are you in this relationship?

[00:06:08]
And there's something else we had to wire up here. No, I think we got it many to one and then we've got many to one here as well. So you can think of plant as almost being like a many. It's sort of a, think of it almost like an edge in a many to many relationship between seed packets and raised beds.

[00:06:32]
Like many seeds of a given type can go into single raised bed and a raised bed can have plants that came from many different kinds of seeds. So this is really like that join table but with extra data attached to it. Great, now let's see if we can wire this up in our API contract to feed it back up to the UI.

[00:07:02]
Actually we got to go back to our garden service and let's create a couple plants in here. So we've got a six by six raised bed. We're gonna need a plant repo,
>> Mike North.: And import that, and then we're going to say plantRepo.create satisfies deep partial land, and what needs to go in here?

[00:07:46]
Position,
>> Mike North.: X and Y are zero, the bed.
>> Mike North.: We need an accent color. So I think what we want to do here is let's hard code this for now and then we can pull it from real data. We'll make this bright red.
>> Mike North.: There's our accent color.

[00:08:35]
>> Mike North.: And you know what, maybe we should just grab it from a seed packet. Yeah, that's what we should do.
>> Mike North.: That has all the data that we kind of want here. So what we can do is say we've got our seed packet repo. We could say seed = seed repo, or SeedPacketRepo.findOne where: id is, and then.

[00:09:22]
>> Mike North.: Or let's not use the id. Let's say find where the. Let me get rid of this so we're not scrolling through a tiny little slit there.
>> Mike North.: I just want something that'll survive the reloads.
>> Seth.: Category tomato.
>> Mike North.: Yeah, category is tomatoes. And we'll just grab the first one.

[00:09:45]
I like that. Find one where category is tomato. And that we probably need to await, indeed.
>> Mike North.: Great, so we've covered that case and now here we can say seed.presentation, accentColor and icon path. And we can add a whole bunch of other information t. Variant, we can say seed.category.

[00:10:37]
Name, we can say seed.name.
>> Seth.: We need to await it as well.
>> Mike North.: We need to what?
>> Seth.: Await the.
>> Mike North.: Not on the create, because this is just kind of creating it in memory and then we have a separate step to persist it. So this is what would allow you to say, I'm gonna create a bunch of things and I want to maybe as one transaction, persist them all at the same time.

[00:11:04]
>> Seth.: Why wouldn't we just relate that plant to the seed?
>> Mike North.: Yeah, [CROSSTALK].
>> Seth.: Duplicating all this data in the data.
>> Mike North.: You're absolutely right. Let's do that. So we've already got presentation and planting distance and description. Yeah, I like that, accent color. Certainly need the bed. I think it really just comes down to this, right?

[00:11:28]
>> Seth.: Coordinates and the relationships.
>> Mike North.: That's great.
>> Mike North.: We've got the bed, we've got the seed, we've got the position.
>> Mike North.: And we're not using this because. Well, first let's save that bed up here.
>> Mike North.: So we've got this is the persistent version of bed. Really, this is the pattern I'd use, for real.

[00:12:07]
Something like this, where you're like, I've got one version of that that's sort of my params. And then here we're saving it.
>> Seth.: Rename the bed one there.
>> Mike North.: Yeah, thanks. And so this it's just gonna get its id populated and all of the things that sort of happen when it's really stored in the database.

[00:12:30]
And then down here we've got bed. We can make this even slicker, call this seed packet. So we can even do that. And now we just need to persist, plant1.
>> Mike North.: This is, wait, cool. Just checking to make sure. Yep, we can get rid of all this crap here cuz the seed is the source of truth here.

[00:13:13]
I love it. And looking at our logs again, make sure that everything seems copacetic. SQLite constraint. Plant position x not set. What could this be? I see we've got position X, position Y, these are the only parameters that are being parsed in. The first thing I would check is in our app or data source, XY coordinates in there, plants in there, so that makes sense.

[00:13:57]
Position, that is xy coordinate, which is an entity.
>> Mike North.: This is quite odd. Let's check x, y coordinate. Make sure that this is set up right. X is a number, Y is a number, can't really get too much simpler than that. Hi, folks. Figured out what the problem was pretty quickly.

[00:14:34]
So you may have noticed that I mentioned my starting point code had this concept of a plant that was sort of very redundant with this concept of a seed packet, where on boot, I don't know if you'd been paying attention to sort of the scroll of logging of object creation here.

[00:14:54]
A plant is created for every seed packet that is read from that seed's YML file. But now that we've stated well, plants have a position and they refer to a seed packet. And you can think of a plant as representing the placement of a seed in a bed, which that is some excellent domain modeling there.

[00:15:16]
A plant represents a placement of seed in a bed is something that gardeners would certainly understand and agree with that that is the correct way to think about this. We're going to have to disable that code. So we have this file in our server module, it's called seed-db.

[00:15:32]
And you'll see here's where we, you know, we find all of the seed packets and this happens. This whole function here, in fact is like kind of what we have to disable generate plants. So we'll disable that. We'll find where it's used down here. And like, instead of after loading the seeds into the database, we generate one plant per seed.

[00:15:56]
We're now saying this concept of a plant models the x, y coordinate of where we're putting our planting, right, our seed that has grown up. And so if we hit save there and we can get rid of some of these imports because we don't need them anymore. And we go back to our plant service.

[00:16:16]
There's also, like, generate plant from seed packet. We can get rid of that as well, because that doesn't really define exactly how this is going to work anymore. And we can get rid of a bunch of this stuff because that was mostly necessary for that purpose. Now we can go back to our garden service, and what you should see is that error has gone away.

[00:16:41]
So the thing that was a little tricky there is, of course, we're just working on this plant. Clearly we have an XY coordinate where we're placing this plant in position. But that error message was from one of those plants created on app initialization that did not have XY coordinates.

[00:17:00]
And so, of course, this ends up being persisted position X just comes from plant.position.x. And if we look at our database, we can see plants. We've got one record in there, and it is in fact at x0, y0. And so we've now got garden, beds, plants. Let's not forget the relationships bit.

[00:17:26]
So when we're getting all plants up here, we've got this relations object, and we can say, well, in this case, true is sort of the termination of a family of paths, if you want to think about this. So, what we're saying is go get beds, and this is the end of what you should go and get.

[00:17:47]
But we can instead say plants. And so now we're gonna get, when you fetch a garden, we're gonna get all the beds, and we're gonna get all the plants that are in those beds. And we should start to see that coming through. But remember, we hard coded that placement item when we were preparing our API response.

[00:18:10]
And so once we do some work there, we're gonna go to our browser, we're gonna hit the API, and we should see that our tomato plant, whatever, the first plant that we found, is being presented in the placements array.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now