2013-08-27 25 views
2

我有一個LoginService及其在MvvmCross(Mvx)插件中定義的接口。MvvmCross語法消耗插件中定義的服務以及應用程序內該服務的生命週期

我MVX應用的核心PCL消耗這個插件使用App.cs以這樣的方式來註冊它

 CreatableTypes(typeof(LoginService).GetTypeInfo().Assembly) 
      .EndingWith("Service") 
      .AsInterfaces() 
      .RegisterAsLazySingleton(); 

然後使用一個CustomAppStart類來啓動應用程序,以確定哪些視圖模型與啓動應用程序:

RegisterAppStart(new CustomAppStart(Mvx.Resolve<ILoginService>())); 

隨着CustomAppStart定義爲:

 public CustomAppStart(ILoginService loginService) 
     { 
      _loginService = loginService; 
     } 

     public void Start(object hint = null) 
     { 
      if (!_loginService.IsLoggedIn) 
      { 
       ShowViewModel<LoginViewModel>(); 
      } 
      else 
      { 
       ShowViewModel<HomeViewModel>(); 
      } 
     } 

兩個部分的問題:我用正確的語法與CreatableTypesRegisterAppStart使用LoginService是在插件定義

  1. 是誰?

  2. 我知道,通過使用RegisterAsLazySingleton()當我提出要求,我會得到相同的 login服務的實例,但 該實例的生命週期是什麼?一旦我在CustomAppStart中請求它,那麼該實例的狀態 只是留在內存中供我調用和使用,或者當我的 ViewModels之一請求在其 構造函數中的同一ILoginService實例時,Mvx奇蹟般地保存並重新水化其狀態?

回答

2

1.您正在使用CreatableTypes的語法看起來不錯給我。

當不使用它裏面App然後,文件的語法是:

「 而且還可以,當然,使用相同類型的註冊邏輯的上比核心其他組件 - 例如:

typeof(Reusable.Helpers.MyHelper).Assembly.CreatableTypes() 
     .EndingWith("Helper") 
     .AsInterfaces() 
     .RegisterAsDynamic(); 

「 從https://github.com/slodge/MvvmCross/wiki/Service-Location-and-Inversion-of-Control#bulk-registration-by-convention

CreateableTypes()EndingWith()等作用在一切都只是相當短的擴展方法- 你可以在IoC/MvxTypeExtensions.cs - 例如

public static IEnumerable<Type> CreatableTypes(this Assembly assembly) 
    { 
     return assembly 
      .ExceptionSafeGetTypes() 
      .Where(t => !t.IsAbstract) 
      .Where(t => t.GetConstructors(BindingFlags.Instance | BindingFlags.Public).Any()); 
    } 

2.一個單身的生命週期,一旦創建它停留在RAM中,只要該應用程序。將其從RAM中刪除的唯一方法是刪除對它的所有引用 - 包括刪除IoC容器引用(只能通過在其位置註冊新實現來完成)。

+0

有沒有這樣做的方式,沒有Core在開發時需要了解其他程序集。 由於接口位於Core程序集中,因此我需要從服務程序集中引用Core,這意味着我不能從Core中引用服務,除非創建新的程序集,僅用於接口。 – JonathanPeel