In which class should I load my data when using MVVM

I am currently learning C #, and recently I learned about the MVVM design pattern for WPF. I am writing a simple program as a way to practice this, but I'm not sure where I should write a method that loads the data.

I have a SalesSheet class as shown below. This contains the data that I am loading from the .xls file.

class SalesSheet
{
    public List<Row> Rows { get; set; }

    public SalesSheet()
    {
        Rows = new List<Row>();
    }

    public class Row
    {
        public string CallType { get; set; }
        public string HistoryText { get; set; }
    }

}

My question is: where should I write a method that loads data? Is it wrong for you to write a method such as:

private void LoadData(string filePath)

in the model and call it a constructor?

Should I download it from ViewModel?

+4
source share
4 answers

, WPF :

  • ProjectName
    • DataAccess
    • Datatypes
    • ViewModels

DataAccess - , . , ; , . , ( ) ... , - ( ), , .

, DataProvider. DataProvider . , :

protected DataProvider DataProvider
{
    get { return new DataProvider(); }
}

, - :

SomeObject someObject = DataProvider.LoadData(filePath);

, , , , .

+6

MVVM LoadData . . , viewmodels , .

+3

. xaml , DataContext.

, . LoadData. , . , Loaded .

(xaml):

<Window x:Class="MyWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Test" 
        xmlns:viewModel="clr-namespace:ViewModels" Loaded="Window_Loaded">
    <Window.DataContext>
        <viewModel:ExampleViewModel/>
    </Window.DataContext>

( ):

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        ((ExampleViewModel)this.DataContext).LoadData();
    }

Loaded , xaml ( "Microsoft.Expression.Interactions" "System.Windows.Interactivity" ):

<Window x:Class="MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Test" 
    xmlns:viewModel="clr-namespace:ViewModels"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"            
    >
<Window.DataContext>
    <viewModel:ExampleViewModel/>
</Window.DataContext>
<i:Interaction.Triggers>
    <i:EventTrigger EventName="Loaded">
        <ei:CallMethodAction TargetObject="{Binding}" MethodName="LoadData"/>
    </i:EventTrigger>
</i:Interaction.Triggers>   

LoadData ViewModel. , . .

public class ExampleViewModel
{
    /// <summary>
    /// Constructor.
    /// </summary>
    public ExampleViewModel()
    {
        // Do NOT do complex stuff here
    }


    public void LoadData()
    {
        // Make a call to the repository class here
        // to set properties of your view model
    }

, LoadData, .

. ( less) , . ( ).

, , , , .

+1

.

DataService .

  • Core ( -) SalesDataService.
  • ( )
  • , GetRows() GetSingleById(int id)... DataService
  • protected SalesDataService _salesDataService
  • Create an instance _salesDataServicein the model constructor of your view
  • Call GetRows()if necessary (if no one requires loading data, then put it in the constructor)
0
source

Source: https://habr.com/ru/post/1532530/


All Articles