Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Dependency injection 处理自动映射配置文件中的依赖关系_Dependency Injection_.net Core_Automapper - Fatal编程技术网

Dependency injection 处理自动映射配置文件中的依赖关系

Dependency injection 处理自动映射配置文件中的依赖关系,dependency-injection,.net-core,automapper,Dependency Injection,.net Core,Automapper,我有以下产品映射,它将在ProductMapService中调用依赖项UpdateProduct,以在初始映射后执行到产品子实体的映射。但是,当我执行映射过程时,我收到一个异常,没有为此对象定义无参数构造函数 我不确定这是在profile类中注入依赖项的最佳方法。有人能给我提供这方面的建议吗 Automapper产品简介: namespace MyAPI.Mappers { public class ProductProfiles: Profile { private readonly

我有以下产品映射,它将在ProductMapService中调用依赖项UpdateProduct,以在初始映射后执行到产品子实体的映射。但是,当我执行映射过程时,我收到一个异常,没有为此对象定义无参数构造函数

我不确定这是在profile类中注入依赖项的最佳方法。有人能给我提供这方面的建议吗

Automapper产品简介:

namespace MyAPI.Mappers
{
public class ProductProfiles: Profile
{
    private readonly IProductMapService _productMap;
    public ProductProfiles(IProductMapService productMap)
    {
        _productMap = productMap;

        CreateMap<ProductForCreationDto, Product>();

        CreateMap<ProductForUpdateDto, Product>()
            .ForMember(p => p.VariOptions, opt => opt.Ignore())
            .AfterMap((pDto, p) => _productMap.UpdateProduct(pDto, p));
    }
}
}
ProductService.cs:

namespace MyAPI.Services
{
public class ProductMapService: IProductMapService
{
    private readonly IMapper _mapper;
    public ProductMapService(IMapper mapper)
    {
        _mapper = mapper;

    }
    public void UpdateProduct(ProductForUpdateDto productForUpdateDto, Product productFromRepo)
    {

         foreach (var variOptionDto in productForUpdateDto.VariOptions)
        {
            if(variOptionDto.Id == 0)
            {
                productFromRepo.VariOptions.Add(Mapper.Map<VariOption>(variOptionDto));
            }
            else
            {
                _mapper.Map(variOptionDto, productFromRepo.VariOptions.SingleOrDefault(vo => vo.Id == variOptionDto.Id));
            }

            foreach (var variOptionTwoDto in variOptionDto.VariOptionTwos)
            {
                if(variOptionTwoDto.Id == 0)
                {
                    productFromRepo.VariOptions.FirstOrDefault(vo => vo.Id == variOptionDto.Id).VariOptionTwos.Add(Mapper.Map<VariOptionTwo>(variOptionTwoDto));
                }
                else
                {
                    _mapper.Map(variOptionTwoDto, productFromRepo.VariOptions.FirstOrDefault(vo => vo.Id == variOptionDto.Id).VariOptionTwos.SingleOrDefault(vot => vot.Id == variOptionTwoDto.Id));
                }
            }
        }
    }
}
}

在Startup.cs中:

services.AddAutoMapper();
services.AddSingleton<IProductMapService, ProductMapService>();

我按照卢西恩发布的文件制作了一个图像

namespace PortalestAPI.Mappers.MappingActions
{
public class ProductVariationTwoUpdateAction: IMappingAction<VariOptionForUpdateDto, VariOption>
{
    private readonly IProductMapService _productMapService;     

    public ProductVariationTwoUpdateAction(IProductMapService productMapService)
    {
        _productMapService = productMapService;
    }  

    public void Process(VariOptionForUpdateDto variOptionForUpdateDto, VariOption variOption)
    {
        _productMapService.UpdateVariationTwo(variOptionForUpdateDto, variOption);
    }
}
}

谢谢@LucianBargaoanu。这很有魅力