Making.Analyzers 1.0.9-preview

This is a prerelease version of Making.Analyzers.
dotnet add package Making.Analyzers --version 1.0.9-preview
                    
NuGet\Install-Package Making.Analyzers -Version 1.0.9-preview
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Making.Analyzers" Version="1.0.9-preview" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Making.Analyzers" Version="1.0.9-preview" />
                    
Directory.Packages.props
<PackageReference Include="Making.Analyzers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Making.Analyzers --version 1.0.9-preview
                    
#r "nuget: Making.Analyzers, 1.0.9-preview"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Making.Analyzers@1.0.9-preview
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Making.Analyzers&version=1.0.9-preview&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=Making.Analyzers&version=1.0.9-preview&prerelease
                    
Install as a Cake Tool

Making.Analyzers

Dependency injection source generator for the Making framework.

Overview

Making.Analyzers provides Roslyn-based source generators and code analyzers for the Making framework. It automatically generates dependency injection registration code, validates service configurations, and provides compile-time analysis for better development experience.

Features

  • Dependency Injection Generator: Automatically generates service registration code
  • Service Validation: Compile-time validation of service configurations
  • Diagnostic Descriptors: Rich diagnostic messages and error reporting
  • Source Generation: Reduces boilerplate code with automatic code generation
  • Roslyn Integration: Leverages Microsoft.CodeAnalysis for powerful analysis

Installation

dotnet add package Making.Analyzers

Usage

Service Registration Generation

// Making your services with lifetime attributes
[Singleton]
public class EmailService : IEmailService
{
    public async Task SendEmailAsync(string to, string subject, string body)
    {
        // Implementation
    }
}

[Scoped]
public class UserService : IUserService
{
    private readonly IEmailService _emailService;
    
    public UserService(IEmailService emailService)
    {
        _emailService = emailService;
    }
    
    public async Task CreateUserAsync(CreateUserRequest request)
    {
        // Implementation
        await _emailService.SendEmailAsync(request.Email, "Welcome", "Welcome to our platform!");
    }
}

[Transient]
public class OrderProcessor : IOrderProcessor
{
    public async Task ProcessOrderAsync(Order order)
    {
        // Implementation
    }
}

Generated Registration Code

The analyzer automatically generates extension methods for service registration:

// Generated code (you don't need to write this)
public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddGeneratedServices(this IServiceCollection services)
    {
        services.AddSingleton<IEmailService, EmailService>();
        services.AddScoped<IUserService, UserService>();
        services.AddTransient<IOrderProcessor, OrderProcessor>();
        
        return services;
    }
}

Using Generated Registration

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        // Use the generated registration method
        services.AddGeneratedServices();
        
        // Add other services manually if needed
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(connectionString));
    }
}

Service Validation

The analyzer validates service configurations and provides warnings:

// This will generate a diagnostic warning
[Singleton]
public class BadService : IBadService
{
    // Warning: Singleton service depends on Scoped service
    public BadService(IScopedService scopedService) // ⚠️ Warning
    {
    }
}

// This will generate a diagnostic error
[Scoped]
public class InvalidService // ❌ Error: No interface implemented
{
    // Error: Service must implement an interface
}

Diagnostic Messages

The analyzer provides helpful diagnostic messages:

  • MARK001: Service must implement at least one interface
  • MARK002: Singleton service should not depend on Scoped services
  • MARK003: Singleton service should not depend on Transient services
  • MARK004: Service interface not found in current assembly
  • MARK005: Circular dependency detected

Advanced Usage

// Multiple interfaces
[Scoped]
public class MultiService : IService1, IService2, IService3
{
    // Will register for all implemented interfaces
}

// Generic services
[Scoped]
public class Repository<T> : IRepository<T> where T : class
{
    // Generic service registration
}

// Named services
[Scoped("PrimaryDatabase")]
public class PrimaryDbService : IDbService
{
    // Named service registration
}

[Scoped("SecondaryDatabase")]
public class SecondaryDbService : IDbService
{
    // Another named service registration
}

Configuration Options

Create a mark.analyzers.json file in your project root:

{
  "generateRegistrationMethods": true,
  "validateServiceLifetimes": true,
  "validateCircularDependencies": true,
  "generateNamesapce": "MyApp.Generated",
  "generateClassName": "MakingServiceRegistration",
  "excludeAssemblies": ["System.*", "Microsoft.*"],
  "includeInternalServices": false
}

MSBuild Integration

The analyzer integrates seamlessly with MSBuild:

<Project Sdk="Microsoft.NET.Sdk">
  
  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
  </PropertyGroup>
  
  <ItemGroup>
    <PackageReference Include="Making.Analyzers" Version="1.0.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
    </PackageReference>
  </ItemGroup>
  
</Project>

Custom Attributes

Define custom service lifetime attributes:

[AttributeUsage(AttributeTargets.Class)]
public class RepositoryAttribute : Attribute
{
    public ServiceLifetime Lifetime { get; }
    
    public RepositoryAttribute(ServiceLifetime lifetime = ServiceLifetime.Scoped)
    {
        Lifetime = lifetime;
    }
}

// Usage
[Repository(ServiceLifetime.Singleton)]
public class CacheRepository : IRepository
{
    // Implementation
}

Requirements

  • .NET Standard 2.0+
  • Microsoft.CodeAnalysis.CSharp (for compilation)
  • C# 9.0+ (for source generators)

License

This project is part of the Making framework.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.9-preview 145 8/20/2025
1.0.8-preview 126 8/20/2025
1.0.7-preview 125 8/20/2025
1.0.6-preview 128 8/19/2025