Metalama, a high-level meta-programming framework based on Roslyn and AOP #68111
Replies: 4 comments
-
I've been using Metalama (beta) for few months in my personal library projects and I love it. I'm happy that it's finally released. 🥳 |
Beta Was this translation helpful? Give feedback.
-
I just started using Metalama this week and have been floored by how quickly I can produce large swathes of consistent code throughout my solutions. Its biggest draw to me is that the aspects you write can have both runtime and compile-time code leaving you with runtime configurability with compile-time flexibility (e.g. automatically introduce dependency injected fields that utilize your runtime DI registration). Congratulations on your 1.0 release! |
Beta Was this translation helpful? Give feedback.
-
this is some aop lanauge that I imagine , it's really cool to see it became reallity ! |
Beta Was this translation helpful? Give feedback.
-
I would love, if you join Metalama communities: |
Beta Was this translation helpful? Give feedback.
-
We are thrilled to announce the launch of Metalama, our new Roslyn-based meta-programming framework for C#. Since its inception in August 2020, Metalama has undergone continuous development, and we are delighted to make it generally available now.
Metalama is designed for line-of-business application development teams, helping them write clean code by minimizing boilerplate and ensuring compliance with custom architecture rules. Drawing from our prior experience and telemetry data gathered from PostSharp, we estimate that this approach allows development teams to achieve savings of 10-20% in lines of code, effort, and bug occurrences.
Metalama vs. Roslyn
Metalama extends Roslyn by offering two significant features. First, it supports aspect-oriented programming, allowing developers to safely and simply enhance their code during the design and compilation stages. Second, Metalama is much more straightforward than the Roslyn API itself, enabling meta-programming code to reside within the same project as the business code. We have broadened the notion of an aspect for modern C#: an aspect can not only provide advice (i.e., code transformations) but also report diagnostics and suggest code fixes for these diagnostics, all within a single class.
Example 1: Simple Logging
The classic Hello world example of aspect-oriented programming is logging. Consider the following aspect:
Observe the use of the
meta
pseudo-keyword in this code, which provides access to the meta-model.Now, let's apply this aspect to the following code:
The resulting code, which will be executed, appears as follows:
Although this example is relatively straightforward, you can follow this tutorial to explore implementing a more practical logging aspect.
Example 2: IChangeTracking
Let's examine a more complex example featuring a different aspect: the automatic implementation of the
IChangeTracking
interface. This aspect also reports a diagnostic if the type already implements theIChangeTracking
interface but does not expose anyOnChange
method, which the design pattern conventionally requires. This aspect showcases several remarkable features. TheBuildAspect
method serves as the method's entry point, providing advice (implementing the interface and overriding property setters) and reporting errors. It also possesses other capabilities beyond this brief introduction's scope.Now, let's apply the aspect to the following code:
The resulting code appears as follows:
For details about this example, see our documentation.
Example 3: architecture verification
The following example demonstrates a fabric class. Similar to aspect classes, fabrics execute at compile time. However, unlike aspects, fabrics do not need to be applied to anything. Their
AmendProject
method is executed automatically. In this example, we register a rule that verifies that theInvoicing
namespace cannot usefloat
ordouble
. This verification is performed at design time. Notice how much simpler it is for application developers than authoring an analyzer for the same purpose. Additionally, a fabric is a class of the same project as the one containing the business code.For more information about fabrics and validation, refer to this documentation article.
How it works
Metalama integrates with various Roslyn extension points, including analyzers, suppressors, code generators, code fix providers, and refactoring providers. However, these were insufficient to implement aspect-oriented programming, necessitating the development and maintenance of a shallow fork of the Roslyn repository to include a new extension point: transformers. Transformers enable arbitrary syntax tree transformations during compile time. We do not encourage users to utilize this API, as our target audience consists of line-of-business developers who likely lack the skills and time necessary to develop compiler plugins.
Summary
Metalama aims to be a modern, comprehensive, integrated, user-friendly, and sound meta-programming framework for C# line-of-business application developers. It empowers them to improve code quality and productivity by eliminating the need to write boilerplate code and enforcing architectural rules in real-time.
Metalama comes with a free edition and several affordable commercial options. Open-source projects can use and distribute Metalama free of change.
To learn more about Metalama, visit our website and explore our documentation.
Beta Was this translation helpful? Give feedback.
All reactions