简体   繁体   中英

ConstructUsingServiceLocator in AutoMapper

I found a method in AutoMapper: IMappingExpression.ConstructUsingServiceLocator() .

When should I use this method and what are the differences between it and ConstructUsing ?

It is used to configure AutoMapper to use certain constructor to instantiate a class.

Mapper.Initialize(cfg =>
    {
        // Adding "Construct" configuration 
        cfg.ConstructServicesUsing(t => new Dest(5));

        // Tell AutoMapper to use already defined configuration to construct Dest class
        cfg.CreateMap<Source, Dest>()
           .ConstructUsingServiceLocator();
    });

The best way to learn AutoMapper is to look into the unit tests .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM