2013-07-10 38 views
2

嗨我正在使用Unity作爲我的IoC容器,並試圖使用ServiceLocator獲取對象的實例。統一IRegisterAutoMapper類型沒有可訪問的構造函數

這是我試圖執行代碼:

ServiceLocator.Current.GetInstance<IAutoMapperRegisterFactory>() 

我在配置文件中設置統一具有實例化對象:

container.RegisterType<IAutoMapperRegisterFactory, AutoMapperRegisterFactory>(); 

現在我CURENT 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; 
    } 
} 

和一切工作只是fine.What我做錯了,我該如何糾正呢?

回答

1

組件AutoMapperRegisterFactory要求作爲依賴IRegisterAutoMapper組件。 IRegisterAutoMapperRegisterAutoMapper類型實現,但RegisterAutoMapper類型的構造函數需要兩個參數:RegisterMappingsRegisterCustomMappings,因此您必須提供它們。

例如:

container.RegisterType<..., RegisterMappings>(); 
container.RegisterType<..., RegisterCustomMappings>(); 
相關問題