AutoMapper auto create createMap

2019-04-04 01:30发布

问题:

I have a services that is calling another services. Both of the services are using "the same classes". The classes are named same and have the same properties but has different namespace so I need to use AutoMapper to map from one of the type to the other type.

No it's pretty simple since all I have to do is the CreateMap<>, but the problem is that we have around hundreds of classes that I manually needs to write the CreateMap<> from, and it's works wired to me. Isn't there any Auto CreateMap function. So if I say CreateMap() then AutoMapper workes thru Organisation and finds all classes and automatically does the CreateMap for these Classes and it's subclasses etc etc…

Hope for a simple solution, or I guess some reflection can fix it...

回答1:

Just set CreateMissingTypeMaps to true in the options:

var dto = Mapper.Map<FooDTO>
     (foo, opts => opts.CreateMissingTypeMaps = true);

If you need to use it often, store the lambda in a delegate field:

static readonly Action<IMappingOperationOptions> _mapperOptions =
    opts => opts.CreateMissingTypeMaps = true;

...

var dto = Mapper.Map<FooDTO>(foo, _mapperOptions);

UPDATE:

The approach described above no longer works in recent versions of AutoMapper.

Instead, you should create a mapper configuration with CreateMissingTypeMaps set to true and create a mapper instance from this configuration:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMissingTypeMaps = true;
    // other configurations
});
var mapper = config.CreateMapper();

If you want to keep using the old static API (no longer recommended), you can also do this:

Mapper.Initialize(cfg =>
{
    cfg.CreateMissingTypeMaps = true;
    // other configurations
});


回答2:

AutoMapper has a DynamicMap method which you might be able to use: here's an example unit test illustrating it.

[TestClass]
public class AutoMapper_Example
{
    [TestMethod]
    public void AutoMapper_DynamicMap()
    {
        Source source = new Source {Id = 1, Name = "Mr FooBar"};

        Target target = Mapper.DynamicMap<Target>(source);

        Assert.AreEqual(1, target.Id);
        Assert.AreEqual("Mr FooBar", target.Name);
    }

    private class Target
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

    private class Source
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}


回答3:

CreateMissingTypeMaps can be set within your profile. It's however recommended to explicitly use CreateMap for each mapping and call AssertConfigurationIsValid in your unit tests for each profile to prevent silent errors.

public class MyProfile : Profile {
    CreateMissingTypeMaps = true;

    // Mappings...
}


回答4:

Set CreateMissingTypeMaps option to true. This is package AutoMapper.Extensions.Microsoft.DependencyInjection's example for ASP.NET Core:

public class Startup {
    //...
    public void ConfigureServices(IServiceCollection services) {
        //...
        services.AddAutoMapper(cfg => { cfg.CreateMissingTypeMaps = true; });
        //...
    }
    //...
}


标签: c# AutoMapper