https://pulumi.com logo
#dotnet
Title
# dotnet
w

witty-vegetable-61961

09/15/2023, 3:11 PM
Can I split my Pulumi code into seperate classes/files?
l

little-library-54601

09/15/2023, 3:59 PM
Yes. My Pulumi project defines resources in 5 separate classes/files, invoked from the primary class.
w

witty-vegetable-61961

09/15/2023, 4:01 PM
Do you have a code sample? Is it basically just copy the main class and the code in that?
l

little-library-54601

09/15/2023, 4:08 PM
The Program file looks like this:
Copy code
class Program
{
    static Task<int> Main() => Deployment.RunAsync<ApiResourcesStack>();
}
The constructor of
ApiResourcesStack
(the class I referred to as "primary") instantiates the classes defined in other files, e.g.
Copy code
var sqlResources = new AzureSqlResources(rgName, envSuffix, envTagName);
Typically, the object returned from one of those
new <class>
calls has properties passed to a subsequent
new <class>
call. That primary file does not itself define/create any resources - it relies on the other classes. But that primary class does handle the
[Output]
properties. Of course, this is just the way that I've chosen to do it. There are assuredly other ways.
w

witty-vegetable-61961

09/15/2023, 4:09 PM
How does your second cclass look?
l

little-library-54601

09/15/2023, 4:12 PM
Either directly in those classes' constructors, or in static helper functions within those classes, they just use standard Pulumi code, i.e.
Copy code
new <PulumiResourceClass>("name", new <PulumiResourceClassArgs>
{
    // set properties
});
IOW, basic C# using the Pulumi constructs, types, API, etc.
s

stocky-restaurant-98004

09/15/2023, 5:29 PM
It's also perfectly valid to use a static method if you want.