Skip to content

Class DbCommandSourceGenSettings

Namespace: Momentum.Extensions.SourceGenerators.DbCommand
Assembly: Momentum.Extensions.SourceGenerators.dll

Contains global configuration settings for the DbCommand source generator, extracted from MSBuild properties.

csharp
public record DbCommandSourceGenSettings : IEquatable<DbCommandSourceGenSettings>

Inheritance

objectDbCommandSourceGenSettings

Implements

IEquatable<DbCommandSourceGenSettings>

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Examples

Example Configuration Effects:

// MSBuild configuration:
// <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
// <DbCommandParamPrefix>sp_</DbCommandParamPrefix>

[DbCommand(sp: "create_user")]
public record CreateUserCommand(
    string FirstName,           // → @sp_first_name
    string LastName,            // → @sp_last_name
    [Column("email_addr")] string Email  // → @email_addr (Column overrides prefix)
) : ICommand<int>;

// Generated ToDbParams():
public object ToDbParams()
{
    var p = new 
    {
        sp_first_name = this.FirstName,
        sp_last_name = this.LastName,
        email_addr = this.Email
    };
    return p;
}

Environment-Specific Configuration:

<!-- Development environment -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <DbCommandDefaultParamCase>None</DbCommandDefaultParamCase>
  <DbCommandParamPrefix></DbCommandParamPrefix>
</PropertyGroup>

<!-- Production environment -->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
  <DbCommandParamPrefix>prod_</DbCommandParamPrefix>
</PropertyGroup>

Remarks

DbCommand Source Generator Settings Detailed Guide

MSBuild Integration

These settings are configured through MSBuild properties in the project file:

xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
    <DbCommandParamPrefix>p_</DbCommandParamPrefix>
  </PropertyGroup>
</Project>

Parameter Name Generation Order

  1. Base Name: Start with C# property name (e.g., "FirstName")
  2. Column Override: Apply [Column("custom")] if present → "custom"
  3. Case Conversion: Apply DbCommandDefaultParamCase if no Column attribute → "first_name"
  4. Global Prefix: Apply DbCommandParamPrefix → "p_first_name"

Configuration Precedence

  • Highest: [Column("name")] attribute on individual properties
  • Medium: DbCommandAttribute.ParamsCase on individual commands
  • Lowest: Global MSBuild settings (DbCommandDefaultParamCase, DbCommandParamPrefix)

Parameter Case Options

  • None: Use property names as-is (e.g., "FirstName" → "@FirstName")
  • SnakeCase: Convert to snake_case (e.g., "FirstName" → "@first_name")

Global Prefix Usage

The DbCommandParamPrefix is useful for:

  • Stored Procedure Conventions: Some teams prefix all SP parameters (e.g., "p_user_id")
  • Avoiding Keywords: Prefix to avoid SQL reserved words (e.g., "p_order" instead of "order")
  • Multi-Tenant Systems: Different prefixes for different tenant databases
  • Legacy System Integration: Match existing database parameter naming conventions

Build-Time Resolution

These settings are resolved during compilation from the MSBuild context, allowing different configurations for different build environments (Development, Staging, Production).

Constructors

DbCommandSourceGenSettings(DbParamsCase, string)

Contains global configuration settings for the DbCommand source generator, extracted from MSBuild properties.

csharp
public DbCommandSourceGenSettings(DbParamsCase DbCommandDefaultParamCase, string DbCommandParamPrefix)

Parameters

DbCommandDefaultParamCase DbParamsCase

The default parameter case conversion to apply when DbCommandAttribute.ParamsCase is set to Unset. This provides a project-wide default for how property names are converted to database parameter names.

DbCommandParamPrefix string

A global prefix to add to all generated parameter names. This is applied after case conversion and Column attribute overrides. Useful for database systems that require parameter prefixes.

Examples

Example Configuration Effects:

// MSBuild configuration:
// <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
// <DbCommandParamPrefix>sp_</DbCommandParamPrefix>

[DbCommand(sp: "create_user")]
public record CreateUserCommand(
    string FirstName,           // → @sp_first_name
    string LastName,            // → @sp_last_name
    [Column("email_addr")] string Email  // → @email_addr (Column overrides prefix)
) : ICommand<int>;

// Generated ToDbParams():
public object ToDbParams()
{
    var p = new 
    {
        sp_first_name = this.FirstName,
        sp_last_name = this.LastName,
        email_addr = this.Email
    };
    return p;
}

Environment-Specific Configuration:

<!-- Development environment -->
<PropertyGroup Condition="'$(Configuration)' == 'Debug'">
  <DbCommandDefaultParamCase>None</DbCommandDefaultParamCase>
  <DbCommandParamPrefix></DbCommandParamPrefix>
</PropertyGroup>

<!-- Production environment -->
<PropertyGroup Condition="'$(Configuration)' == 'Release'">
  <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
  <DbCommandParamPrefix>prod_</DbCommandParamPrefix>
</PropertyGroup>

Remarks

DbCommand Source Generator Settings Detailed Guide

MSBuild Integration

These settings are configured through MSBuild properties in the project file:

xml
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <DbCommandDefaultParamCase>SnakeCase</DbCommandDefaultParamCase>
    <DbCommandParamPrefix>p_</DbCommandParamPrefix>
  </PropertyGroup>
</Project>

Parameter Name Generation Order

  1. Base Name: Start with C# property name (e.g., "FirstName")
  2. Column Override: Apply [Column("custom")] if present → "custom"
  3. Case Conversion: Apply DbCommandDefaultParamCase if no Column attribute → "first_name"
  4. Global Prefix: Apply DbCommandParamPrefix → "p_first_name"

Configuration Precedence

  • Highest: [Column("name")] attribute on individual properties
  • Medium: DbCommandAttribute.ParamsCase on individual commands
  • Lowest: Global MSBuild settings (DbCommandDefaultParamCase, DbCommandParamPrefix)

Parameter Case Options

  • None: Use property names as-is (e.g., "FirstName" → "@FirstName")
  • SnakeCase: Convert to snake_case (e.g., "FirstName" → "@first_name")

Global Prefix Usage

The DbCommandParamPrefix is useful for:

  • Stored Procedure Conventions: Some teams prefix all SP parameters (e.g., "p_user_id")
  • Avoiding Keywords: Prefix to avoid SQL reserved words (e.g., "p_order" instead of "order")
  • Multi-Tenant Systems: Different prefixes for different tenant databases
  • Legacy System Integration: Match existing database parameter naming conventions

Build-Time Resolution

These settings are resolved during compilation from the MSBuild context, allowing different configurations for different build environments (Development, Staging, Production).

DbCommandSourceGenSettings(DbCommandSourceGenSettings)

csharp
protected DbCommandSourceGenSettings(DbCommandSourceGenSettings original)

Parameters

original DbCommandSourceGenSettings

Properties

DbCommandDefaultParamCase

The default parameter case conversion to apply when DbCommandAttribute.ParamsCase is set to Unset. This provides a project-wide default for how property names are converted to database parameter names.

csharp
public DbParamsCase DbCommandDefaultParamCase { get; init; }

Property Value

DbParamsCase

DbCommandParamPrefix

A global prefix to add to all generated parameter names. This is applied after case conversion and Column attribute overrides. Useful for database systems that require parameter prefixes.

csharp
public string DbCommandParamPrefix { get; init; }

Property Value

string

EqualityContract

csharp
protected virtual Type EqualityContract { get; }

Property Value

Type

Methods

<Clone>$()

csharp
public virtual DbCommandSourceGenSettings <Clone>$()

Returns

DbCommandSourceGenSettings

Deconstruct(out DbParamsCase, out string)

csharp
public void Deconstruct(out DbParamsCase DbCommandDefaultParamCase, out string DbCommandParamPrefix)

Parameters

DbCommandDefaultParamCase DbParamsCase

DbCommandParamPrefix string

Equals(object?)

csharp
public override bool Equals(object? obj)

Parameters

obj object?

Returns

bool

Equals(DbCommandSourceGenSettings?)

csharp
public virtual bool Equals(DbCommandSourceGenSettings? other)

Parameters

other DbCommandSourceGenSettings?

Returns

bool

GetHashCode()

csharp
public override int GetHashCode()

Returns

int

PrintMembers(StringBuilder)

csharp
protected virtual bool PrintMembers(StringBuilder builder)

Parameters

builder StringBuilder

Returns

bool

ToString()

csharp
public override string ToString()

Returns

string

Operators

operator ==(DbCommandSourceGenSettings?, DbCommandSourceGenSettings?)

csharp
public static bool operator ==(DbCommandSourceGenSettings? left, DbCommandSourceGenSettings? right)

Parameters

left DbCommandSourceGenSettings?

right DbCommandSourceGenSettings?

Returns

bool

operator !=(DbCommandSourceGenSettings?, DbCommandSourceGenSettings?)

csharp
public static bool operator !=(DbCommandSourceGenSettings? left, DbCommandSourceGenSettings? right)

Parameters

left DbCommandSourceGenSettings?

right DbCommandSourceGenSettings?

Returns

bool