Check out a free preview of the full Rapid Application Development with Code Generation course

The "Generating Stacks" 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 walks through how to generate an entire stack by defining a collection of layers, iterating over the collection, capturing each layer, and reducing them into a single piece of code.


Transcript from the "Generating Stacks" Lesson

>> Finally, the question is, how do we do an entire stack? Well, hopefully at this point, you've heard 10,000 variations of the same thing. And you've started to do some pattern recognition of your own, and you know where i'm going with this. So let's go to our stack generator start.

And let's build this out. So up to this point, this is pretty much where we left off. So we have our domain, and we have our layer. So, if we wanted to generate more than one layer, how would we do that? Pausing for dramatic effect, [SOUND]. Well, I have confidence that you have probably figured out, at least you have some idea what is coming next.

So if a layer is a grouping of cells, or entities, or domain objects. A stack is a grouping of layers. So, we're going to go, and we're going to go, layers dot, wait for it, reduce, and from here, we're going to accumulate on the code layer. And within this, I have forgotten some parentheses.

We're going to go code +, Equals something, we'll fix that in just a moment. We're going to return code, And we're going to start with an empty string. And so with this, We need to define our layer before we go any further. So what does a layer look like?

What would this look like? Now remember what I said, I gave everybody hint earlier, a generator is tied to a layer. So you typically will use a single generator to generate a layer, or a generator to generate a sub layer. So within this, what we need to do, is we need to define our layer.

And we're going to give it two properties. So the first thing that we're going to do is give it a name, data layer. And then we're going to define, can you believe it? The generator. So, service generator, and we'll do this twice, so we'll go state. Or rather I really should have waited for dramatic effect as well to just be like, how hard would it be for me to do another layer?

Wait for it. But I am just going to play it straight here, and just do both of them at the same time. But again, if I asked everybody on a scale of one to 10, how complicated Is this data structure? Well, it's [INAUDIBLE] G, it's an array with objects that have two properties on them.

That's a two, maybe even a one. Well, that's the point. And so from here, let's go ahead and let's just add for the sake of UI UX, we're gonna add in, A header here, so H2. And within this, layer name, and then we're gonna do one more line here.

And I realized there's a couple ways that I could do this, in this case, this is the way the wind was blowing today. So, we're gonna go generate layer, and in here, we are gonna pass in layer generator. And you could absolutely type this, and we're gonna go domain, config, and we're going to save this.

So all I've done at this point, up to this point, is defined a layers collection, super simplistic object in there. And then i'm just reducing, or iterating over the layers. And then for each layer, i'm capturing the layer itself, and then reducing all of this into a single piece of code.

And remember code is what? Strings, lots of strings, and so, let's open this in a new window. Yeah. I feel pretty good about this. How long did that take me? So if we go from template, To layer, to stack, we're just taking the same approach for the most part, certainly between the layer, and the stack.

And notice how, for the layer in the stack, the code between these two functions are almost identical. Very similar, and they're not doing the same thing. So don't think that they're duplicated, but conventionally, they're very much the same. And so this is when I say that I realized that when I started to converge to best practices in first principles, that I was doing a lot of the same thing over and over in very small increments.

And this is where, when I say that mastery is reductive, this is, I think we're seeing hints of this, that we are literally generating entire pieces of an application in well, we got 50 lines here. And if we go back to our generator, or our engine, rather, and so this is our name variations.

55, 56 lines, and I really do believe that the core of this, comes down to this file right here. And we're just iterating over this. And just like we're building a code snippet, we're just taking the pieces that need to be changed, and we're building a mold around that.

This is working code for the most part. If I were to go and build an angular or react application, this is the code that I would be handing off to you, the client. And so there's nothing about this that I have to apologize for, or make concessions of like, we'll fix that later.

But rather, this is equivalent. It has parody too, what I would consider to be enterprise grade production code.

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