https://github.com/mrahhal/mr.augmenter
Take control of the data your API returns.
https://github.com/mrahhal/mr.augmenter
api aspnetcore augmenter csharp middleware mvc
Last synced: 11 months ago
JSON representation
Take control of the data your API returns.
- Host: GitHub
- URL: https://github.com/mrahhal/mr.augmenter
- Owner: mrahhal
- License: mit
- Created: 2017-01-07T10:48:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2018-07-21T13:41:49.000Z (almost 8 years ago)
- Last Synced: 2025-08-01T09:15:40.083Z (11 months ago)
- Topics: api, aspnetcore, augmenter, csharp, middleware, mvc
- Language: C#
- Size: 158 KB
- Stars: 7
- Watchers: 2
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# MR.Augmenter
AppVeyor | Travis
---------|-------
[](https://ci.appveyor.com/project/mrahhal/mr-augmenter) | [](https://travis-ci.org/mrahhal/MR.Augmenter)
[](https://www.nuget.org/packages/MR.Augmenter)
[](https://opensource.org/licenses/MIT)
Take control of the data your API returns.
[CHANGELOG](CHANGELOG.md)
## 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:
```cs
return Ok(Service.GetModel());
```
## Example
This is what we'll be able to do after we configure Augmenter:
```cs
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.
}
```
```cs
public IActionResult Get()
{
var model = Service.GetModel();
return Ok(model);
}
```
Returned json:
```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:
```cs
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
```cs
services.AddAugmenter(config =>
{
// Start configuring the type "Model1".
config.Configure(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:
```cs
services.AddAugmenter(config =>
{
// Add the assemblies that you want us to scan for type configuration classes.
config.AddAssembly(typeof(Startup).Assembly);
});
```
And somewhere else:
```cs
// You should have a public default ctor and call configuration methods from it.
public class Model1Configuration : TypeConfiguration
{
public Model1Configuration()
{
Remove(nameof(Model1.Secret));
}
}
```
For a lot more options checkout the samples.
## Advanced
[TODO]
## Samples
Check out the [`samples`](samples) under "samples/" for more practical use cases.
#### [`Basic`](samples/Basic)
Shows how to configure Augmenter in an Asp.Net core app with some practical examples.