Skip to content

Commit 5f8e176

Browse files
committed
add customizable resource expressions
1 parent 96a6570 commit 5f8e176

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

JsonApiDotNetCore/Abstractions/JsonApiContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public JsonApiContext(HttpContext httpContext, Route route, object dbContext, Js
2424

2525
public Type GetJsonApiResourceType()
2626
{
27-
return Configuration.ResourceMapDefinitions[Route.BaseModelType];
27+
return Configuration.ResourceMapDefinitions[Route.BaseModelType].Item1;
2828
}
2929

3030
public string GetEntityName()

JsonApiDotNetCore/Configuration/IJsonApiModelConfiguration.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using AutoMapper;
34
using JsonApiDotNetCore.Abstractions;
45

56
namespace JsonApiDotNetCore.Configuration
@@ -23,10 +24,11 @@ public interface IJsonApiModelConfiguration
2324
/// <summary>
2425
/// Define explicit mapping of a model to a class that implements IJsonApiResource
2526
/// </summary>
26-
/// <param name="modelType"></param>
27-
/// <param name="resourceType"></param>
27+
/// <typeparam name="TModel"></typeparam>
28+
/// <typeparam name="TResource"></typeparam>
29+
/// <param name="mappingExpression"></param>
2830
/// <exception cref="ArgumentException"></exception>
29-
void AddResourceMapping(Type modelType, Type resourceType);
31+
void AddResourceMapping<TModel, TResource>(Action<IMappingExpression> mappingExpression);
3032

3133
/// <summary>
3234
/// Specifies a controller override class for a particular model type.

JsonApiDotNetCore/Configuration/JsonApiConfigurationBuilder.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using JsonApiDotNetCore.Routing;
44
using Microsoft.EntityFrameworkCore;
55
using System.Linq;
6+
using System.Linq.Expressions;
67
using AutoMapper;
78
using JsonApiDotNetCore.Abstractions;
89
using JsonApiDotNetCore.Attributes;
@@ -68,10 +69,10 @@ private void SetupResourceMaps()
6869
{
6970
foreach (var definition in Config.ResourceMapDefinitions)
7071
{
71-
cfg.CreateMap(definition.Key, definition.Value);
72+
var mappingExpression = cfg.CreateMap(definition.Key, definition.Value.Item1);
73+
definition.Value.Item2?.Invoke(mappingExpression);
7274
}
7375
});
74-
7576
Config.ResourceMapper = mapConfiguration.CreateMapper();
7677
}
7778

@@ -87,7 +88,7 @@ private void LoadDefaultResourceMaps()
8788
// do not overwrite custom definitions
8889
if(!Config.ResourceMapDefinitions.ContainsKey(modelType.UnderlyingSystemType))
8990
{
90-
Config.ResourceMapDefinitions.Add(modelType.UnderlyingSystemType, resourceType);
91+
Config.ResourceMapDefinitions.Add(modelType.UnderlyingSystemType, new Tuple<Type, Action<IMappingExpression>>(resourceType, null));
9192
}
9293
}
9394
}

JsonApiDotNetCore/Configuration/JsonApiModelConfiguration.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,23 @@ public class JsonApiModelConfiguration : IJsonApiModelConfiguration
1818
public IMapper ResourceMapper;
1919
public Type ContextType { get; set; }
2020
public List<RouteDefinition> Routes = new List<RouteDefinition>();
21-
public Dictionary<Type, Type> ResourceMapDefinitions = new Dictionary<Type, Type>();
21+
public Dictionary<Type, Tuple<Type, Action<IMappingExpression>>> ResourceMapDefinitions = new Dictionary<Type, Tuple<Type, Action<IMappingExpression>>>();
2222
public Dictionary<Type, Type> ControllerOverrides = new Dictionary<Type, Type>();
2323

2424
public void SetDefaultNamespace(string ns)
2525
{
2626
Namespace = ns;
2727
}
2828

29-
// TODO: change to AddResourceMapping(Type, Type)
30-
public void AddResourceMapping(Type modelType, Type resourceType)
29+
public void AddResourceMapping<TModel, TResource>(Action<IMappingExpression> mappingExpression)
3130
{
31+
var resourceType = typeof(TResource);
32+
var modelType = typeof(TModel);
33+
3234
if (!resourceType.GetInterfaces().Contains(typeof(IJsonApiResource)))
3335
throw new ArgumentException("Specified type does not implement IJsonApiResource", nameof(resourceType));
3436

35-
ResourceMapDefinitions.Add(modelType, resourceType);
37+
ResourceMapDefinitions.Add(modelType, new Tuple<Type, Action<IMappingExpression>>(resourceType, mappingExpression));
3638
}
3739

3840
public void UseController(Type modelType, Type controllerType)

JsonApiDotNetCoreExample/Startup.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public void ConfigureServices(IServiceCollection services)
4141
config.SetDefaultNamespace("api/v1");
4242
config.UseContext<ApplicationDbContext>();
4343
config.UseController(typeof(TodoItem), typeof(TodoItemsController));
44+
config.AddResourceMapping<Person, PersonResource>(map =>
45+
{
46+
map.ForMember("Name", opt => opt.MapFrom(src => $"{((Person)src).Name}_1"));
47+
});
4448
});
4549
}
4650

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ public class PersonResource : IJsonApiResource
4949
}
5050
```
5151

52-
We use [AutoMapper](http://automapper.org/) as the mapping tool.
53-
You can specify a custom mapping configuration in your `Startup` class like so:
52+
We use [AutoMapper](http://automapper.org/) to map from the context model to the JsonApiResource.
53+
The below snippet shows how you can specify a custom mapping expression in your `Startup` class that will apped '_1' to the resource name.
54+
Check out [AutoMapper's Wiki](https://github.com/AutoMapper/AutoMapper/wiki) for detailed mapping options.
5455

5556
```
56-
// not implemented
57+
services.AddJsonApi(config => {
58+
...
59+
config.AddResourceMapping<Person, PersonResource>(map =>
60+
{
61+
map.ForMember("Name", opt => opt.MapFrom(src => $"{((Person)src).Name}_1"));
62+
});
63+
...
64+
});
5765
```
5866

5967
## Overriding controllers

0 commit comments

Comments
 (0)