pattern never provides an example of subclassing a
ComponentResource
to specialise a more general abstraction. Is inheritance discouraged?
l
little-cartoon-10569
06/06/2024, 4:45 AM
Composition is the normal pattern. Inheritance can be used, but it's just not as common.
l
loud-france-35553
06/06/2024, 4:48 AM
Thanks @little-cartoon-10569 - I'd assumed as much, but figured there might have been a reason why it wasn't shown in the documentation. Is the usual compositional pattern to create a new more specialised type/component resource, and then have the new type simply carry the more general type within, assigned to one of the instance attributes?
l
little-cartoon-10569
06/06/2024, 5:10 AM
Yes. Since you can't inherit from the cloud resources (what kind of a bucket is a kind-of S3 bucket?), and all you can do is configure them (a self-cleaning S3 bucket is an ordinary S3 bucket, with certain lifecycle rules applied.. it's not a subclass of an S3 bucket) or compose them (an auto-backed-up bucket is an S3 bucket with another S3 bucket and a lambda that copies any changes based on certain events), then composition is generally more suited. Plus, it's easier to grok when reading the code. Inheritance necessitates a lot of boilerplate, classes in other files, and related messiness.
l
loud-france-35553
06/06/2024, 5:31 AM
Yep, makes sense. Thanks for your input @little-cartoon-10569 🙂
No matter how you like to participate in developer communities, Pulumi wants to meet you there. If you want to meet other Pulumi users to share use-cases and best practices, contribute code or documentation, see us at an event, or just tell a story about something cool you did with Pulumi, you are part of our community.