PRISM + Splat 作为 IOC
PRISM + Splat as IOC
我以前使用过 DryIOC 作为我的 IOC,但我想尝试将 Splat 用于我的 IOC,是否可以将这两者结合起来?
我试过让 LoginModule
继承 IModule
所以我的 LoginModule
class:
public void OnInitialized(IContainerProvider containerProvider)
{
Locator.CurrentMutable.RegisterLazySingleton(() => new ServiceEntityMapper(), typeof(IServiceEntityMapper));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginAPIService(), typeof(ILoginAPIService));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginManager(
Locator.Current.GetService<IServiceEntityMapper>(),
Locator.Current.GetService<ILoginAPIService>()), typeof(ILoginManager));
}
我的视图模型构造函数有这个:
public LoginViewModel(INavigationService navigationService, ILoginManager loginManager = null) : base(navigationService)
{
LoginManager = loginManager ?? Locator.Current.GetService<ILoginManager>();
}
结果,每当我导航到该页面时,我都会收到此异常
{System.TypeLoadException: Could not resolve the signature of a virtual method
at System.Lazy`1[T].CreateValue () [0x00081] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].LazyInitValue () [0x00080] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].get_Value () [0x0003a] in <fe08c003e91342eb83df1ca48302ddbb>:0
at Splat.DependencyResolverMixins+<>c__DisplayClass7_0.<RegisterLazySingleton>b__0 () [0x00000] in <89c762f12a12451a8970372dc9921547>:0
at Splat.ModernDependencyResolver.GetService (System.Type serviceType, System.String contract) [0x00032] in <89c762f12a12451a8970372dc9921547>:0
at Splat.DependencyResolverMixins.GetService[T] (Splat.IDependencyResolver resolver, System.String contract)
据我所知,Splat 是一个服务定位器,而不是真正的 DI 容器。也就是说,您当然不限于基本的 Prism 实现,因为提供这些实现是为了使其易于采用和入门。在您的情况下,我可能建议您创建自己的 IContainerExtension 实现并继承自 PrismApplicationBase。
通过查看 Unity or DryIoc... there is a similar example using the Grace DI Container 的实现,您可以看到在您的应用程序 class 中确实没有太多额外的工作。请记住,自上次预览以来已添加了几个新的 API,并提出了一项重大更改以使 IContainerRegistry 具有流畅的 API.
我以前使用过 DryIOC 作为我的 IOC,但我想尝试将 Splat 用于我的 IOC,是否可以将这两者结合起来?
我试过让 LoginModule
继承 IModule
所以我的 LoginModule
class:
public void OnInitialized(IContainerProvider containerProvider)
{
Locator.CurrentMutable.RegisterLazySingleton(() => new ServiceEntityMapper(), typeof(IServiceEntityMapper));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginAPIService(), typeof(ILoginAPIService));
Locator.CurrentMutable.RegisterLazySingleton(() => new LoginManager(
Locator.Current.GetService<IServiceEntityMapper>(),
Locator.Current.GetService<ILoginAPIService>()), typeof(ILoginManager));
}
我的视图模型构造函数有这个:
public LoginViewModel(INavigationService navigationService, ILoginManager loginManager = null) : base(navigationService)
{
LoginManager = loginManager ?? Locator.Current.GetService<ILoginManager>();
}
结果,每当我导航到该页面时,我都会收到此异常
{System.TypeLoadException: Could not resolve the signature of a virtual method
at System.Lazy`1[T].CreateValue () [0x00081] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].LazyInitValue () [0x00080] in <fe08c003e91342eb83df1ca48302ddbb>:0
at System.Lazy`1[T].get_Value () [0x0003a] in <fe08c003e91342eb83df1ca48302ddbb>:0
at Splat.DependencyResolverMixins+<>c__DisplayClass7_0.<RegisterLazySingleton>b__0 () [0x00000] in <89c762f12a12451a8970372dc9921547>:0
at Splat.ModernDependencyResolver.GetService (System.Type serviceType, System.String contract) [0x00032] in <89c762f12a12451a8970372dc9921547>:0
at Splat.DependencyResolverMixins.GetService[T] (Splat.IDependencyResolver resolver, System.String contract)
据我所知,Splat 是一个服务定位器,而不是真正的 DI 容器。也就是说,您当然不限于基本的 Prism 实现,因为提供这些实现是为了使其易于采用和入门。在您的情况下,我可能建议您创建自己的 IContainerExtension 实现并继承自 PrismApplicationBase。
通过查看 Unity or DryIoc... there is a similar example using the Grace DI Container 的实现,您可以看到在您的应用程序 class 中确实没有太多额外的工作。请记住,自上次预览以来已添加了几个新的 API,并提出了一项重大更改以使 IContainerRegistry 具有流畅的 API.