Implementation

Intellenum.CodeFixers

This project contains Roslyn analyzers and code fixers to help developers create more robust, type-safe enums.

The goal is to enforce specific coding conventions related to enums, aiming for a cleaner and more predictable codebase.

Code Analysis Rules

Here’s an overview of the code analysis rules implemented in Intellenum.CodeFixers.

DoNotUseNewAnalyzer

Purpose: To prevent direct instantiation of enum types using the new keyword.

Justification: Enums are value types and don’t require instantiation. The new keyword can lead to confusion and unnecessary memory allocation.

Example:

// Incorrect usage:
          CustomerType customerType = new CustomerType();
          
          // Correct usage:
          CustomerType customerType = CustomerType.Standard;
          

Source: tests/AnalyzerTests/DoNotUseNewAnalyzerTests.cs

Intellenum.CodeFixers.IntellenumAnalyzer

Purpose: Enforces Intellenum-specific conventions and ensures code adheres to its design principles.

Example:

// Example of a code analysis rule:
          [DiagnosticAnalyzer(LanguageNames.CSharp)]
          [DiagnosticCategory("Intellenum")]
          public class IntellenumAnalyzer : DiagnosticAnalyzer
          {
              // ... implementation ...
          }
          

Source: src/Intellenum.CodeFixers/IntellenumAnalyzer.cs

Intellenum.CodeFixers.EnumGenerator

Purpose: The EnumGenerator is a Roslyn code generator that helps in the creation of specialized enums with improved type safety.

Example:

// Code generation example:
          public static void GenerateEnum(ISymbol symbol, Compilation compilation, CodeWriter codeWriter)
          {
              // ... implementation ...
          }
          

Source: src/Intellenum.CodeFixers/EnumGenerator.cs

Intellenum.CodeFixers.EnumExtensionsGenerator

Purpose: Generate extension methods for enums that simplify common operations, enhancing usability.

Example:

// Example of a generated extension method:
          public static bool TryParse(this CustomerType value, string? name, out CustomerType result)
          {
              // ... implementation ...
          }
          

Source: src/Intellenum.CodeFixers/EnumExtensionsGenerator.cs

Intellenum.CodeFixers.EnumExtensions

Purpose: This class provides extension methods that simplify working with Intellenum enums.

Example:

// Example of an extension method for enum operations:
          public static bool IsStandard(this CustomerType value)
          {
              // ... implementation ...
          }
          

Source: src/Intellenum/Extensions/EnumExtensions.cs

Intellenum

This project contains core logic and essential functionality for the Intellenum library.

Intellenum.Extensions.IMethodSymbolExtensions

Purpose: This class offers extension methods specifically for analyzing and manipulating IMethodSymbol objects, which represent methods within a codebase.

Example:

/// <summary>
          /// Checks if the given method overrides <see cref="object.Equals(object)"/>.
          /// </summary>
          public static bool IsObjectEqualsOverride(this IMethodSymbol method)
          {
              return method != null &&
                     method.IsOverride &&
                     method.Name == WellKnownMemberNames.ObjectEquals &&
                     method.ReturnType.SpecialType == SpecialType.System_Boolean &&
                     method.Parameters.Length == 1 &&
                     method.Parameters[0].Type.SpecialType == SpecialType.System_Object &&
                     IsObjectMethodOverride(method);
          }
          
          /// <summary>
          /// Checks if the given method is <see cref="object.Equals(object)"/>.
          /// </summary>
          public static bool IsObjectEquals(this IMethodSymbol method)
          {
              return method != null &&
                     method.ContainingType.SpecialType == SpecialType.System_Object &&
                     method.IsVirtual &&
                     method.Name == WellKnownMemberNames.ObjectEquals &&
                     method.ReturnType.SpecialType == SpecialType.System_Boolean &&
                     method.Parameters.Length == 1 &&
                     method.Parameters[0].Type.SpecialType == SpecialType.System_Object;
          }
          

Source: src/Intellenum/Extensions/IMethodSymbolExtensions.cs

Additional Information

Intellenum and Other Enum Implementations

Intellenum aims to improve upon traditional enums and other enum-like libraries. While the goal is similar (providing type-safe enums), Intellenum focuses on specific aspects that differentiate it.

  • StandardEnums: Basic enums, using the standard Enum type.
  • EnumGenerators: A library using code generation to create enums with additional methods.
  • SmartEnums: A library providing a type-safe enum implementation with some features.
  • Intellenums Utilizes Roslyn analyzers and code generation to enforce specific conventions and improve type safety.

Comparison:

Implementation Performance Comments
StandardEnums 123.937 ns Uses traditional Enum type.
EnumGenerators 9.067 ns Uses code generation to create enums.
SmartEnums 30.719 ns Provides type-safe enums with some features.
Intellenums 11.460 ns Uses Roslyn analyzers and code generation to enforce conventions and improve type safety.

Source: docs/site/Writerside/topics/discussions/Overview.md

Integration

Intellenum offers seamless integration with various frameworks and libraries, simplifying common tasks and ensuring consistent data handling.

Integration Options:

Integration Description
NewtonsoftJson Creates a Newtonsoft.Json.JsonConverter for serializing the value object to its primitive value.
SystemTextJson Creates a System.Text.Json.Serialization.JsonConverter for serializing the value object to its primitive value.
EfCoreValueConverter Creates an EF Core Value Converter for extracting the primitive value.
DapperTypeHandler Creates a Dapper TypeHandler for converting to and from the type.
LinqToDbValueConverter Creates a LinqToDb ValueConverter for converting to and from the type.

Source: docs/site/Writerside/topics/reference/Integration.md


          ## Top-Level Directory Explanations
          
          <a class='local-link directory-link' data-ref="samples/" href="#samples/">samples/</a> - This directory contains example projects demonstrating the usage of Intellenum.
          
          <a class='local-link directory-link' data-ref="samples/Intellenum.Examples/" href="#samples/Intellenum.Examples/">samples/Intellenum.Examples/</a> - Contains various example projects demonstrating different aspects of Intellenum, such as serialization, conversion, syntax examples, types, typical scenarios, and more.
          
          <a class='local-link directory-link' data-ref="samples/WebApplication/" href="#samples/WebApplication/">samples/WebApplication/</a> - Contains a sample web application that uses Intellenum.
          
          <a class='local-link directory-link' data-ref="src/" href="#src/">src/</a> - This directory contains the source code of the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Benchmarks/" href="#src/Benchmarks/">src/Benchmarks/</a> - Contains benchmark tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum.CodeFixers/" href="#src/Intellenum.CodeFixers/">src/Intellenum.CodeFixers/</a> - Contains code fixers for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum.SharedTypes/" href="#src/Intellenum.SharedTypes/">src/Intellenum.SharedTypes/</a> - Contains shared types used across the Intellenum library.
          
          <a class='local-link directory-link' data-ref="src/Intellenum/" href="#src/Intellenum/">src/Intellenum/</a> - Contains the main source code for the Intellenum library. This directory is further divided into subdirectories for diagnostics, extensions, generators, member building, properties, rules, static constructor building, templates, and more.
          
          <a class='local-link directory-link' data-ref="tests/" href="#tests/">tests/</a> - This directory contains test projects for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/AnalyzerTests/" href="#tests/AnalyzerTests/">tests/AnalyzerTests/</a> - Contains unit tests for the Intellenum analyzer.
          
          <a class='local-link directory-link' data-ref="tests/ConsumerTests/" href="#tests/ConsumerTests/">tests/ConsumerTests/</a> - Contains tests for consuming the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Intellenum.Tests/" href="#tests/Intellenum.Tests/">tests/Intellenum.Tests/</a> - Contains additional tests for the Intellenum library.
          
          <a class='local-link directory-link' data-ref="tests/Shared/" href="#tests/Shared/">tests/Shared/</a> - Contains shared test files.
          
          <a class='local-link directory-link' data-ref="tests/SnapshotTests/" href="#tests/SnapshotTests/">tests/SnapshotTests/</a> - Contains snapshot tests for the Intellenum library.