Typeless and Partial-Object Operations

Most methods on the SimolClient class fit into one of the following categories:
  • Those requiring a generic type parameter and operating on entire objects
  • Those requiring a generic type parameter and operating on partial object property sets held in a PropertyValues collection
  • Those requiring an ItemMapping and operating on arbitrary PropertyValues collections
This section focuses on the last two method styles, which we'll refer to as partial-object and typeless operations, respectively.

Typeless and partial-object operations require a bit more code to use but are necessary for the following reasons:
  • Performance - If your application needs to operate on just one or two attributes for a large number of items you can avoid reading or updating the attributes that aren't needed.
  • Infrastructure support - Tools or application infrastructure that cannot reference persistent types at compile time can use the typeless operations to implement completely dynamic interactions with SimpleDB using arbitrary types and mappings
  • Domain partitioning - If your application needs to partition data for the same item type across multiple SimpleDB domains you can use typeless operations to change domain names on the fly, rather than being restricted to one static domain per persistent type.
  • Data conversion - If you need to reformat existing data in SimpleDB you can use typeless operations to convert the data. Otherwise you would need to create two versions of your persistent type to define the before and after format.
  • Decoupling your data model from Simol - If you want to keep your data model classes completely separate from your persistence mechanism you should exclusively use typeless operations to interact with Simol. This will let you use Simol's many features without the need to mark your data classes with Simol mapping attributes.

The example below demonstrates getting a single property value using all three method styles:

    var employeeId = new Guid("6c0a37b4-9c59-49ce-95af-d01a66f9605d");
    DateTime hireDate;

    // Load the entire employee object and get the HireDate property
    Employee e = simol.Get<Employee>(employeeId);
    hireDate = e.HireDate;

    // Load just the HireDate using a partial-object operation
    PropertyValues values1 = simol.GetAttributes<Employee>(employeeId, "HireDate");
    hireDate = (DateTime)values1["HireDate"];

    // Load just the HireDate using a typeless operation
    AttributeMapping idMapping = AttributeMapping.Create("Id", typeof (Guid));
    AttributeMapping hireDateMapping = AttributeMapping.Create("HireDate", typeof (DateTime));
    ItemMapping mapping = ItemMapping.Create("Employee", idMapping);
    mapping.AttributeMappings.Add(hireDateMapping);

    PropertyValues values2 = simol.GetAttributes(mapping, employeeId, "HireDate");
    hireDate = (DateTime)values2["HireDate"];

For applications that use all three styles the Simol API includes several utility methods for creating ItemMappings and PropertyValues collections from your data objects. The example below demonstrates use of these helper methods to get an Employee hire date using a typeless operation, but with significantly less code than in previous example:

    var employeeId = new Guid("6c0a37b4-9c59-49ce-95af-d01a66f9605d");
    ItemMapping mapping = ItemMapping.Create(typeof (Employee));
    PropertyValues values = simol.GetAttributes(mapping, employeeId, "HireDate");

We can also create a complete Employee instance from a PropertyValues collection with just one more line of code:

    var employeeId = new Guid("6c0a37b4-9c59-49ce-95af-d01a66f9605d");
    ItemMapping mapping = ItemMapping.Create(typeof(Employee));
    PropertyValues values = simol.GetAttributes(mapping, employeeId);
    Employee e = (Employee) PropertyValues.CreateItem(typeof (Employee), values);

Last edited May 24, 2011 at 3:25 AM by ashleytate, version 13

Comments

No comments yet.