Share code in F # Azure Functions project

I am currently developing F # Azure uinsg VS Code features, and I set out my project as such:

ProjectFolder -> Function1 -> function.json -> run.fsx -> Function2 -> function.json -> run.fsx 

Given that F # does not support folders, what is the best way to share code (especially types) between two functions?

+5
source share
1 answer

The F # compiler supports folders just fine. You can keep in mind the fact that Visual Studio by default does not allow adding folders to F # projects - this is a true fact. But if you manage to add folders to the project file (either manually editing the file or F # Power Tools ), then the F # compiler will not have problems with them.

But this is still irrelevant to your case, because scripts ( fsx ) are not the same as compiled modules ( fs ). Scripts do not have a project for linking them together, but instead must #load each other to use each other. And #load can be done in any way.

For example, you can lay out your code as follows:

 ProjectFolder -> Common -> Helpers.fsx -> Function1 -> function.json -> run.fsx -> Function2 -> function.json -> run.fsx 

And then, in Function1/run.fsx , load the Helpers.fsx file using the relative path:

 // Function1/run.fsx #load "../Common/Helpers.fsx" let x = Helpers.someFunction() 

As you can see from the above example, a script loaded in this way will appear in the script host as a module named after the script file.

It works, but over time it will become very dirty. I highly recommend using precompiled code instead. That way, you can put the common code into a shared library and reference it from both functions.

I highly recommend this latest video from NDC Oslo . He speaks very well about these things and much more.

+5
source

Source: https://habr.com/ru/post/1269768/


All Articles