Skip to content
Chris/0 edited this page Nov 6, 2018 · 2 revisions

Ignore

If a value is used for link creation it's unlikely to be useful to a client in the body of the value.

Please refer to this sample document for examples for the remainder of the documentation:

/// <summary>Defines HAL+JSON transformations for the application.</summary>
sealed class HalProfile
    : IHalProfile
{
    static readonly Uri _pool = new Uri("https://relations.fen.cimpress.io/pool", Absolute);
    static readonly Uri _events = new Uri("https://relations.fen.cimpress.io/events", Absolute);
    static readonly Uri _orderedItem = new Uri("https://relations.fen.cimpress.io/ordered-item", Absolute);
    static readonly Uri _asset = new Uri("https://relations.fen.cimpress.io/asset", Absolute);

    /// <inheritdoc/>
    void IHalProfile.OnTransformationMapCreating(ITransformationMap transformationMap)
    {
        transformationMap
            .Self<PrintJob>(pj => Route("GetByPrintJobId", new { pj.Id }))
            .LinkAndIgnore(_pool, pj => pj.Pool)
            .Link(_events, pj => Route("GetEventsForPrintJob", new { pj.Id }))
            .Embed(_orderedItem, pj => pj.Items, pj => Route("GetPrintJobItem", new { pj.Id }))
            .Link(_asset, pj => pj.Assets, a => new Constant(a.Uri) { Title = a.Role })
            .Ignore(pj => pj.Id, pj => pj.Watchers, pj => pj.Events, pj => pj.Assets);

        transformationMap
            .Self<PrintJobEventCollection, PrintJobEvent>(pjec => Route("GetEventsForPrintJob", new { id = pjec.PrintJobId }))
            .LinkElements("item", pje => Route("GetEvent", new { e.Id, e.PrintJobId }))
            .Link("up", pjec => Route("GetByPrintJobId", new { id = pjec.PrintJobId }))
            .Hoist(pjec => pjec.Count);

        transformationMap
            .Self<PrintJobEvent>(pje => Route("GetEvent", new { pje.Id, pje.PrintJobId }))
            .Link("collection", pje => Route("GetEventsForPrintJob", new { id = pje.PrintJobId }))
            .Link("up", e => Route("GetByPrintJobId", new { id = e.PrintJobId }))
            .Ignore(pj => pj.Id, pj => pj.PrintJobId);

        transformationMap
            .Self<ItemCollection, Item>(ic => Route("GetItemsForPrintJob", new { id = ic.PrintJobId }))
            .LinkElements("item", i => i.ItemUri)
            .Link("up", ic => Route("GetByPrintJobId", new { id = ic.PrintJobId }))
            .Hoist(ic => ic.Count);

        transformationMap
            .Self<Item>(i => i.ItemUri)
            .Link(_asset, i => i.Assets, a => new Constant(a.Uri) { Title = a.Role })
            .Link("collection", i => Route("GetItemsForPrintJob", new { id = i.PrintJobId }))
            .Link("up", i => Route("GetByPrintJobId", new { id = i.PrintJobId }))
            .Ignore(pj => pj.ItemUri, pj => pj.PrintJobId, pj => pj.Assets);
    }
}

This can be seen in pj.Id on line 14 of the example.

.Self<PrintJob>(pj => Route("GetByPrintJobId", new { pj.Id }))

Once a client has requested application/hal+json, they should no longer have a need to glue together baseURIs and IDs (or whatever) in order to synthesize a URI to a resource. The method Ignore will remove the selected property (key and value) from the serialization. Many properties can be specified in a single call to Ignore, as on line 19 of the example.

.Ignore(pj => pj.Id, pj => pj.Watchers, pj => pj.Events, pj => pj.Assets);

In short, this is similar to JSON.NET's JsonIgnoreAttribute, but specific to the HAL+JSON serialization without coupling the data type to its serialized representations.

Clone this wiki locally