Unity和Prism的自动注册无法正常工作

时间:2015-05-11 11:38:46

标签: unity-container prism-5

我正在使用Unity和Prism进行此配置:

public class EmployeeDataService : IEmployeeDataService
{
}
public class EmployeeController(IEmployeeDataService dataService)
{
    this.dataService = dataService;
}

public class EmployeeModuleInit : IModule
{
   readonly IUnityContainer container;
   readonly IRegionManager  regionManager;
   readonly EmployeeController employeeController;

   public EmployeeModuleInit(IUnityContainer container, IRegionManager regionManager, EmployeeController employeeController)
   {
       this.container     = container;
       this.regionManager = regionManager;
       this.employeeController = employeeController;
   }
}

使用以下方式映射类型:

Container.RegisterTypes(AllClasses.FromLoadedAssemblies(),
    WithMappings.FromMatchingInterface,
    WithName.TypeName,
    WithLifetime.Transient );

然而,在初创时我遇到了这个错误:

{“当前类型IEmployeeDataService是一个接口,无法构造。您是否缺少类型映射?”}

IUnityContainer和IRegionManager已正确注册和解析,但是它们是在Prism.UnityExtensions.UnityBootstrapper.cs中手动注册的,我不想手动注册项目中的所有类型。

任何想法如何解决这个问题?

谢谢你, 丹尼尔

1 个答案:

答案 0 :(得分:0)

按照惯例注册是使用名称注册所有类型。我想你可能想要这个......

Container.RegisterTypes(AllClasses.FromLoadedAssemblies(),
    WithMappings.FromMatchingInterface,
    WithName.Default,
    WithLifetime.Transient);