It happens that we have to develop several services and deploy them in Azure. They are small, repetitive but different, often not very different. Why not use code generation techniques to simplify the development and implementation of these services? Let's see with .NET comes to meet us and helps us to deploy in Azure.
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Code Generation for Azure with .net
1. Code Generation for Azure with
.NET
Marco Parenzan
marcoparenzan/CodeGenerationForAzureWithDotNet (github.com)
2. Senior Solution Architect in beanTech
Microsoft Azure MVP
Community Lead 1nn0va // Pordenone
1nn0va After Hour
https://bit.ly/1nn0va-video
Linkedin: https://www.linkedin.com/in/marcoparenzan/
Marco Parenzan
3. Code Generation for Azure with
.NET
Marco Parenzan
marcoparenzan/CodeGenerationForAzureWithDotNet (github.com)
4. So another story from my love
for (un)successfull/(un)tested(?) technologies
5. • Faster upgrades
• Framework (.NET 6.0 anyone?)
• Cloud Services
• Enforce (best) practices
• Increase maintainability
Challenges for the modern cloud company
6. • Service vs. Web Service
• Servicebusiness logic
• Web Serviceprotocol adaptation
• HTTPS vs GRPC
• Azure Function vs. API App
• Develop in front of a component
• Better testing
• Single Responsability Principle!
• Business logic and protocol adaptation are two responsabilities
Value vs. Not Value
7. • “Code that generates code…”
• Reflection
• Powerful
• Difficult to maintain
• Difficult to customize
• DSL
• Powerful
• Difficult to maintain
• Risk to customize
• Code Generation
• Powerful
• Simpler to maintain
• Simpler(?) to customize
Dynamic (Meta)Programming
12. Building your own Fix
var value = 1 + 2;
ExpressionSyntax
SyntaxToken
SyntaxTrivia
var equalsValue =
SyntaxFactory.EqualsValueClause(
SyntaxFactory.BinaryExpression(
SyntaxKind.AddExpression,
SyntaxFactory.LiteralExpression(
SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1)),
SyntaxFactory.LiteralExpression(
SyntaxKind.NumericLiteralExpression,SyntaxFactory.Literal(2))
));
var typeSyntax =
SyntaxFactory.VariableDeclaration(
SyntaxFactory.ParseTypeName("var"),
new SeparatedSyntaxList<VariableDeclaratorSyntax>()
.Add(SyntaxFactory.VariableDeclarator(“value")
.WithInitializer(equalsValue)))
.NormalizeWhitespace(" ")
.WithAdditionalAnnotations(Formatter.Annotation);
var declaration = SyntaxFactory.LocalDeclarationStatement(typeSyntax);
13. [DiagnosticAnalyzer(LanguageNames.CSharp)]
public class WebApiControllerNamingConventionAnalyzer :
DiagnosticAnalyzer
{
string DiagnosticId = “WA0001";
string Description = "Controller type should end in 'Controller'";
string MessageFormat = "Type name '{0}' does not end in Controller";
internal const string Category = "Naming";
private static DiagnosticDescriptor Rule = new DiagnosticDescriptor(
DiagnosticId, Description, MessageFormat, Category,
DiagnosticSeverity.Warning, true);
// Add implementation
}
private void Analyzer(SymbolAnalysisContext
context)
{
var symbol = (INamedTypeSymbol)context.Symbol;
if (symbol.BaseType == null) return;
if ((symbol.BaseType.Name == "Controller"
|| symbol.BaseType.Name == "ApiController")
&& !symbol.Name.EndsWith("Controller"))
{
var diagnostic = Diagnostic.Create(
Rule, symbol.Locations[0],
symbol.Name);
context.ReportDiagnostic(diagnostic);
}
}
Custom Diagnostic - Analyzer
14. var script = CSharpScript.Create<int>("int x = 1;")
.ContinueWith("int y = 2;")
.ContinueWith("x + y");
Assert.AreEqual(
3,
(int)(await script.RunAsync()).ReturnValue);
C# Scripting / Interactive
15. • - Extremely verbose
• + (Extremely) Statically Typed
• + Extremely meaningful
• Can be optimizedStatical VS refactoring
• C#6+ Era of immutability
• This is why some C# syntax exists
• Partial classes
• Partial methods
Roslyn code consideration