小记DevExpress中的数据持久层

本文介绍了DevExpress中的数据持久层实现,主要包括XPObject和XPBaseObject两种数据持久对象的区别,以及如何通过Session进行数据访问的基本设置。同时,还探讨了DevExpress提供的数据操作功能,并附上了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

小记DevExpress中的数据持久层

数据持久对象:

DevExpress.XPO中的数据持久对象主要是XPObjectXPBaseObject,它们的区别是XPBaseObject要自己定义一个主ID,而从XPObject继承的对象默认会在OID为主ID,如果你已经有了一个现成的数据表,而且已经有了主ID,那么应该从XPBaseObject派生你的数据持久对象。

数据访问层:

DevExpress中的数据访问层主要封装在一个Session中,默认使用DevExrepss.Xpo.Session.DefaultsSession,你可以通过设置这个默认的Session来完成数据访问层的一些基本设置,例如数据库链接字符串等。

数据操作:

数据操作内容比较多,一般在XPBaseObject上就完成了添加工作,而查询操作并不在数据持久层上,而在数据层上,也就是在DevExpress.Data中,这里有一些很细致的数据操作,可以对数据进行位操作,也可以进行一些查询分级等高级操作。这也难怪为什么它的一些高级控件,如XpivotGrid, GridControl等在数据操作上都做的非常出色了。当然,这些数据操作控件主要还是利用了一些自己定义的数据穿视图,才使得操作变得很容易。

 

 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

部份参考代码:

1.数据持久类:

None.gif public class T_User : XPBaseObject
ExpandedBlockStart.gifContractedBlock.gif 
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif  
Fields & Properties#region Fields & Properties
InBlock.gif  
protected string _LoginName;
InBlock.gif  
protected int _UserID;
InBlock.gif
InBlock.gif  
public string LoginName
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this._LoginName;}
ExpandedSubBlockStart.gifContractedSubBlock.gif   
setdot.gif{this._LoginName = value;}
ExpandedSubBlockEnd.gif  }

InBlock.gif
InBlock.gif  [Key(
true)]//用特性来映射数据
InBlock.gif
  public int UserID
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif   
getdot.gif{return this._UserID;}
ExpandedSubBlockStart.gifContractedSubBlock.gif   
setdot.gif{this._UserID = value;}
ExpandedSubBlockEnd.gif  }
   
InBlock.gif        
public T_User()
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif            
// Use this constructor when you want to create a new object.
InBlock.gif            
// Place here your initialization code.
InBlock.gif
   this._ParentID = -1;
InBlock.gif   
this._RegDate = DateTime.Now;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
public T_User(Session session) : base(session) dot.gif{
InBlock.gif            
// This constructor is used when an object is loaded from a persistent storage.
InBlock.gif            
// Do not place any code here.
ExpandedSubBlockEnd.gif
        }

ExpandedBlockEnd.gif    }

None.gif


2.设置默认链接

None.gifXpoDefault.ConnectionString = MSSqlConnectionProvider.GetConnectionString("Server","UserID","Pwd","DB");


当然,它还有其它的链接驱动可用。

3.测试:

None.gif            try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                WebAD.Controls.T_User m_user 
= new WebAD.Controls.T_User();
InBlock.gif                m_user.LoginName 
= "apple";
InBlock.gif                m_user.Password 
= "test";
InBlock.gif                m_user.Save();                
ExpandedBlockEnd.gif            }

None.gif            
catch(Exception ex)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif{
InBlock.gif                System.Diagnostics.Debug.WriteLine(ex.Message);
ExpandedBlockEnd.gif            }


从整个结构上看,DevExpress的数据持久层做的应该还算不错的,一方面可以大量简化开发者的代码编写工作,而数据持久对象的代码又是机械化的,应该可以写工具来完成。

简单小记!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值