반응형
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;

// Directive for the data model.
using LocalDatabaseSample.Model;


namespace LocalDatabaseSample.ViewModel
{
    public class ToDoViewModel : INotifyPropertyChanged
    {
        // LINQ to SQL data context for the local database.
        private ToDoDataContext toDoDB;

        // Class constructor, create the data context object.
        public ToDoViewModel(string toDoDBConnectionString)
        {
            toDoDB = new ToDoDataContext(toDoDBConnectionString);
        }

        //
        // TODO: Add collections, list, and methods here.
        //

        // Write changes in the data context to the database.
        public void SaveChangesToDB()
        {
            toDoDB.SubmitChanges();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}

이것은 ViewModel의 템플릿입니다. LocalDatabaseSample.Model 네임스페이스에 대한 지시문을 사용하여 이전 단원에서 만든 LINQ to SQL 데이터 모델을 참조합니다. ViewModel은 변경 추적을 위한 INotifyPropertyChanged 인터페이스를 구현합니다.

ViewModel의 핵심 기능은 로컬 데이터베이스에서 작업을 수행하는 것입니다. LINQ to SQL 데이터 컨텍스트인 toDoDB는 ViewModel 전체에서 참조되며 ViewModel의 생성자에서 생성됩니다. SaveChangesToDB 메서드는 일반 용도의 저장 메커니즘을 뷰에 제공합니다.


반응형
Posted by 컴스터
,