Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM Light:使用DataService检索数据库项_C#_Wpf_Entity Framework_Mvvm Light - Fatal编程技术网

C# MVVM Light:使用DataService检索数据库项

C# MVVM Light:使用DataService检索数据库项,c#,wpf,entity-framework,mvvm-light,C#,Wpf,Entity Framework,Mvvm Light,我正在处理一个示例MVVM Light项目,并正在实现SimpleIoc ViewModelLocator。我已经能够构建一个IRepositoryService,从数据库(即公司、员工等)检索信息,并将信息存储到ObservableCollection中。然后,IRepositoryService将ObservableCollection返回给ViewModel。这是如何实现的: public interface IRepositoryService { void GetCompan

我正在处理一个示例MVVM Light项目,并正在实现SimpleIoc ViewModelLocator。我已经能够构建一个IRepositoryService,从数据库(即公司、员工等)检索信息,并将信息存储到ObservableCollection中。然后,IRepositoryService将ObservableCollection返回给ViewModel。这是如何实现的:

public interface IRepositoryService
{

    void GetCompanies(Action<ObservableCollection<Company>, Exception> callback);

    void GetEmployees(Action<ObservableCollection<Employee>, Exception> callback);

}


class RepositoryService : IRepositoryService
{
    public void GetCompanies(Action<ObservableCollection<Company>, Exception> callback)
    {
        using (var context = new SidekickEntities())
        {
            var _companies = from co in context.Companies
                             select co;
            callback(new ObservableCollection<Company>(_companies), null);
        }
    }

    public void GetEmployees(Action<ObservableCollection<Employee>, Exception> callback)
    {
        using (var context = new SidekickEntities())
        {
            var _employees = from co in context.Employees
                             select co;
            callback(new ObservableCollection<Employee>(_employees), null);
        }

    }
}

下面是我的一个viewmodels的示例

class YearModel : INotifyPropertyChanged
{
    #region Members

    Year _year;

    #endregion

    #region Properties

    public Year Year
    {
        get { return _year; }
    }

    public Int32 id
    {
        get { return Year.id; }
        set
        {
            Year.id = value;
            NotifyPropertyChanged("id");
        }
    }

    public String Code
    {
        get { return Year.Code; }
        set
        {
            Year.Code = value;
            NotifyPropertyChanged("Code");
        }
    }

    public String Description
    {
        get { return Year.Description; }
        set
        {
            Year.Description = value;
            NotifyPropertyChanged("Description");
        }
    }

    public DateTime DateCreated
    {
        get { return Year.DateCreated; }
        set
        {
            Year.DateCreated = value;
            NotifyPropertyChanged("DateCreated");
        }
    }

    public Int32 CreatedByID
    {
        get { return Year.CreatedByID; }
        set
        {
            Year.CreatedByID = value;
            NotifyPropertyChanged("CreatedByID");
        }
    }

    public String CreatedByDesc
    {
        get { return Year.CreatedByDesc; }
        set
        {
            Year.CreatedByDesc = value;
            NotifyPropertyChanged("CreatedByDesc");
        }
    }

    public DateTime DateEdited
    {
        get { return Year.DateEdited; }
        set
        {
            Year.DateEdited = value;
            NotifyPropertyChanged("DateEdited");
        }
    }

    public Int32 EditedByID
    {
        get { return Year.EditedByID; }
        set
        {
            Year.EditedByID = value;
            NotifyPropertyChanged("EditedByID");
        }
    }

    public String EditedByDesc
    {
        get { return Year.EditedByDesc; }
        set
        {
            Year.EditedByDesc = value;
            NotifyPropertyChanged("EditedByDesc");
        }
    }

    #endregion

    #region Construction

    public YearModel()
    {
        this._year = new Year
        {
            id = 0,
            Code = "",
            Description = "",
            DateCreated = new DateTime(1900, 01, 01, 00, 00, 00),
            CreatedByID = 0,
            CreatedByDesc = "",
            DateEdited = new DateTime(1900, 01, 01, 00, 00, 00),
            EditedByID = 0,
            EditedByDesc = ""
        };
    }

    #endregion

    #region Property Change Events

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion

    #region Commands

    public ICommand EditCommand
    {
        get { return new RelayCommand(EditYear); }
    }

    public ICommand AddCommand
    {
        get { return new RelayCommand(AddYear); }
    }

    #endregion

    #region Functions

    public void EditYear()
    {
        try
        {

            Service1Client service = new Service1Client();

            Year y = new Year();
            y.id = this.id;
            y.Code = this.Code;
            y.Description = this.Description;
            y.DateEdited = DateTime.Now;
            y.EditedByID = (Int32)Application.Current.Resources["UserID"];


            service.EditYear(y);
            MessageBox.Show("Your Year was edited successfully", "Success", MessageBoxButton.OK);
        }
        catch (Exception ex)
        {
            logError le = new logError();
            le.log(ex, "YearViewModel", "EDIT");
        }
    }

    public void AddYear()
    {
        try
        {
            Service1Client service = new Service1Client();

            Year y = new Year();
            y.Code = this.Code;
            y.Description = this.Description;
            y.DateCreated = DateTime.Now;
            y.CreatedByID = (Int32)Application.Current.Resources["UserID"];
            y.DateEdited = this.DateEdited;


            service.AddYear(y);
            MessageBox.Show("Your new Year was entered successfully", "Success", MessageBoxButton.OK);
        }
        catch (Exception ex)
        {
            logError le = new logError();
            le.log(ex, "YearViewModel", "ADD");
        }
    }

    #endregion
}
}


类中继命令:ICommand
{
行动;
公共中继命令(操作执行)
{
行动=执行;
}
公共布尔CanExecute(对象参数)
{
返回true;
}
公共事件处理程序CanExecuteChanged;
public void Execute(对象参数)
{
动作();
}
}

只需将对象添加到ObservableCollection(或修改现有对象),一旦以某种方式触发save命令,就可以通过ObservableCollection中的对象进行编辑,并根据需要进行更新或插入。显然,您希望将这些方法添加到IRepositoryService中,因为您已经在使用这些方法来获取记录……我将使用编辑和添加命令创建一个视图模型。这样,所有的东西都包含在一个地方。
CompanyList.Add(new Company(-1, "Chipotle", "1400 High Street", "", "Columbus", "OH", "43235"));
class YearModel : INotifyPropertyChanged
{
    #region Members

    Year _year;

    #endregion

    #region Properties

    public Year Year
    {
        get { return _year; }
    }

    public Int32 id
    {
        get { return Year.id; }
        set
        {
            Year.id = value;
            NotifyPropertyChanged("id");
        }
    }

    public String Code
    {
        get { return Year.Code; }
        set
        {
            Year.Code = value;
            NotifyPropertyChanged("Code");
        }
    }

    public String Description
    {
        get { return Year.Description; }
        set
        {
            Year.Description = value;
            NotifyPropertyChanged("Description");
        }
    }

    public DateTime DateCreated
    {
        get { return Year.DateCreated; }
        set
        {
            Year.DateCreated = value;
            NotifyPropertyChanged("DateCreated");
        }
    }

    public Int32 CreatedByID
    {
        get { return Year.CreatedByID; }
        set
        {
            Year.CreatedByID = value;
            NotifyPropertyChanged("CreatedByID");
        }
    }

    public String CreatedByDesc
    {
        get { return Year.CreatedByDesc; }
        set
        {
            Year.CreatedByDesc = value;
            NotifyPropertyChanged("CreatedByDesc");
        }
    }

    public DateTime DateEdited
    {
        get { return Year.DateEdited; }
        set
        {
            Year.DateEdited = value;
            NotifyPropertyChanged("DateEdited");
        }
    }

    public Int32 EditedByID
    {
        get { return Year.EditedByID; }
        set
        {
            Year.EditedByID = value;
            NotifyPropertyChanged("EditedByID");
        }
    }

    public String EditedByDesc
    {
        get { return Year.EditedByDesc; }
        set
        {
            Year.EditedByDesc = value;
            NotifyPropertyChanged("EditedByDesc");
        }
    }

    #endregion

    #region Construction

    public YearModel()
    {
        this._year = new Year
        {
            id = 0,
            Code = "",
            Description = "",
            DateCreated = new DateTime(1900, 01, 01, 00, 00, 00),
            CreatedByID = 0,
            CreatedByDesc = "",
            DateEdited = new DateTime(1900, 01, 01, 00, 00, 00),
            EditedByID = 0,
            EditedByDesc = ""
        };
    }

    #endregion

    #region Property Change Events

    public event PropertyChangedEventHandler PropertyChanged;

    protected void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    #endregion

    #region Commands

    public ICommand EditCommand
    {
        get { return new RelayCommand(EditYear); }
    }

    public ICommand AddCommand
    {
        get { return new RelayCommand(AddYear); }
    }

    #endregion

    #region Functions

    public void EditYear()
    {
        try
        {

            Service1Client service = new Service1Client();

            Year y = new Year();
            y.id = this.id;
            y.Code = this.Code;
            y.Description = this.Description;
            y.DateEdited = DateTime.Now;
            y.EditedByID = (Int32)Application.Current.Resources["UserID"];


            service.EditYear(y);
            MessageBox.Show("Your Year was edited successfully", "Success", MessageBoxButton.OK);
        }
        catch (Exception ex)
        {
            logError le = new logError();
            le.log(ex, "YearViewModel", "EDIT");
        }
    }

    public void AddYear()
    {
        try
        {
            Service1Client service = new Service1Client();

            Year y = new Year();
            y.Code = this.Code;
            y.Description = this.Description;
            y.DateCreated = DateTime.Now;
            y.CreatedByID = (Int32)Application.Current.Resources["UserID"];
            y.DateEdited = this.DateEdited;


            service.AddYear(y);
            MessageBox.Show("Your new Year was entered successfully", "Success", MessageBoxButton.OK);
        }
        catch (Exception ex)
        {
            logError le = new logError();
            le.log(ex, "YearViewModel", "ADD");
        }
    }

    #endregion
}
<Button Content="Save New" 
  DataContext="{StaticResource ResourceKey=NewYear}" 
  Command="{Binding Path=AddCommand}"/>


class RelayCommand : ICommand
{
    Action action;

    public RelayCommand(Action execute)
    {
        action = execute;
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        action();
    }
}