Julius's profileJulius Ganns . netzkernPhotosBlogNetwork Tools Help

Blog


    March 23

    Performing Updates with the DataTransferObjectManager

    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.

    March 01

    Top 10 Must-Read Books for Personal Success

    I love reading books. Usually that means one, sometimes two or three books per week. In the last four years I extended my personal library to several self-development and management books and I have to say, I’m glad that I read most of them. Below you can find my personal top ten list in those two categories:

    Getting Things Done
    The 7 Habits of Highly Effective People
    Brain Rules
    Slowing Down to the Speed of Life
    How To Be Brilliant
    Behind Closed Doors
    Good to Great
    Speed Reading for Professionals
    First Things First
    Mind Performance Hacks

    Additionally I own several other books that I haven’t read yet but that might make it to this list, maybe even replacing one or two of the ones listed above. Here are those I’m eager to read:

    Think And Grow Rich
    How To Win Friends And Influence Others
    Your Money or Your Life
    The Power Of Less

    For a complete list of all my books, please visit my website at www.juliusganns.com.