Monday, September 6, 2010

Sitecore Fetch Squad

Automated crawler fetching websites and blogs from Sitecore content

In a previous post we took a look at an early preview version of the DataTransferObjectManager, a library we developed at netzkern to integrate WCF with O/RM tools. Today we will take a look at how nk.DTOM solves the problem of mapping updates from clients to entity objects. Here is how to do this:

// This object represents an update request from an external client.
HouseDTO updateHouseParam = new HouseDTO();
updateHouseParam.ID = “6”;
updateHouseParam.Name = “176 Wallstreet”;
updateHouseParam.Price = “10 Million Dollar”;

// Read the corresponding entity from the data source.
House houseEntity = houseRepository.GetHouse(updateHouseParam.ID);

// Map the properties of the DTO to the entity.
DataTransferObjectManager.Map(updateHouseParam, houseEntity);

// Update the data source.
houseRepository.Save(houseEntity);

Pretty straight forward, right? Remember, by default nk.DTOM only maps value type properties from the source object to the destination object, and that’s exactly what we need here.

Now, let’s consider our entity “houseEntity” contains a property that should be readonly for external clients. In our case, the price of our house is fix and we don’t want it to be changed. In this case, we use the support for selective updates in nk.DTOM. The “Map”-Method contains a fourth parameter that allows us to specify certain properties that we want to preserve in the destination object.

// Create an empty PropertyPathCollection as third parameter, as we do not want to include any references.
PropertyPathCollection includeSourceReferenceProperties = new PropertyPathCollection();

// Create a PropertyPathCollection containing all the PropertyPaths we want nk.DTOM to skip during the mapping, which means preserving the original value.
PropertyPathCollection skipDestinationProperties = new PropertyPathCollection();
skipDestinationProperties.Add(“Price”);

// Call Map() with all four parameters.
DataTransferObjectManager.Map(updateHouseParam, houseEntity, includeSourceReferenceProperties, skipDestinationProperties);

By combining the third and the fourth parameter, nk.DTOM even enables us to update certain values of complete graphs of entity objects. We just need to preserve the original reference to the entity object and maybe some other properties. Take a look:

// Update a referenced DTO on the client-side.
updateHouseParam.Owner.Name = “Some new guy”;

// Create a PropertyPathCollection on the server-side that allows the client to update the referenced entity “Person” in the property “Owner” as well.
PropertyPathCollection includeSourceReferenceProperties = new PropertyPathCollection();
includeSourceReferenceProperties.Add(“Owner”);

// Create a PropertyPathCollection containing all the PropertyPaths we want nk.DTOM to skip, in this case especially the property “Owner” (as the client is not allowed to change the Owner itself and we don’t want nk.DTOM to initialize the property with a new instance) the Owner’s ID.
PropertyPathCollection skipDestinationProperties = new PropertyPathCollection();
skipDestinationProperties.Add(“Price”);
skipDestinationProperties.Add(“Owner.ID”);

// Call Map().
DataTransferObjectManager.Map(updateHouseParam, houseEntity, includeSourceReferenceProperties, skipDestinationProperties);

The service developer can even decide which properties can be changed based on the identity of the caller, for example by relying on WCF authentication features.

We’re currently planning to release netzkern.DataTransferObjectManager as part of a small toolkit on CodePlex. We hope, you’re going to use and like it.

Julius Ganns . netzkern

Comments are closed.