您的位置:首页 > 数据库

Windows Phone Mango 本地数据库数据访问层

2011-11-15 10:30 351 查看
Domain文件夹

User类

public class User : INotifyPropertyChanged, INotifyPropertyChanging
{
private int _index;
[Column(IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true, DbType = "INT NOT NULL Identity", AutoSync = AutoSync.OnInsert)]
public int Index
{
get
{
return _index;
}
set
{
if (_index != value)
{
NotifyPropertyChanging("Index");
_index = value;
NotifyPropertyChanged("Index");
}

}
}

private string _name;
[Column]
public string Name
{
get
{
return _name;
}
set
{
NotifyPropertyChanging("Name");
_name = value;
NotifyPropertyChanged("Name");
}
}

private String _gen;
[Column]
public String Gen
{
get
{
return _gen;
}
set
{
NotifyPropertyChanging("Gen");
_gen = value;
NotifyPropertyChanged("Gen");
}
}

private int _age;
[Column]
public int Age
{
get
{
return _age;
}
set
{
NotifyPropertyChanging("Age");
_age = value;
NotifyPropertyChanged("Age");
}
}

//private BitmapImage _thumbnail;
//[Column]
//public BitmapImage Thumbnail
//{
//    get
//    {
//        return _thumbnail;
//    }
//    set
//    {
//        NotifyPropertyChanging("Thumbnail");
//        _thumbnail = value;
//        NotifyPropertyChanged("Thumbnail");
//    }
//}

#region INotifyPropertyChanged Members

public event PropertyChangedEventHandler PropertyChanged;

// Used to notify the page that a data context property changed
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}

#endregion

#region INotifyPropertyChanging Members

public event PropertyChangingEventHandler PropertyChanging;

// Used to notify the data context that a data context property is about to change
private void NotifyPropertyChanging(string propertyName)
{
if (PropertyChanging != null)
{
PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
}
}

#endregion
}

Data文件夹

DatabaseContext类

public class DatabaseContext : DataContext
{
public const string ConnectionStr = "Data Source=isostore:/MyDB12.sdf";
public Table<User> Users;
public DatabaseContext()
: base(ConnectionStr)
{
if (!DatabaseExists())
CreateDatabase();
}
}


Repository类

public abstract class Repository<T>
{
protected DatabaseContext ActiveContext = new DatabaseContext();

public Repository()
{
ActiveContext = new DatabaseContext();
}

protected abstract void AddObject(T item);
protected abstract void DeleteObject(T item);
protected abstract ObservableCollection<T> GetObjectQuery();
protected abstract void UpdateObject(T item);

public void Add(T item)
{
AddObject(item);
Submit();
}

public void Delete(T item)
{
DeleteObject(item);
Submit();
}

public void Update(T item)
{
UpdateObject(item);
Submit();
}

public ObservableCollection<T> GetAll()
{
return GetObjectQuery();
}

public void Submit()
{
try
{
this.ActiveContext.SubmitChanges();
}
catch (Exception ex)
{
throw ex;
}
}
}


UserReporsitory类

public class UserReporsitory:Repository<User>
{
protected override void AddObject(User item)
{
this.ActiveContext.Users.InsertOnSubmit(item);
}

protected override void DeleteObject(User item)
{
this.ActiveContext.Users.DeleteOnSubmit(item);
}

protected override void UpdateObject(User item)
{
User subject = ActiveContext.Users.Single(c=>c.Index == item.Index);
subject.Age = item.Age;
subject.Gen = item.Gen;
subject.Name = item.Name;

//this.GetObjectQuery().Single(c => c.Guid == item.Guid);
//this.ActiveContext.DeptStds.ApplyCurrentValues(item);

}

protected override ObservableCollection<User> GetObjectQuery()
{
var items = new ObservableCollection<User>();
var enumerator = ActiveContext.Users.GetEnumerator();
while (enumerator.MoveNext())
items.Add(enumerator.Current);
return items;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: