MvxException:无法解析类型... IRepository

时间:2013-12-10 17:50:43

标签: c# inversion-of-control xamarin mvvmcross

在我们的MvxApplication中,我使用EndingWith方法注册我们的存储库。

我在一个不同的项目中使用了这种方法,我们的App.cs和我们的存储库位于同一个项目中,一切正常。现在我已将App.cs移动到一个单独的项目中,MvvmCross IOC不会从我们的核心应用程序注册依赖项。

初始化方法

public override void Initialize() {

    // stuff happens here

    // Register all repositories
    CreatableTypes().EndingWith("Repository").AsInterfaces().RegisterAsLazySingleton();

    // more stuff happens here

    // Resolve the DbVersionRepo to run migrations.
    // This line is breaking
    var databaseVersionRepository = MvxSimpleIoCContainer.Instance.Resolve < IDatabaseVersionRepository > ();

}

有问题的资料库

public class DatabaseVersionRepository : RepositoryBase, IDatabaseVersionRepository
{ 
    // do stuff
}

例外

Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type OurApp.AppCore.Repositories.IDatabaseVersionRepository
12-10 10:35:43.275 E/mono    (24043): 
12-10 10:35:43.275 E/mono    (24043): Unhandled Exception:
12-10 10:35:43.275 E/mono    (24043): Cirrious.CrossCore.Exceptions.MvxException: Failed to resolve type OurApp.AppCore.Repositories.IDatabaseVersionRepository
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Resolve (System.Type t) [0x00000] in <filename unknown>:0 
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.CrossCore.IoC.MvxSimpleIoCContainer.Resolve[IDatabaseVersionRepository] () [0x00000] in <filename unknown>:0 
12-10 10:35:43.275 E/mono    (24043):   at OurApp.CustomApp.Core.App.Initialize () [0x00135] in c:\Projects\OurApp\OurApp.Droid\App.cs:63 
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.MvvmCross.Platform.MvxSetup.CreateAndInitializeApp (IMvxPluginManager pluginManager) [0x00000] in <filename unknown>:0 
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.MvvmCross.Platform.MvxSetup.InitializeApp (IMvxPluginManager pluginManager) [0x00000] in <filename unknown>:0 
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.MvvmCross.Platform.MvxSetup.InitializeSecondary () [0x00000] in <filename unknown>:0 
12-10 10:35:43.275 E/mono    (24043):   at Cirrious.MvvmCross.Droid.Platform.MvxAndroidSetupSingleton.<InitializeFromSplashScreen>b__5 (System.Object ignored) [0x00000] in <filename unknown>:0 
The program 'Mono' has exited with code 0 (0x0).

项目结构是这样的。

OurCompany.AppCore
    \ Repositories
        \IDatabaseVersionRepository.cs
        \DatabaseVersionRepository.cs

OurCompany.CustomApp.Core
    \ App.cs

OurCompany.CustomApp.Droid.Ui
    \ Setup.cs

我还应该注意到我尝试了这种方法但得到了相同的结果。

CreatableTypes().Inherits<IDatabaseVersionRepository>().AsInterfaces().RegisterAsLazySingleton();

1 个答案:

答案 0 :(得分:2)

根据Stuart上面的评论,解决当前程序集之外的依赖关系的方法是执行以下操作。

typeof(RepositoryBase).Assembly
                      .CreatableTypes()
                      .EndingWith("Repository")
                      .AsInterfaces()
                      .RegisterAsLazySingleton();