Skip to content

mrahhal/MR.Augmenter

Repository files navigation

MR.Augmenter

AppVeyor Travis
Build status Travis

NuGet version License

Take control of the data your API returns.

CHANGELOG

What and Why

  • We have some changes (add props, remove props) we want to apply to our models centrally (and conditionally).
  • We want this to play nice with inheritance, nesting, enumerables, ...
  • We want all of this to happen preferably automatically. We should be able to just write:
return Ok(Service.GetModel());

Example

This is what we'll be able to do after we configure Augmenter:

class Model
{
    public int Id { get; set; }

    public string Hash { get; set; }

    // Suppose we need this in our action, but we want to hide it in our response.
    public string Secret { get; set; }

    // Also, we want to add computed "Image" and "ImageThumb" properties.
}
public IActionResult Get()
{
    var model = Service.GetModel();
    return Ok(model);
}

Returned json:

{
  "Id": 42,
  "Hash": "80f0aa63b234498a88fe5f9d2522c2a7",
  "Image": "/images/80f0aa63b234498a88fe5f9d2522c2a7.jpg",
  "ImageThumb": "/images/thumbs/80f0aa63b234498a88fe5f9d2522c2a7.jpg"
}

Getting started

You'll want to add the MR.Augmenter.AspNetCore package to your dependencies (which depends on MR.Augmenter).

Add Augmenter and configure global options:

services
    .AddAugmenter(config => { ... })
    .ForMvc(); // This will add a global filter that will handle augmenting models you return from actions.

From here on out, simply do what you always do. Augmenter will start working automatically with the models you return.

Inheritance, nested types, anonymous objects containing configured models, lists and arrays... Those are all accounted for.

Configuration

services.AddAugmenter(config =>
{
    // Start configuring the type "Model1".
    config.Configure<Model1>(c =>
    {
        // Use Remove to configure a "Remove" agumentation.
        // From now on, the "Secret" property will always be removed from the response.
        c.Remove(nameof(Model1.Secret));

        // Use Add to configure an "Add" augmentation.
        // From now on, the "Image" property will always be added to the response.
        c.Add("Image", (x, state) => $"/{x.Hash}/some/path");
    });
});

You can also extend TypeConfiguration<>. Augmenter will automatically scan for such types on startup, but you'll have to add your assembly first:

services.AddAugmenter(config =>
{
    // Add the assemblies that you want us to scan for type configuration classes.
    config.AddAssembly(typeof(Startup).Assembly);
});

And somewhere else:

// You should have a public default ctor and call configuration methods from it.
public class Model1Configuration : TypeConfiguration<Model1>
{
    public Model1Configuration()
    {
        Remove(nameof(Model1.Secret));
    }
}

For a lot more options checkout the samples.

Advanced

[TODO]

Samples

Check out the samples under "samples/" for more practical use cases.

Shows how to configure Augmenter in an Asp.Net core app with some practical examples.