Files
PowderCoatingLogix/MAPPER_CONFIGURATION_FIX.md
2026-04-23 21:38:24 -04:00

4.9 KiB

MapperConfiguration Constructor Error - FIXED

🐛 Error Found

'MapperConfiguration' does not contain a constructor that takes 1 arguments

🔍 Root Cause

AutoMapper 16.0.0 has a different API for the MapperConfiguration constructor compared to earlier versions.

The lambda-based constructor with Action<IMapperConfigurationExpression> is the correct signature.

Fix Applied

Updated both Program.cs files (Web and API).

Before (Incorrect for AutoMapper 16.0):

var mapperConfig = new MapperConfiguration(mc =>
{
    mc.AddProfile<CustomerProfile>();    // ❌ Generic method
    mc.AddProfile<JobProfile>();
});

After (Correct for AutoMapper 16.0):

var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());  // ✅ Instance method
    cfg.AddProfile(new JobProfile());
});

📝 What Changed in AutoMapper 16.0

Key Differences:

  1. Profile Registration Method

    • Old: cfg.AddProfile<TProfile>()
    • New: cfg.AddProfile(new TProfile())
  2. Constructor Parameter

    • Still accepts Action<IMapperConfigurationExpression>
    • But the expression methods have changed

Why This Change?

AutoMapper 16.0 simplified profile registration to always use instances rather than generic type parameters. This provides:

  • More consistent API
  • Better support for profiles with constructor parameters
  • Clearer initialization semantics

🔧 Updated Configuration

Web Project (src/PowderCoating.Web/Program.cs)

// Configure AutoMapper
var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());
    cfg.AddProfile(new JobProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
builder.Services.AddSingleton<IMapper>(mapper);

API Project (src/PowderCoating.Api/Program.cs)

// Configure AutoMapper
var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());
    cfg.AddProfile(new JobProfile());
});
IMapper mapper = mapperConfig.CreateMapper();
builder.Services.AddSingleton(mapper);
builder.Services.AddSingleton<IMapper>(mapper);

Verification

The configuration now correctly:

  1. Creates instances of each profile
  2. Adds them to the configuration
  3. Creates the mapper
  4. Registers both the mapper and IMapper interface

Testing:

// In any controller
public class CustomersController : Controller
{
    private readonly IMapper _mapper;

    public CustomersController(IMapper mapper)
    {
        _mapper = mapper;  // ✅ Will inject successfully
    }

    public IActionResult Index()
    {
        var customer = new Customer { /* ... */ };
        var dto = _mapper.Map<CustomerDto>(customer);  // ✅ Will work
        return View(dto);
    }
}

📚 AutoMapper 16.0 Profile Creation

Your profile classes remain unchanged:

public class CustomerProfile : Profile
{
    public CustomerProfile()  // ✅ Parameterless constructor
    {
        CreateMap<Customer, CustomerDto>();
        CreateMap<CreateCustomerDto, Customer>();
        // etc...
    }
}

The profiles themselves don't change - only how they're registered.

🎯 Alternative Approaches (For Reference)

Option 1: Current Approach (Used)

var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());
    cfg.AddProfile(new JobProfile());
});

Explicit
Clear what's registered
Easy to debug

Option 2: Assembly Scanning (NOT used)

var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddMaps(typeof(CustomerProfile).Assembly);
});

Less explicit
Harder to debug
May register unwanted profiles

We're using Option 1 for clarity and control.

🚀 Build Status

After this fix, the build should succeed:

dotnet clean
dotnet restore
dotnet build

# Expected Output:
# Build succeeded.
#     0 Warning(s)
#     0 Error(s)

📋 Files Modified

  1. src/PowderCoating.Web/Program.cs - Lines 51-59
  2. src/PowderCoating.Api/Program.cs - Lines 75-83

🔄 Migration Guide (For Reference)

If you add more profiles in the future:

// Create the profile class
public class InventoryProfile : Profile
{
    public InventoryProfile()
    {
        CreateMap<InventoryItem, InventoryItemDto>();
        // ... more mappings
    }
}

// Register it in Program.cs
var mapperConfig = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new CustomerProfile());
    cfg.AddProfile(new JobProfile());
    cfg.AddProfile(new InventoryProfile());  // ← Add new profile
});

💡 Key Takeaway

AutoMapper 16.0 Syntax:

cfg.AddProfile(new ProfileClassName());  // ✅ Correct

NOT:

cfg.AddProfile<ProfileClassName>();      // ❌ Old syntax

MapperConfiguration constructor error is now fixed! The project should build successfully with AutoMapper 16.0.0.