2016-04-13 24 views
2

私はアプリの認証プロパティに関する助けが必要です。MVVM WPF - ログインロジック

enter image description here

Q1:ViewModelにに私はidUser(サーバーからの応答を)持っているモデルを持っているので、私は、いくつかのモデルJSONをデシリアライズ。 FriendsViewModelでMVVMアーキテクチャを壊さずにその情報をどのように使用できますか?

敬具、 アントワーヌ

+0

あなたが実行フローについて尋ねますか? – Dennis

+0

@Dennisはい実行フローと私が応答とどのようにモデルを含むことができますか?編集:私は、このモデルは、LoginViewModelに記入し、もうランタイムを変更しないことを追加したいと思います。私はIdUserを時々チェックして、Webサービスにリクエストを送信したい – Antoine

答えて

0

実行フローは、このようなことができます:

  • アプリケーションビューモデルが起動する(あなたの図は、このビューモデルをミス)。
  • ログインビューモデルを使用して非同期にログインを要求します。
  • ログインが成功すると、アプリケーションビューモデルは一般的なビューモデルを作成します。
  • 一般的なビューモデルの読み込み友人
  • ログインに失敗した場合、アプリケーションビューモデルはエラービューモデルを作成します。

サンプルコード(ビューモデルを作成し、サービスを注入するために、お気に入りのIoCを使用する場合があります):

public class ApplicationVm 
{ 
    private readonly IInteractionService interactionService; 
    private readonly IDataService dataService; 

    private async Task LoadDataAsync() 
    { 
     try 
     { 
      // we need to ask for credentials 
      var loginVm = new LoginVm(); 
      if (interactionService.ShowInDialog(loginVm) == true) 
      { 
       // performing login 
       var userId = await dataService.LoginAsync(loginVm.UserName, loginVm.Password); 
       // setting content to general view model, which is the payload of our app 
       Content = new GeneralVm(userId, dataService); 
      } 
      else 
      { 
       // setting content to stub, which shows us "Login cancelled" message 
       Content = new StubVm { Message = "Login cancelled" }; 
      } 
     } 
     catch (Exception ex) 
     { 
      // setting content to stub, which shows us "Login failed" message 
      Content = new StubVm { Message = $"Login failed: {ex.Message}" }; 
     } 
    } 

    public ApplicationVm(IInteractionService interactionService, IDataService dataService) 
    { 
     this.interactionService = interactionService; 
     this.dataService = dataService; 
     var _ = LoadDataAsync(); 
    } 

    public object Content { get; private set; } 
} 

public interface IInteractionService 
{ 
    bool? ShowInDialog(object viewModel); 
} 

public interface IDataService 
{ 
    Task<int> LoginAsync(string userName, string password); 
    Task<IEnumerable<Friend>> GetFriendsAsync(int userId); 
} 

public class LoginVm 
{ 
    public string UserName { get; set; } 
    public string Password { get; set; } 
} 

public class GeneralVm 
{ 
    private readonly int userId; 
    private readonly IDataService dataService; 

    private async Task LoadFriendsAsync() 
    { 
     var friends = await dataService.GetFriendsAsync(userId); 
     Friends = friends 
      .Select(model => new FriendVm(userId, model)); 
    } 

    public GeneralVm(int userId, IDataService dataService) 
    { 
     this.userId = userId; 
     this.dataService = dataService; 
    } 

    public IEnumerable<FriendVm> Friends { get; private set; } 
} 

public class FriendVm 
{ 
    public FriendVm(int userId, Friend model) 
    { 
    } 
} 

public class Friend { } 

public class StubVm 
{ 
    public string Message { get; set; } 
}