ObjectSearcher Guidelines

This guideline shows how to create your own class that inherits from ObjectSearcher. When an application needs complex queries they should be encapsulated in instances of a class deriving from ObjectSearcher. This ensures that the queries are not spead over the whole application and allows to execute different queries depending on the storage type.

The following sample shows how to derive from ObjectSearcher.

C#
// This class allows to search for persistent objects of type "User".
public class UserSearcher : ObjectSearcher<User>
{
    // Creates a new instance of the UserSearcher Class. The base constructor takes 
    // an instance of the ObjectContext.
    public UserSearcher(ObjectContext context) : base(context)
    {
    }
}

VB.NET
' This class allows to search for persistent objects of type "User".
Public Class UserSearcher 
    Inherits ObjectSearcher(Of User)

    ' Creates a new instance of the UserSearcher Class. The base constructor takes 
    ' an instance of the ObjectContext.
    Public Sub New(ByVal context As ObjectContext)
        MyBase.New(context)

    End Sub

End Class

The ObjectSearcher class has a constructor that takes one argument. The argument is an instance of the ObjectContext. When implementing an own searcher class that constructor needs to be called (as done in the previous code sample).

The next step takes us to implement a method to get all user with a given name. The query in this example is very simple and only intended for demonstration purposes.

C#
// This class allows to search for persistent objects of type "User".
public class UserSearcher : ObjectSearcher<User>
{
    // Creates a new instance of the UserSearcher Class. The base constructor takes 
    // an instance of the ObjectContext.
    public UserSearcher(ObjectContext context) : base(context)
    {
    }
    
    // Returns all user by a given name.
    public ObjectSet<User> GetUserByName(string name)
    {
        SqlQuery query = null;
        
        // Check the type of the storage to compile a storage dependent query.
        if (this.Context.Storage.GetType() == typeof(AccessStorage))
            query = new SqlQuery("select * from [USER] where [NAME] = {0}", name);
        else if (this.Context.Storage.GetType() == typeof(MsSqlStorage))
            query = new SqlQuery("select u.* from USER u where NAME = {0}", name);
        else
        {
            // Throw an exception if the storage is not supported.
            throw new NotSupportedException("Storage type not supported.");
        }
        
        // Returns the ObjectSet with the user matching the query.
        return this.Context.GetObjectSet<User>(query);
    }
}

VB.NET
' This class allows to search for persistent objects of type "User".
Public Class UserSearcher
    Inherits ObjectSearcher(Of User)

    ' Creates a new instance of the UserSearcher Class. The base constructor takes 
    ' an instance of the ObjectContext.
    Public Sub New(ByVal context As ObjectContext)
        MyBase.New(context)

    End Sub

    ' Returns all user by a given name.
    Public Function GetUserByName(ByVal name As String) As ObjectSet(Of User)
        Dim sqlQuery

        ' Check the type of the storage to compile a storage dependent query.
        If TypeOf (Me.Context.Storage) Is AccessStorage Then
            sqlQuery = New SqlQuery("select * from [USER] where [NAME] = {0}", name)
        ElseIf TypeOf (Me.Context.Storage) Is MsSqlStorage Then
            sqlQuery = New SqlQuery("select u.* from USER u where NAME = {0}", name)
        Else
            ' Throw an exception if the storage is not supported.
            Throw New NotSupportedException("Storage type not supported.")
        End If

        ' Returns the ObjectSet with the user matching the query.
        Return Me.Context.GetObjectSet(Of User)(sqlQuery)
    End Function

End Class

The example shows how to detect the current storage and create a query for that storage. If the storage is not supported an exception is thrown. The result is an ObjectSet with all user matching the query.

Last edited Nov 24, 2009 at 5:15 PM by davidbrown, version 1

Comments

No comments yet.