Argolis icon indicating copy to clipboard operation
Argolis copied to clipboard

Provide tooling for working with URI Templates

Open tpluscode opened this issue 9 years ago • 0 comments

Currently there is no concrete way to simplify how URI Templates are used throughout a Hydra application.

First, they are used as create resource identifiers for serialized objects. Currently the only way is to manually use a URI Template to construct the identifier

public class MyModel 
{
    public Uri Id { get; set; }
}

var model = new MyModel();
var template = new UriTemplate("path/{name}{?includeSub}");
model.Id = template.BindByName(new Dictionary<string, object>
{
    { "name", "guid" },
    { "includeSub", true }
});

Second, the very same template could be used for routing in Nancy. It is what I started implementing in Nancy.Routing.UriTemplates

public class MyModelModule : UriTemplateModule 
{
    public MyModelModule()
    {
        using (Templates)
        {
            Get("path/{name}{?includeSub}", _ => new MyModel());
        }
    }
}

By introducing a pattern similar to ServiceStack routing, where routing is part of the model it should be possible to simplify handling of URI Templates for routing and creating identifiers.

Something like

[IdentifierTemplate("path/{name}{?includeSub}")]
public class MyModel 
{
    public Uri Id { get; set; }

    public string Name { get; set; }
}

var model = new MyModel { Name = "Tomasz" };
model.Id = model.BindTemplate(new { includeSub = true });

The BindTemplate would be an extension which looks for IdentifierTemplateAttribute on given instance's type and uses passed params and/or property values to fill in the template.

And finally, the route path wouldn't be needed at all in module

// ArgolisModule inheriting UriTemplateModule
public class MyModelModule : ArgolisModule
{
    public MyModelModule()
    {
        using (Templates)
        {
            Get<MyModel>(_ => new MyModel());
        }
    }
}

tpluscode avatar Nov 28 '16 12:11 tpluscode