T
Chili.Opf3 Send comments on this topic.
Generic ObjectSearcher Class
See Also  Members   Example 
Chili.Opf3 Namespace : Generic ObjectSearcher Class




Allows to search in the storage for persistent objects of a certain type.

Object Model


Syntax

Visual Basic (Declaration) 
Public Class ObjectSearcher(Of T) 
Visual Basic (Usage)Copy Code
Dim instance As ObjectSearcher(Of T)
C# 
public class ObjectSearcher<T> 
Managed Extensions for C++ 
public __gc class ObjectSearcher<T> 
C++/CLI 
generic<typename T>
public ref class ObjectSearcher 

Type Parameters

T

Example

The following example shows how to get an ObjectSearcher that searches for User objects. The second example shows how to derive from ObjectSearcher to create an own searcher class.
C#Copy Code
             [Persistent("USER")] 
public class User 

                    private string _name = null; 
 
                    [Field("NAME")] 
                    public string Name 
                    { 
                        get { return _name; } 
                        set { _name = value; } 
                    } 
 
                    // ... Other properties and fields. 

 
// ... Other code 
 
// Get the ObjectSearcher for User. 
ObjectSearcher<User> searcher = context.GetObjectSearcher<User>(); 
// Find all user in the storage. 
ObjectSet<User> objectSet = searcher.FindAll(); 
 
// Find only those with a given name by using conditions and sorts them. 
objectSet = searcher.FindAll("Name Like {0} SortBy Name Asc", "%mith%"); 
                    
C#Copy Code
// UserSearcher is a specialized class that searches only for user objects. 
public class UserSearcher : ObjectSearcher<User> 

                    public UserSearcher(ObjectContext context) : base(context) 
                    { 
                    } 
 
                    // Returns all user with at least one call. 
                    public ObjectSet<User> GetWithCalls() 
                    { 
                        // In the example a SqlQuery is used, but you could also use an ObjectQuery. 
                        SqlQuery query = new SqlQuery("select distinct u.* from [USER] u, CALLS c where " +  
                            "u.ID = c.USER_ID;", null); 
                        return Context.GetObjectSet<User>(query); 
                    } 

 
// ... Other code 
 
// Creates a new ObjectContext that uses an MsSql Server as storage. 
ObjectContext context = new ObjectContext(new MsSqlStorage("sa", "", "localhost", "application")); 
// Set the MD5 Concurrency Manager. 
context.ConcurrencyManager = new Md5ConcurrencyManager(); 
 
// We can't use directly the methods of the <see cref="T:Chili.Opf3.ObjectContext">ObjectContext</see> 
// to get the UserSearcher. We have to create an instance and pass the current context. 
UserSearcher searcher = new UserSearcher(context); 
ObjectSet<User> os = searcher.GetWithCalls();

Remarks

This class is specialized to load objects from the storage. The implementation is gerneric: once created it allows you to load only objects of the generic type. Usually the generic ObjectSearcher class is created by using the GetObjectSearcher function of the ObjectContext. ObjectSearcher contains a few methods (for example: FindAll) to search for objects.

FindAll returns an instance of the ObjectSet class. This class contains the result of the search. FindAll has been overloaded to return also a class that implements the IList interface with the resultset.

The ObjectSearcher should be also used to encapsulate complex queries. If you have a complex SQL statement you should derive from ObjectSearcher and create your own "Searcher" class. To create your own "Searcher" classes derive directly from ObjectSearcher and extend it with your own methods (For further information see the example below).

Inheritance Hierarchy

System.Object
   Chili.Opf3.ObjectSearcher

Requirements

Namespace: Chili.Opf3

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Chili.Opf3 (in Chili.Opf3.dll)

See Also