Check out a free preview of the full Rapid Application Development with Code Generation course
The "Generating Layers" Lesson is part of the full, Rapid Application Development with Code Generation course featured in this preview video. Here's what you'd learn in this lesson:
Lukas discusses the differences between generating a single template output and generating all of the templates. A demonstration of defining schemas, inserting them into an array, looping over the array, and reducing them to an output is also provided in this segment.
Transcript from the "Generating Layers" Lesson
[00:00:00]
>> We've seen how to generate a cell. What do you think goes into generating a layer? So, with that said for a second Think of something, and now I'm going to answer it and you can check your answer. The difference between generating a cell and a layer. And so by that, I mean, a single output of a template versus all of the templates for a single or for a collection of entities is ultimately just a matter of iterating.
[00:00:53]
And so if we go to our layer generator start, we are going to see how to do this. And what you'll see here is you start to poke into this is that it's kind of almost where I left off last time in the sense of we now have more than one schema.
[00:01:24]
And within that now we can group these together into a domain. So, for the sake of conversation semantically, let's say that a domain is a collection of entities or schemas. And then from here, the question is how would we generate a layer? So how would we generate a service layer?
[00:02:03]
How would we generate a state layer? Well, we know how to generate and iterate a single template. What would it take for us to generate more than one of those things? Well, if we go up into our generate layer function, yes, this is a function. It's just as a placeholder and right now, we can update this to do something.
[00:02:43]
And that is we are going to take a generator. So a generator works on at the layer and the domain, which is a collection of entities or schemas. And we're going to iterate over that domain. And for every entity, we're going to generate a template. So we're just taking a very simple thing and we're just iterating on it to create a larger thing.
[00:03:11]
So from here, let's go with, Domain reduce, and we're going to go with code and we know that every time it iterates, what we're actually getting is a schema object. And then from here, we're going to Update our code. And I'm gonna just put a placeholder here for just a moment.
[00:03:48]
And then we're going to return code. And within here, we're gonna pass in an empty string. And so the magic is going to happen here. So what I'm gonna do is because I foolishly deleted it, is we're gonna go code class language typescript. And then I'll close this out.
[00:04:29]
And we need to generate something in between this. So this is kind of like a string sandwich. So we're going to take our generator, whatever that is. So this is the beauty of this, so I can even type this it's a generator And I could type it, but I'm gonna not type it because it would distract me for just a moment.
[00:04:59]
So we'll go generator and then we're going to go generate good times come on. And we're gonna just pass in schema, config and what this is going to return is a template or this is what we want to render, template. All right, I'm kind of nervous, kind of excited.
[00:05:30]
But let's see what happens when we open up this window. All right, let me do this in a standalone window so we can see it. So now what we have is, courses, services, lessons, services and assignments, services. And what if we wanted to generate the state layer? Well, let's go active.
[00:06:08]
Dive inner HTML equals generate layer and we'll go reducer generator and/or domain, config. All right, how long did it take me to type that line? I don't know, like 10 seconds maybe. And so we'll save this and let me just delineate this here. So this is where I'm getting paid by the hour, and I realized that I'm working too fast.
[00:06:54]
And so I need to slow my roll to justify my timesheet. I'm just kidding. So now, let's flip back over here. And data layer, all the way down to the state layer. How long did that take? Well, hardly any at all, any time at all, but more importantly, what did it take me in terms of effort to go from and I'm just gonna open this up and From a template, I guess we're doing more than one template.
[00:07:47]
What did it take to go from here where I'm doing them one by one to this, where it's generating it all at once? It took me exactly, well, seven lines of code I believe [INAUDIBLE] 7 lines of code. So I went from generating a cell or a template to a layer in seven lines of code.
[00:08:27]
And all we're doing is defining our schemas, putting them into an array, looping over, reducing it into an output that we're then putting on to the screen. So from here again Let's go with challenges. Now start the timer. We've just realized that, we're gonna add in a new feature.
[00:09:04]
And we're gonna have the ability to do challenges. Okay, that's awesome. And how soon can we have something ready to demo for the stakeholders? Well, notice, I've defined a schema and I've just added it into my domain. And if I come back here, now I have a challenges service.
[00:09:37]
And if I go to the bottom I have a challenges reducer. How long did that take me? Not very long because I'm thinking not about building one widget, I'm thinking about building 10,000 of them. And so this is the difference of moving from wiggling something by hand with a pocket knife to plastic injection molding on an assembly line
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops