<3>进入开发
如上可以看出Repid第一个版本只是实现WP7中数据基本操作, 只能说基本满足需求.如下我会演示基于Repid数据库基本的CRUD操作.在进入开发前需要在Rapid Repository - Windows 7 Phone Database上下载引用RepidRepository.DLL.
新建一个Windows Phone 7 Application 命名RepidDBLocalDemo: 添加RepidRepository.DLL引用:
建立测试页面MainPage.Xaml:
MainPage页面保存一个实体类EntityClass 货物Product信息到Repid数据库中,定义好页面后,如下需要定义 Product实体:
1: using RapidRepository;
2:
3: namespace RepidDBLocalDemo.EntityModel
4: {
5: ///
6: /// Define the Product Model to Store
7: /// Author:chenkai Date:2010年11月16日18:10:47
8: ///
9: public class Product :IRapidEntity
10: {
11: public Guid Id { get; set; }
12: public string ProductName { get; set; }
13: public string ProductCatory { get; set; }
14: public ProductPackage ProductSpec { get; set; }
15: public DateTime CreateDate { get; set; }
16:
17: }
18:
19: public class ProductPackage
20: {
21: public string PId { get; set; }
22: public string PackageSpec { get; set; }
23: }
24: }
实体类Product定义中发现继承了一个接口IRapidEntity, 这个IRapidEntity我们查看一下它的定义:
1: namespace RapidRepository
2: {
3: public interface IRapidEntity
4: {
5: Guid Id { get; set; }
6: }
7: }
you see! 其实继承IRapidEntity接口目的就是为了给实体类强制加了一个Guid属性.这也不难理解, Guid[全球优异标识符]值就是这个唯一的标识码来标识实体类. 这主要是为了当我们Repid数据库中存储多个实体通过Guid来唯一标识实体类. 在Product用属性Id来实现用户自定义Guid.
Product实体类把ProductPackage规格类作为ProductSpec属性. 这就是初步复杂类型实体
定义完实体类Product后,对于实体类存储操作我们需要定义一个对应的ProductRepository操作类:
1: using RapidRepository;
2: using RapidRepository.Context;
3: using RepidDBLocalDemo.EntityModel;
4:
5: namespace RepidDBLocalDemo.EntityModel
6: {
7: ///
8: /// Define the ProductRepository
9: /// Author:chenkai Date:2010年11月16日18:45:19
10: ///
11: public class ProductRepository :RapidRepository
12: {
14: ///
15: /// Override Base Add new Product EntityModel Method
16: /// Author:chenkai Date:2010年11月17日10:21:51
17: ///
18: public override Product Add(Product entity)
19: {
20: var newproduct = base.Add(entity);
21: RapidContext.CurrentContext.SaveChanges();
22: return newproduct;
23: }
24:
26: ///
27: /// use GUID Delete EntityModel Class
28: ///
29: /// Entity Guid
30: public override void Delete(Guid id)
31: {
32: base.Delete(id);
33: RapidContext.CurrentContext.SaveChanges();
34: }
36:
37: ///
38: /// Update the EntityModel
39: ///
40: public override Product Update(Product entity)
41: {
42: var updateproduct = base.Update(entity);
43: RapidContext.CurrentContext.SaveChanges();
44: return updateproduct;
45: }
46:
47: }
48: }
ProductRepository类是对实体Product进行数据库操作时使用类.ProductRepository 继承了RapidRepository类,
其实RepidRespository是定义目前Repid数据所有CRUD操作:
1: public class RapidRepository where TEntity : global::RapidRepository.IRapidEntity
2: {
3: public RapidRepository();
4:
5: public virtual TEntity Add(TEntity entity);
6: public virtual void Delete(Guid id);
7: public virtual void Delete(TEntity entity);
8: public virtual IList GetAll();
9: public virtual TEntity GetById(Guid id);
10: public virtual TEntity Update(TEntity entity);
11: }
对于Repid数据库所有对源数据发生变动需要使用repidContent.CurrentContent.SaveChange()方法强制保存.才能生效.
MainPage添加对Repid数据库相关引用:
1: //Add References
2: using RapidRepository;
3: using RapidRepository.Database;
4: using RapidRepository.Context;
5: using RepidDBLocalDemo.EntityModel;
点击Save按钮触发保存一个Product实体到Repid数据库中并确认是否保存成功:
1: private void Saveinfor_Click(object sender, RoutedEventArgs e)
2: {
3: //Save Product Entity
4: try
5: {
6: if (!string.IsNullOrEmpty(this.Product_Id.Text) && !string.IsNullOrEmpty(this.Product_Name.Text)
7: && !string.IsNullOrEmpty(this.Product_catory.Text))
8: {
9: Product newproduct = new Product
10: {
11: Id=new Guid(),
12: ProductName=this.Product_Name.Text,
13: ProductCatory=this.Product_catory.Text,
14: ProductSpec=new ProductPackage
15: { PId=this.Product_Id.Text, PackageSpec="ProductSpec_Value"},
16: CreateDate=DateTime.Now
17: };
18:
19: //Save The Product Entity
20: ProductRepository productrep = new ProductRepository();
21: Product saveproduct=productrep.Add(newproduct);
22:
24: //query the Entity Confirm Entity is Save?
25: Product queryproduct = productrep.GetById(saveproduct.Id);
26:
27: if (queryproduct != null)
28: MessageBox.Show("Save Sucess!\r\n产品编号:"
29: +queryproduct.ProductSpec.PId+"\r\n产品名称:"
30: +queryproduct.ProductName+"\r\n产品分类:"
31: +queryproduct.ProductCatory+"\r\n创建时间:"
32: +queryproduct.CreateDate.ToString());
33: else
34: MessageBox.Show("Save Fail!");
35: }
36: }
37: catch (Exception se)
38: {
39: MessageBox.Show("sorry you get Exception:" + se.Message);
40: }
41: }
输入数据后保存效果:
上面保存验证是先把产品信息保存Repid数据库中,然后通过查询方式获取到已经保存的Product信息,以确认信息正确保存,查询方式目前在Repid1.0中只有两种方式GetById()和GetAll():
1: ///
2: /// query a Entity by Guid
3: ///
4: public override Product GetById(Guid id)
5: {
6: return base.GetById(id);
7: }
9: ///
10: /// Get All Entity Model
11: ///
12: /// List of Sotre Model
13: public override IList GetAll()
14: {
15: return base.GetAll();
16: }
很明显GetById通过Guid方式唯一查询一个Entity实体.这里你明白了为何要在Product中继承接口的目的. GetAll方式则获取repid数据库目前所有实体数据.返回的是一个实现接口IList集合.
其他简单的CRUD操作基本雷同, 不在赘述.注意有一点就是当对Repid数据库发生一定操作改变数据源是一定不要忘了采用PepidContent.CurrentContent.SaveChanges();方法保存修改.