Code Samples

Query a Repository

The basics is to query data from an existing repository. You create a query object from the URL string. The system is designed to use the URL as its parameter set. However this is optional you can build the query object manually if desired. The query object contains all information to filter, sort and page the information coming back.
//Create a query object directly from the page URL
var query = new ListingQuery(this.Request.Url.PathAndQuery);

//Create a WCF service connection
using (var factory = RepositoryConnection.GetFactory())
{
	var service = factory.CreateChannel();

	//Query data with the query object passing in the service reference
	var results = RepositoryConnection.QueryData(query, service);

	//Loop though all dimensions and show the name and count of each
	//In the Autos sample these are Make, Model, Price, Year, etc
	foreach (var dimension in results.DimensionList)
	{
		Debug.WriteLine("Dimension: " + dimension.Name);
		Debug.WriteLine("Item Count: " + dimension.RefinementList.Count);

		//Loop through the refinement values of each dimension
		//Print out the value and the matching count
		//In the autos example for Make dimension these would be Ford, Audi, etc
		foreach (var refinement in dimension.RefinementList)
		{
			Debug.WriteLine("Refinement: " + refinement.FieldValue);
			Debug.WriteLine("Count: " + refinement.Count);
		}

	}
}

Add / Update a Repository

You can add a single item or a list of items. There is an overload to the UpdateData method for both cases.
using (var factory = RepositoryConnection.GetFactory())
{
    var service = factory.CreateChannel();
    RepositoryConnection.UpdateData(item, service);
}

Delete from a Repository

If you have a reference to an existing object, you can delete it from the repository by calling the DeleteData method. Much like Entity Framework, you must have a reference to the object before you can remove it.
using (var factory = RepositoryConnection.GetFactory())
{
    var service = factory.CreateChannel();
    RepositoryConnection.DeleteData(item, service);
}

Clear entire Repository

You can clear all items from a repository simply by issue the clear command.
using (var factory = RepositoryConnection.GetFactory())
{
    var service = factory.CreateChannel();
    RepositoryConnection.Clear(service);
}

Last edited May 6, 2013 at 2:50 PM by codetools, version 6

Comments

No comments yet.