External Folder

One of the strengths of CodeStencil is the ability to manipulate external files and folders.

The External Folder node type allows you to pull in a folder containing files during your code generation cycle and, while doing that, also update the contents of the file(s) to allow you to bypass manual updates to the said files. 

 

This node type enables CodeStencil to copy file(s) from a folder to the folder the node is created in.

From the screenshot:

(1) This is an "External Folder" node and it has been created under the Model folder.

(2) In the node details you can see the source folder the files are going to be copied from - c:\codestencil\generated\ef-scaffold-folder.

(3) This shows the target path of the copied files

(4) In the comments section of the node form, we can see some descriptive text:

 

(a) The [ExcludeLinesContaining] string lets the code generation engine know that there is some post-processing required for the copied file(s). In this case, some lines have to be removed.

(b) The [%DB_CONTEXT%] indicates the file(s) that need post-processing. So, during code generation, this string is expanded ( or transformed) to get the actual name of the physical file. In this case, the file we want to manipulate is the DBContext file. Because a Stencil does not know what the runtime file is named, we have cleverly used an Expander - [%DB_CONTEXT%] that always contains the name of the current DBContext file.

To reference more than one file, all you have to do is list the files separated by commas, e.g. [%DB_CONTEXT%], File1, File2 ...

(c) These lines of text are the lines we want to remove from the derived file in (b)

#warning To protect potentially sensitive information

optionsBuilder.UseSqlServer

Scenario

A typical scenario that describes the feature mentioned above can be seen (depending on your version of Entity Framework) as we use Entity Framework to scaffold entity-type classes.

The DB context file is always generated with the connection string in the OnConfiguring method. Of course, you can manually remove this after code generation, but one of the strengths of CodeStencil is the ability to manipulate how your code is generated.

CS_clip0204

After that node has been processed, this is what your code will look like:

CS_clip0205

 


Was this article helpful?