Once you have annotated Types with SossIndexAttribute and stored instances of those types in a NamedCache, you can retrieve all instances of a given type from a StateServer NamedCache, using QueryObjectsT or QueryKeysT depending on whether you want to access the object directly or want access to the objects' keys.
Suppose you have instances of these classes
1[Serializable] 2class Stock 3{ 4 [SossIndex] 5 public string Ticker { get; set; } 6 [SossIndex] 7 public DateTime DelistingDate { get; set; } 8 9 public decimal TotalShares { get; set; } 10 public decimal Price { get; set; } 11} 12 13[Serializable] 14class SmallCapStock : Stock 15{ 16}
1NamedCache cache = CacheFactory.GetCache("Stocks"); 2foreach (Stock s in cache.QueryObjects<Stock>()) { 3 Console.WriteLine(s.Ticker); 4}
The QueryObjectsT method returns all objects in a given NamedCache of the same type as or any subtype of its generic type parameter.
The generic type parameter T in QueryObjects and QueryKeys must correspond to a type which either has properties annotated with the SossIndexAttribute or, the type itself is annotated with SossIndexAttribute. Otherwise, a runtime exception will be thrown indicating that the type requested has no type signature information in the property index.
If you wanted to update objects queried from the StateServer, retrieving the keys of the objects might be more convenient:
1foreach (CachedObjectId<Stock> key in cache.QueryKeys<Stock>()) { 2 Stock s = key.Value; 3 // .. perform some update on the stock 4 cache[key] = s; 5}
Note that QueryKeysT returns instances of the stronly-typed class CachedObjectIdT rather than CachedObjectId. CachedObjectIdT's Value property may then be used to retrieve the object from the NamedCache and cast it to the expected type.
Tip |
---|
Since CachedObjectIdT is a subclass of CachedObjectId it can be used in any NamedCache method that calls for a CachedObjectId reference. |
The return type of QueryObjectsT and QueryKeysT is an IQueryableT. In addition to allowing objects of a given type stored in a NamedCache to be enumerable, QueryObjectsT and QueryKeysT can serve as a a LINQ data source for arbitrary LINQ queries using the LINQ pattern. Combining QueryObjectsT and QueryKeysT with LINQ queries is covered in Using LINQ.