XPO 第三方控件学习(DevExpress Persistent Object )系列 其他(session,事务,有效性检查...

五、Session :

管理数据库的连接信息。有一个默认的连接:MS Access OLEDB provider。如果使用它,在程序中就不必自己初始化Session的实例。但如果想使用自己的连接,两种办法:一是更改默认连接的连接信息,二是自己创建,但在持久类构建时必须引用它。还是第一种简单一点。除非应用程序要考虑连接两个数据库

六、对二进制大对象字段的存取

    使用延迟装载(Delayed Loading):

       这里必须指定一个私有的XPDelayedProperty类型的属性,并且加上Attribute,设计上有点繁琐。

Public Class Customer

    Inherits XPObject

    '...

    Private document As New XPDelayedProperty

    <Delayed("document")> _

       Public Property Attachment() As <Byte>()

        Get

            Return CType(document.Value, <Byte>())

        End Get

        Set(ByVal Value As <Byte>())

            document.Value = value

        End Set

    End Property

End Class 'Customer

七、事务的支持:

事务在数据库程序内是不可或缺的。

显然该功能是由Session提供的。

Dim account As New Account

Session.DefaultSession.BeginTransaction()

Try

   account.Amount = amount

   account.Save()

   Session.DefaultSession.CommitTransaction()

Catch e As Exception

   Session.DefaultSession.RollbackTransaction()

   account.Reload()

End Try

注意在Exception发生时,使用了Reload()。

八、保存之前的数据有效性检查:

Class Account

    Inherits XPObject

    Public Amount As Double = DefaultAmount

 

    Protected Overrides Sub BeforeSave()

        MyBase.BeforeSave()

        If Not IsDeleted Then

            If Amount < 0 Then

                Throw New Exception("Negative amount")

            End If

        End If

    End Sub 'BeforeSave

End Class 'Account

注意先判断了IsDeleted。

九、并发操作:

        提供了检查对象在更改之前是否已经变化的检查机制。在以前我们必须自己写代码去检查,现在这也提供了。

十、数据分页机制

       对大数据量,我们一般都不是一次提取的,而是分批提取,从而减少内存使用和加快提取速度。XPO提供了直接支持。但是它没有使用XPCollection,而是使用了另外一个类XPCusor。使用上和XPCollection差不多,也支持条件对象,但就是多了个分页支持。

        这个设计思想令人纳闷,为什么不合二为一?

十一、对结构的持久化支持:

Public Structure Point

    <Persistent("Abscissa")> Public X As Integer

    Public Y As Integer

End Structure 'Point

 

Public Class Shape

    Inherits XPObject

    Public Name As String = ""

    <Persistent("Location")> Public Position As Point

End Class 'Shape

      注意使用了Attribute,结构才能持久化。

作者:johnny 出处:http://www.cnblogs.com/sunjunlin 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/sunjunlin/p/1678669.html