|
|
@ -1,8 +1,7 @@ |
|
|
|
using Microsoft.Practices.Unity; |
|
|
|
using eShopOnContainers.Core.ViewModels; |
|
|
|
using eShopOnContainers.Services; |
|
|
|
using System; |
|
|
|
using System.Globalization; |
|
|
|
using System.Reflection; |
|
|
|
using eShopOnContainers.Core.Services.Catalog; |
|
|
|
using eShopOnContainers.Core.Services.OpenUrl; |
|
|
|
using eShopOnContainers.Core.Services.RequestProvider; |
|
|
@ -10,53 +9,54 @@ using eShopOnContainers.Core.Services.Basket; |
|
|
|
using eShopOnContainers.Core.Services.Identity; |
|
|
|
using eShopOnContainers.Core.Services.Order; |
|
|
|
using eShopOnContainers.Core.Services.User; |
|
|
|
using Xamarin.Forms; |
|
|
|
|
|
|
|
namespace eShopOnContainers.Core.ViewModels.Base |
|
|
|
namespace eShopOnContainers.ViewModels.Base |
|
|
|
{ |
|
|
|
public static class ViewModelLocator |
|
|
|
public class ViewModelLocator |
|
|
|
{ |
|
|
|
private static readonly IUnityContainer _unityContainer = new UnityContainer(); |
|
|
|
private bool _useMockService; |
|
|
|
private readonly IUnityContainer _unityContainer; |
|
|
|
|
|
|
|
public static readonly BindableProperty AutoWireViewModelProperty = |
|
|
|
BindableProperty.CreateAttached("AutoWireViewModel", typeof(bool), typeof(ViewModelLocator), default(bool), propertyChanged: OnAutoWireViewModelChanged); |
|
|
|
private static readonly ViewModelLocator _instance = new ViewModelLocator(); |
|
|
|
|
|
|
|
public static bool GetAutoWireViewModel(BindableObject bindable) |
|
|
|
{ |
|
|
|
return (bool)bindable.GetValue(ViewModelLocator.AutoWireViewModelProperty); |
|
|
|
} |
|
|
|
public static ViewModelLocator Instance |
|
|
|
{ |
|
|
|
get { return _instance; } |
|
|
|
} |
|
|
|
|
|
|
|
public bool UseMockService |
|
|
|
{ |
|
|
|
get { return _useMockService; } |
|
|
|
set { _useMockService = value; ; } |
|
|
|
} |
|
|
|
|
|
|
|
public static void SetAutoWireViewModel(BindableObject bindable, bool value) |
|
|
|
{ |
|
|
|
bindable.SetValue(ViewModelLocator.AutoWireViewModelProperty, value); |
|
|
|
} |
|
|
|
protected ViewModelLocator() |
|
|
|
{ |
|
|
|
_unityContainer = new UnityContainer(); |
|
|
|
|
|
|
|
public static bool UseMockService { get; set; } |
|
|
|
// Services
|
|
|
|
_unityContainer.RegisterType<IDialogService, DialogService>(); |
|
|
|
RegisterSingleton<INavigationService, NavigationService>(); |
|
|
|
_unityContainer.RegisterType<IOpenUrlService, OpenUrlService>(); |
|
|
|
_unityContainer.RegisterType<IRequestProvider, RequestProvider>(); |
|
|
|
_unityContainer.RegisterType<IIdentityService, IdentityService>(); |
|
|
|
|
|
|
|
public static void Initialize() |
|
|
|
{ |
|
|
|
// Services
|
|
|
|
_unityContainer.RegisterType<IDialogService, DialogService>(); |
|
|
|
_unityContainer.RegisterType<INavigationService, NavigationService>(new ContainerControlledLifetimeManager()); |
|
|
|
_unityContainer.RegisterType<IOpenUrlService, OpenUrlService>(); |
|
|
|
_unityContainer.RegisterType<IRequestProvider, RequestProvider>(); |
|
|
|
_unityContainer.RegisterType<IIdentityService, IdentityService>(); |
|
|
|
_unityContainer.RegisterType<ICatalogService, CatalogMockService>(); |
|
|
|
_unityContainer.RegisterType<IBasketService, BasketMockService>(); |
|
|
|
_unityContainer.RegisterType<IUserService, UserMockService>(); |
|
|
|
_unityContainer.RegisterType<ICatalogService, CatalogMockService>(); |
|
|
|
_unityContainer.RegisterType<IBasketService, BasketMockService>(); |
|
|
|
_unityContainer.RegisterType<IUserService, UserMockService>(); |
|
|
|
|
|
|
|
// View models
|
|
|
|
_unityContainer.RegisterType<BasketViewModel>(); |
|
|
|
_unityContainer.RegisterType<CatalogViewModel>(); |
|
|
|
_unityContainer.RegisterType<CheckoutViewModel>(); |
|
|
|
_unityContainer.RegisterType<LoginViewModel>(); |
|
|
|
_unityContainer.RegisterType<MainViewModel>(); |
|
|
|
_unityContainer.RegisterType<OrderDetailViewModel>(); |
|
|
|
_unityContainer.RegisterType<ProfileViewModel>(); |
|
|
|
_unityContainer.RegisterType<SettingsViewModel>(); |
|
|
|
} |
|
|
|
// View models
|
|
|
|
_unityContainer.RegisterType<BasketViewModel>(); |
|
|
|
_unityContainer.RegisterType<CatalogViewModel>(); |
|
|
|
_unityContainer.RegisterType<CheckoutViewModel>(); |
|
|
|
_unityContainer.RegisterType<LoginViewModel>(); |
|
|
|
_unityContainer.RegisterType<MainViewModel>(); |
|
|
|
_unityContainer.RegisterType<OrderDetailViewModel>(); |
|
|
|
_unityContainer.RegisterType<ProfileViewModel>(); |
|
|
|
_unityContainer.RegisterType<SettingsViewModel>(); |
|
|
|
} |
|
|
|
|
|
|
|
public static void UpdateDependencies(bool useMockServices) |
|
|
|
public void UpdateDependencies(bool useMockServices) |
|
|
|
{ |
|
|
|
// Change injected dependencies
|
|
|
|
if (useMockServices) |
|
|
@ -80,31 +80,29 @@ namespace eShopOnContainers.Core.ViewModels.Base |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static T Resolve<T>() |
|
|
|
public T Resolve<T>() |
|
|
|
{ |
|
|
|
return _unityContainer.Resolve<T>(); |
|
|
|
} |
|
|
|
|
|
|
|
private static void OnAutoWireViewModelChanged(BindableObject bindable, object oldValue, object newValue) |
|
|
|
{ |
|
|
|
var view = bindable as Element; |
|
|
|
if (view == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
public object Resolve(Type type) |
|
|
|
{ |
|
|
|
return _unityContainer.Resolve(type); |
|
|
|
} |
|
|
|
|
|
|
|
public void Register<T>(T instance) |
|
|
|
{ |
|
|
|
_unityContainer.RegisterInstance<T>(instance); |
|
|
|
} |
|
|
|
|
|
|
|
var viewType = view.GetType(); |
|
|
|
var viewName = viewType.FullName.Replace(".Views.", ".ViewModels."); |
|
|
|
var viewAssemblyName = viewType.GetTypeInfo().Assembly.FullName; |
|
|
|
var viewModelName = string.Format(CultureInfo.InvariantCulture, "{0}Model, {1}", viewName, viewAssemblyName); |
|
|
|
public void Register<TInterface, T>() where T : TInterface |
|
|
|
{ |
|
|
|
_unityContainer.RegisterType<TInterface, T>(); |
|
|
|
} |
|
|
|
|
|
|
|
var viewModelType = Type.GetType(viewModelName); |
|
|
|
if (viewModelType == null) |
|
|
|
{ |
|
|
|
return; |
|
|
|
} |
|
|
|
var viewModel = _unityContainer.Resolve(viewModelType); |
|
|
|
view.BindingContext = viewModel; |
|
|
|
} |
|
|
|
public void RegisterSingleton<TInterface, T>() where T : TInterface |
|
|
|
{ |
|
|
|
_unityContainer.RegisterType<TInterface, T>(new ContainerControlledLifetimeManager()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |