AutoMapperRegisterFactory

AutoMapperRegisterFactory

嗨,我使用Unity作为我的IoC容器,并尝试使用ServiceLocator获取对象的实例。

这是我要执行的代码:

ServiceLocator.Current.GetInstance<IAutoMapperRegisterFactory>()


我在配置文件中设置了必须实例化的对象:

container.RegisterType<IAutoMapperRegisterFactory, AutoMapperRegisterFactory>();


现在,我当前的AutoMapperRegisterFactory看起来像这样:

public class AutoMapperRegisterFactory : IAutoMapperRegisterFactory
{
    private readonly IRegisterAutoMapper m_RegisterAutoMapper;

    public AutoMapperRegisterFactory(IRegisterAutoMapper registerAutoMapper)
    {
        m_RegisterAutoMapper = registerAutoMapper;
    }

    public IEnumerable<IRegisterAutoMapper> GetRegisteredAutoMappers()
    {
        var registeredAutoMappers = new List<IRegisterAutoMapper> { m_RegisterAutoMapper };
        return registeredAutoMappers;
    }
}


如果我尝试运行此错误,则会出现此错误:

 The type IRegisterAutoMapper does not have an accessible constructor.


以前,我的AutomapperRegisterFactory如下所示:

public class AutoMapperRegisterFactory : IAutoMapperRegisterFactory
{
    public IEnumerable<IRegisterAutoMapper> GetRegisteredAutoMappers()
    {
        var registeredAutoMappers = new List<IRegisterAutoMapper> { new RegisterAutoMapper(new RegisterMappings(), new RegisterCustomMappings()) };
        return registeredAutoMappers;
    }
}


一切正常,我在做什么错,我该如何纠正?

最佳答案

组件AutoMapperRegisterFactory需要作为依赖项IRegisterAutoMapper组件。
IRegisterAutoMapperRegisterAutoMapper类型实现,但是RegisterAutoMapper类型的构造函数需要两个参数:RegisterMappingsRegisterCustomMappings,因此必须提供它们。

例如:

container.RegisterType<..., RegisterMappings>();
container.RegisterType<..., RegisterCustomMappings>();

09-28 02:39