none
MVVM with WCF RRS feed

  • Question

  • I begin to learn the MVVM in the Windows Phone8, I know that we can implement the MVVM with WCF, but I can not find any example on the Internet. Hope someboby can give me a code example of it.
    Wednesday, June 25, 2014 9:11 AM

Answers

  • Hi,

    MVVM pattern provides a flexible way to build applications(such as windows phone, silverlight and so no) that promotes code re-use, simplifies maintenance and supports testing. And we can use the wcf service to provide data to the MVVM.

    And I have created the following example for using the WCF with the MVVM, please try to check it:
    In the IService1:

     [ServiceContract]
        public interface IService1
        {
        
           [OperationContract]
           bool Loginuser(User user);
    
        }


    In the Service1:

    public class Service1 : IService1
        {
    
    
            public bool Loginuser(User user)
            {
                LoginEntities1 ss = new LoginEntities1();//get the data from the database
                logintable1 table = ss.logintable1.Where(u => u.username == user.username && u.password == user.password).FirstOrDefault();
                if (table == null)
                {
                    return false;
                }
    
                else return true;
    
            }
    }


    Then in the window phone application the ViewModel.cs:

     private string username;
            public string UserName
            {
                get { return username; }
                set { username = value;
                      RaisePropertyChanged("UserName");
                    }
            }
            private string password;

            public string Password
            {
                get { return password; }
                set { password = value;
                      RaisePropertyChanged("Password");
                    }
            }

    public DelegateCommand LoginCommand { get; private set; } public ViewModel() { LoginCommand = new DelegateCommand(new Action(this.Login)); } public void Login() { LoginServiceRef.User u = new LoginServiceRef.User { UserName = this.UserName, Password = this.Password }; LoginServiceRef.LoginServiceClient lc = new LoginServiceRef.LoginServiceClient(); lc.LoginCompleted += new EventHandler<LoginServiceRef.LoginCompletedEventArgs>(lc_LoginCompleted); lc.LoginAsync(u); } private void lc_LoginCompleted(object sender, LoginServiceRef.LoginCompletedEventArgs e) { MessageBox.Show(e.Result ? "successfully login" : "failed"); }

    In the windows phone MainPage.xaml:

      <TextBox Name="usernametext" HorizontalAlignment="Left" Height="72" Margin="92,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding UserName,Mode=TwoWay}"/>
      <TextBox Name="passwordtext" HorizontalAlignment="Left" Height="72" Margin="92,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding Password,Mode=TwoWay}"/>
      <Button Content="Button" HorizontalAlignment="Left" Margin="92,122,0,0" VerticalAlignment="Top" Command="{Binding LoginCommand}"/>


    In the MainPage.xaml.cs:

     public MainPage()
    
            {
    
                InitializeComponent();
    
               this.DataContext = new ViewModel() ;
    
            }

    Besides, the following article may also help you, please try to check it:
    http://www.c-sharpcorner.com/UploadFile/raj1979/implement-mvvm-pattern-in-windows-phone-7-using-wcf-service/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Thursday, June 26, 2014 7:54 AM
    Moderator
  • I begin to learn the MVVM in the Windows Phone8, I know that we can implement the MVVM with WCF, but I can not find any example on the Internet. Hope someboby can give me a code example of it.

    What you need to learn first is how to use n-tier. You need to learn how to develop a solution using N-tier and WCF,  You figure that out, then it doesn't matter if one is using Windows Phone 8 application using MVVM, Windows Desktop solution using MVP, ASP.NET using MVP or MVC because they are all using n-tier.

    There are plenty of example and tutorials on how to develop .NET UI solutions using n-tier and WCF using n-tier. It doesn't matter what UI design pattern of MVVM, MVP, or MVC is being used or what type of UI of ASP.NET, Windows form, etc etc, if you know how to use N-tier.

    http://en.wikipedia.org/wiki/Multitier_architecture

    Use Bing or Google, because there is something out there on using MVVM with n-tier and for WCF too using n-tier. But first,  maybe you just need to learn how to create a simple n-tier solution and what it means before using move advanced things using n-tier.

    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 11:16 AM
  • MVVM is a well structured pattern for developing Silverlight/WPF based application which is basically UI application.

    WCF is different domain. Using WCF you can build you Silverlight/WPF application service oriented. But you cannot impliment MVVM in WCF like you cannot impliment the same methodology that prepares a cup of tea to prepare noodles.


    One good question is equivalent to ten best answers.

    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 11:47 AM
  • The MVVM (Model-View-ViewModel) desing pattern targets user interface (UI) applications such as WPF, Silverlight, Windows Phone and Windows Store Apps and is not really applicable to WCF since a WCF service has no views: http://en.wikipedia.org/wiki/Model_View_ViewModel
    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 1:41 PM

All replies

  • I begin to learn the MVVM in the Windows Phone8, I know that we can implement the MVVM with WCF, but I can not find any example on the Internet. Hope someboby can give me a code example of it.

    What you need to learn first is how to use n-tier. You need to learn how to develop a solution using N-tier and WCF,  You figure that out, then it doesn't matter if one is using Windows Phone 8 application using MVVM, Windows Desktop solution using MVP, ASP.NET using MVP or MVC because they are all using n-tier.

    There are plenty of example and tutorials on how to develop .NET UI solutions using n-tier and WCF using n-tier. It doesn't matter what UI design pattern of MVVM, MVP, or MVC is being used or what type of UI of ASP.NET, Windows form, etc etc, if you know how to use N-tier.

    http://en.wikipedia.org/wiki/Multitier_architecture

    Use Bing or Google, because there is something out there on using MVVM with n-tier and for WCF too using n-tier. But first,  maybe you just need to learn how to create a simple n-tier solution and what it means before using move advanced things using n-tier.

    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 11:16 AM
  • MVVM is a well structured pattern for developing Silverlight/WPF based application which is basically UI application.

    WCF is different domain. Using WCF you can build you Silverlight/WPF application service oriented. But you cannot impliment MVVM in WCF like you cannot impliment the same methodology that prepares a cup of tea to prepare noodles.


    One good question is equivalent to ten best answers.

    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 11:47 AM
  • The MVVM (Model-View-ViewModel) desing pattern targets user interface (UI) applications such as WPF, Silverlight, Windows Phone and Windows Store Apps and is not really applicable to WCF since a WCF service has no views: http://en.wikipedia.org/wiki/Model_View_ViewModel
    • Marked as answer by RR TT Thursday, June 26, 2014 1:34 PM
    Wednesday, June 25, 2014 1:41 PM
  • Hi,

    MVVM pattern provides a flexible way to build applications(such as windows phone, silverlight and so no) that promotes code re-use, simplifies maintenance and supports testing. And we can use the wcf service to provide data to the MVVM.

    And I have created the following example for using the WCF with the MVVM, please try to check it:
    In the IService1:

     [ServiceContract]
        public interface IService1
        {
        
           [OperationContract]
           bool Loginuser(User user);
    
        }


    In the Service1:

    public class Service1 : IService1
        {
    
    
            public bool Loginuser(User user)
            {
                LoginEntities1 ss = new LoginEntities1();//get the data from the database
                logintable1 table = ss.logintable1.Where(u => u.username == user.username && u.password == user.password).FirstOrDefault();
                if (table == null)
                {
                    return false;
                }
    
                else return true;
    
            }
    }


    Then in the window phone application the ViewModel.cs:

     private string username;
            public string UserName
            {
                get { return username; }
                set { username = value;
                      RaisePropertyChanged("UserName");
                    }
            }
            private string password;

            public string Password
            {
                get { return password; }
                set { password = value;
                      RaisePropertyChanged("Password");
                    }
            }

    public DelegateCommand LoginCommand { get; private set; } public ViewModel() { LoginCommand = new DelegateCommand(new Action(this.Login)); } public void Login() { LoginServiceRef.User u = new LoginServiceRef.User { UserName = this.UserName, Password = this.Password }; LoginServiceRef.LoginServiceClient lc = new LoginServiceRef.LoginServiceClient(); lc.LoginCompleted += new EventHandler<LoginServiceRef.LoginCompletedEventArgs>(lc_LoginCompleted); lc.LoginAsync(u); } private void lc_LoginCompleted(object sender, LoginServiceRef.LoginCompletedEventArgs e) { MessageBox.Show(e.Result ? "successfully login" : "failed"); }

    In the windows phone MainPage.xaml:

      <TextBox Name="usernametext" HorizontalAlignment="Left" Height="72" Margin="92,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding UserName,Mode=TwoWay}"/>
      <TextBox Name="passwordtext" HorizontalAlignment="Left" Height="72" Margin="92,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding Password,Mode=TwoWay}"/>
      <Button Content="Button" HorizontalAlignment="Left" Margin="92,122,0,0" VerticalAlignment="Top" Command="{Binding LoginCommand}"/>


    In the MainPage.xaml.cs:

     public MainPage()
    
            {
    
                InitializeComponent();
    
               this.DataContext = new ViewModel() ;
    
            }

    Besides, the following article may also help you, please try to check it:
    http://www.c-sharpcorner.com/UploadFile/raj1979/implement-mvvm-pattern-in-windows-phone-7-using-wcf-service/ .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Thursday, June 26, 2014 7:54 AM
    Moderator
  • Hi,

    MVVM pattern provides a flexible way to build applications(such as windows phone, silverlight and so no) that promotes code re-use, simplifies maintenance and supports testing. And we can use the wcf service to provide data to the MVVM.

    And I have created the following example for using the WCF with the MVVM, please try to check it:
    In the IService1:

     [ServiceContract]
        public interface IService1
        {
        
           [OperationContract]
           bool Loginuser(User user);
    
        }


    In the Service1:

    public class Service1 : IService1
        {
    
    
            public bool Loginuser(User user)
            {
                LoginEntities1 ss = new LoginEntities1();//get the data from the database
                logintable1 table = ss.logintable1.Where(u => u.username == user.username && u.password == user.password).FirstOrDefault();
                if (table == null)
                {
                    return false;
                }
    
                else return true;
    
            }
    }


    Then in the window phone application the ViewModel.cs:

     private string username;
            public string UserName
            {
                get { return username; }
                set { username = value;
                      RaisePropertyChanged("UserName");
                    }
            }
            private string password;

            public string Password
            {
                get { return password; }
                set { password = value;
                      RaisePropertyChanged("Password");
                    }
            }

    public DelegateCommand LoginCommand { get; private set; } public ViewModel() { LoginCommand = new DelegateCommand(new Action(this.Login)); } public void Login() { LoginServiceRef.User u = new LoginServiceRef.User { UserName = this.UserName, Password = this.Password }; LoginServiceRef.LoginServiceClient lc = new LoginServiceRef.LoginServiceClient(); lc.LoginCompleted += new EventHandler<LoginServiceRef.LoginCompletedEventArgs>(lc_LoginCompleted); lc.LoginAsync(u); } private void lc_LoginCompleted(object sender, LoginServiceRef.LoginCompletedEventArgs e) { MessageBox.Show(e.Result ? "successfully login" : "failed"); }

    In the windows phone MainPage.xaml:

      <TextBox Name="usernametext" HorizontalAlignment="Left" Height="72" Margin="92,24,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding UserName,Mode=TwoWay}"/>
      <TextBox Name="passwordtext" HorizontalAlignment="Left" Height="72" Margin="92,122,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="354" Text="{Binding Password,Mode=TwoWay}"/>
      <Button Content="Button" HorizontalAlignment="Left" Margin="92,122,0,0" VerticalAlignment="Top" Command="{Binding LoginCommand}"/>


    In the MainPage.xaml.cs:

     public MainPage()
    
            {
    
                InitializeComponent();
    
               this.DataContext = new ViewModel() ;
    
            }

    Besides, the following article may also help you, please try to check it:
    http://www.c-sharpcorner.com/UploadFile/raj1979/implement-mvvm-pattern-in-windows-phone-7-using-wcf-service/ .

    Best Regards,
    Amy Peng


    Excellent!
    Thank you Amy Peng. And thanks all.


    • Edited by RR TT Thursday, June 26, 2014 1:35 PM
    Thursday, June 26, 2014 1:33 PM