October 25, 2016
Hot Topics:

WinFS Basics: Working with a Data Store

  • January 25, 2006
  • By Sahil Malik
  • Send Email »
  • More Articles »

Working with WinFS Data Using the WinFS API

The WinFS .NET object-based API consists of various classes that can be divided into two main groups: the data, and what lets you work with the data. Let's begin with data. Figure 1 shows a data class in WinFS, also known as Entity class, which is one among four others: Item, ItemFragment, ItemExtension, and Link.

Click here for a larger image.

Figure 1. Organization of Major WinFS Data-Related Classes

An Item is what you will be dealing with most frequently. This is the semistructured object structure that you identify with when you think of unique entities in your data. So you can think of a document as an item inherited from the Item class. Similarly, a message would be an item. A folder, a contact, an attachment, a video file, an audio file, etc. would also be items.

An ItemFragment enables the declaration of large collections in item types. For instance, a message may have a number of participants, each represented by an instance of Participant. In this scenario, Participants would be a property on the Message Item of data type ItemFragmentCollection<Participant>, where Participant would inherit from ItemFragment. Also, since ItemFragment is yet another entity that simply lives as data in a WinFS store, it also inherits from Entity.

An EntityExtension is what would allow you to extend built-in items by adding further properties as necessary. As you may expect, this property is defined in the Item abstract class, so every WinFS Item has this property exposed as the Extension property, which is of data type ItemExtensionCollection. This is what lets you "tag" your data with words. Examine keyword assignments to your objects once more to set things in place:

ItemKeywords keywords = new ItemKeywords();
keywords.Keywords.Add(new Keyword("Diamonds"));

Finally, Link is also data, but it is data that defines links between two or more distinct existing item types. So a Message may have attachments that are documents.

Classes for Working with WinFS Data Stores

Now that you have data squared away, you also need classes to work with the WinFS data stores. Many WinFS framework classes will help you work with WinFS data stores. Besides the two namespaces dedicated exclusively to WinFS Sync and WinFS Rules (namely, System.Storage.Sync and System.Storage.Rules), you will frequently deal with StorageContext, StorageSearcher, and StorageWatcher classes.

StorageContext is what allows you to connect to a WinFS data store. StorageSearcher performs searches and holds a result set. You already saw usage of this class in the following data search code:

StorageSearcher<Document> docs = 
typeof(ItemKeywords), "Diamonds"); foreach (Document doc in docs) { Console.WriteLine("Doc's Title = {0}", doc.Title); }

As you can see, the WinFS API supports an object path query language that allows you to specify filter criteria in your queries.

StorageWatcher monitors for various events or data change notifications on a WinFS data store.

Putting all these together, you could easily add a folder called "WinFS article folder" and add one item in it called Maliks, which is of type System.Storage.Contacts.Contact, by writing the following code:

using (WinFSData wfsDat = new WinFSData())
   Folder newFolder = new Folder();
   newFolder.DisplayName = "WinFS article folder";
   newFolder.NamespaceName = "WinFS article folder";
   newFolder.Container = wfsDat.GetRootItem();
   Contact sahil = new Contact(newFolder);
   sahil.DisplayName = "Sahil Malik";
   sahil.NamespaceName = "Maliks";
   ContactCard sahilCard = new ContactCard();
      new System.Storage.Core.EAddress("smalik@nospam.com"));

Figure 2 shows the results of the above code.

Figure 2. Adding a Folder and an Item

Now that you have a data store populated, you could query it using the following code:

using (WinFSData wfsDat = 
   new WinFSData(@"\\HOMEPCOLD\DefaultStore\WinFS article folder"))
   StorageSearcher<Contact> contacts = 
   foreach (Contact c in contacts)
      Console.WriteLine("Display Name: " + c.DisplayName);

This code should produce the output in Figure 3.

Click here for a larger image.

Figure 3. Query Your Data Store

Interestingly, if you accessed the file backend with the Win32 API using the following VBScript:

filePath="\\HOMEPCOLD\DefaultStore\WinFS article folder"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFolder(filePath)
msgbox f.Name

It should produce the following output:

If you instead browsed to a text document stored in a WinFS data store, you would be able to read its contents using a simple text stream—just as you would read off of your C:\ drive.

The Basics of WinFS

WinFS is a large topic that cannot be covered in a single article, but you now know the basics of working with a data store. WinFS has other exciting facets, such as being able to author your own types, WinFS rules, and WinFS Sync, which make WinFS one of the most exciting offers on the horizon.

About the Author

Sahil Malik has worked for a number of top-notch clients in Microsoft technologies ranging from DOS to .NET. He is the author of Pro ADO.NET 2.0 and co-author of Pro ADO.NET with VB.NET 1.1. Sahil is currently also working on a multimedia series on ADO.NET 2.0 for Keystone Learning. For his community involvement, contributions, and speaking, he has also been awarded the Microsoft MVP award.


Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel