Oct
15
2009

XPO Simplified Criteria Syntax explained (part 1)

User Rating: / 0
PoorBest 

Re-reading my previous blog and after receiving some comments I realised I haven't really explain the essence of what I wanted to explain.

For the purpose of this Blog I will use these Classes for my Examples

TestObjectModel

What is XPO Simplified Criteria Syntax?

XPO Simplified Criteria Syntax is a neat IDE CodeRush "plugin" to XPO, although unofficially supported, DX XPO Evangelist Oliver Sturm and DevExpress do maintain the plugin and is available via Blog posts on the official DevExpress XPO Blog (http://community.devexpress.com/blogs/xpo/default.aspx). The concept of the plugin is to allow people to perform Criteria operations easier by using IntelliSense to specify what field you wish to work with, this includes nested fields. The Fields property is included in the PersistentBase which is the class from which all XPO Objects derive from whether you use (XPBaseObject, XPLiteObject, XPCustomObject or XPObject) (for a comparison of these classes please see the help file http://www.devexpress.com/help/?document=Xpo/CustomDocument3311.htm). The Fields class is usually an empty FieldsClass that does nothing, however it comes alive when the CR_XPOFieldSync is loaded into your IDE. When the Sync occurs, the plugin creates a new FieldsClass that Shadows the PersistentBase property, and populates it with all the persisted properties of your object. Lets take a quick look at an example

Normally your class would look like this

Imports DevExpress.Xpo
Imports DevExpress.Data.Filtering
Imports DevExpress.Xpo.Helpers

Public Class TestObject
    Inherits XPObject

    Private _subject As String
    Public Property Subject() As String
        Get
            Return _subject
        End Get
        Set(ByVal Value As String)
            SetPropertyValue("Subject", _subject, Value)
        End Set
    End Property

    Private _status As TestObjectStatus
    Public Property Status() As TestObjectStatus
        Get
            Return _status
        End Get
        Set(ByVal Value As TestObjectStatus)
            SetPropertyValue("Status", _status, Value)
        End Set
    End Property
End Class

This will still have the PersistentBase Fields property exposed however it will be empty and useless, however run your XPOFieldSync and it will generate a Shadow FieldsClass and Fields property for your Object and generate code for each of your properties

#Region "XPO nested fields class - don't edit manually"
    Public Shadows Class FieldsClass
        Inherits DevExpress.Xpo.XPObject.FieldsClass
        Public Sub New()
            MyBase.New()
        End Sub
        Public Sub New(ByVal propertyName As String)
            MyBase.New(propertyName)
        End Sub
        Public ReadOnly Property Subject() As OperandProperty
            Get
                Return New OperandProperty(GetNestedName("Subject"))
            End Get
        End Property
        Public ReadOnly Property Status() As TestObjectStatus.FieldsClass
            Get
                Return New TestObjectStatus.FieldsClass(GetNestedName("Status"))
            End Get
        End Property
    End Class
    Private Shared _fields As FieldsClass
    Public Shared Shadows ReadOnly Property Fields() As FieldsClass
        Get
            If ReferenceEquals(_fields, Nothing) Then
                _fields = New FieldsClass()
            End If
            Return _fields
        End Get
    End Property
#End Region

Usually when performing Criteria such as obtaining a Collection of all Objects that contain "ThisSubject" in the Subject field you would normally write this code

Dim MyXPObjects As New XPCollection(Of TestObject)(New BinaryOperator("Subject", "ThisSubject"))

Now with the XPO Simplified Criteria Syntax you can change this line to

Dim MyXPObjects As New XPCollection(Of TestObject)(New BinaryOperator(TestObject.Fields.Subject, New OperandValue("ThisSubject"), BinaryOperatorType.Equal))

Why should I useĀ  XPO Simplified Criteria Syntax?

Well, I have found that the "Simplified Criteria Syntax" doesn't really explain the full potential of this little plugin.

  1. You can define your Criteria using IntelliSense
  2. Allows you to have strongly typed references to your fields, this means that should you change the Title field to Name (for some reason) you will receive a Compile error whereever you have used your TestObject.Fields.Title which wouldn't exist before as it would now be TestObject.Fields.Name

No 2 above is the main reason I use the XPO plugin, I use the Fields property for everything in my app such as Fieldname property on GridColumns, Property for DataBinding and also use it within my XPO objects.

I will explain use scenarios a bit later.

How do I start using XPO Simplified Criteria Syntax?

  1. Close Visual Studio
  2. Download the plugin from the Blog
    http://community.devexpress.com/blogs/xpo/default.aspx
  3. Extract the CR_XPOFieldSync.dll into your {My Documents}\DevExpress\IDE Tools\Community\PlugIns folder
  4. Start Visual Studio
  5. Go to the menu DevExpress > Options
    (or I believe CTRL+ALT+SHIFT+O if you are using VS Express and don't have the DevExpress menu)
    DevExpress,Options
  6. On the Left hand Tree List go to IDE > Shortcuts
  7. I would suggest creating your own folder to store your own shortcuts, it makes it easier to find them in future, so click on the New Folder window
    DevExpress,ToolbarThen name it what you like, however set the folder to Root
    DevExpress,NewShortCutFolder
  8. Now ensure your folder is selected and click on the New Shortcut button
    DevExpress,NewShortCut
  9. Setup the options
    DevExpress,ShortcutOptions
    Pick a key combination that you will find easy, I personally use CTRL+1
    Make sure it is Enabled
    Select the SyncXPOClass command
    Then set the use to Editor > Code > In XPO Persistent Class
  10. THEN MAKE SURE YOU CLICK OK

You should now be able to go into your Persisted Class and Press your shortcut (in the example CTRL+1) and you should see the FieldsClass generated at the bottom.

As this post is going to be bigger than ben hur, I am going to break it up so at least information is starting to get out. In the next post I will go through some Use Case Scenario's and why I strongly believe this plugin is actually a big feature of XPO and not just a "Helper" plugin.

Till then

XPOLogoBackground: eXpress Persistent Objects (XPO) is a Object Relation Mapping (ORM) solution provided by DevExpress (DX), XPO is a Object->Database model (ie. You create strongly typed objects to which XPO will handle the persistance to a database store). XPO supports persisting to 16databases out of the box (although you can easily extend their DataStore classes to gain support for your database). See here for more details http://www.devexpress.com/Help/?document=XPO

Add comment

Although I believe your free to say what you want, please don't abuse either myself or other peoples, be constructive.


Security code
Refresh

Should AussieALF stay bald?




Results

Latest Comments

My Twitter

Follow me on twitter