Can I split my Pulumi code into seperate classes/f...
# dotnet
w
Can I split my Pulumi code into seperate classes/files?
l
Yes. My Pulumi project defines resources in 5 separate classes/files, invoked from the primary class.
w
Do you have a code sample? Is it basically just copy the main class and the code in that?
l
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
How does your second cclass look?
l
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
It's also perfectly valid to use a static method if you want.