Jump to the main content

How to encapsulate the class library using Natasha

Natasha encapsulation protocol

Natasha has its own encapsulation rules, which allow the encapsulator to think more clearly and make the work easier to maintain.


A complete Operator

Operator, as a dynamically built operation class for external use, can consist of approximately the following three parts:

  • Template
  • Builder
  • Package / Extension

Script Builder

As the most important core part of Operator, Builder primarily provides delegates for Operator, can receive configurations from outside, can combine templates internally, and compiles them.
it is roughly divided into two parts, the Template template and the Compiler compiler:
it is roughly divided into two parts, the Template template and the Compiler compiler:
it is roughly divided into two parts, the Template template and the Compiler compiler:


  • Template Build Template Use template templates to build runtime script strings, and templates leak OUT of the API to make it easier for consumers to compose compilation strings.

    • UsingTemplat is Natasha's built-in template that provides code builds from namespaces to complete objects.

    • DelegateTemplate is Natasha's built-in template that provides the build of method code.

    • FieldTemplate is Natasha's built-in template that provides the build of field code.

    • PropertyTemplate is Natasha's built-in template that provides the build of property code.


  • Compiler compiler

    The compiler takes the strings provided by the template and compiles them to complete Builder's compilation task.

    • AssemblyCSharpBuilder: Using Natasha's CSharp compiler, you can easily compile strings and extract metadata.

 Customization can be quickly achieved using Natasha's built-in Builder, such as: OopBuilder<TOperator> , MethodBuilder<TOperator>.
The former provides it with an object construction template, while the latter focuses on the build method.

Operator

Operator is packaged on the basis of Builder, which stores the compilation results provided by Builder and leaks user-level APIs.


Case

For example, Natasha's built-in FastMethodOperator is packaged and simplified on the basis of MethodBuilder , and FastMethodOpearor customizes its own scripting process in its initialization function, as translated into public static

this. Access(AccessFlags.Public)
. Modifier(ModifierFlags.Static);