Skip to content

Commit a983d18

Browse files
committed
refactor UseController method
1 parent 61e75c5 commit a983d18

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

JsonApiDotNetCore/Configuration/IJsonApiModelConfiguration.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public interface IJsonApiModelConfiguration
3333
/// <summary>
3434
/// Specifies a controller override class for a particular model type.
3535
/// </summary>
36-
/// <param name="modelType"></param>
37-
/// <param name="controllerType"></param>
36+
/// <typeparam name="TModel"></typeparam>
37+
/// <typeparam name="TController"></typeparam>
3838
/// <exception cref="ArgumentException"></exception>
39-
void UseController(Type modelType, Type controllerType);
39+
void UseController<TModel, TController>();
4040
}
4141
}

JsonApiDotNetCore/Configuration/JsonApiModelConfiguration.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ public void AddResourceMapping<TModel, TResource>(Action<IMappingExpression> map
3737
ResourceMapDefinitions.Add(modelType, new Tuple<Type, Action<IMappingExpression>>(resourceType, mappingExpression));
3838
}
3939

40-
public void UseController(Type modelType, Type controllerType)
40+
public void UseController<TModel, TController>()
4141
{
42-
if(!controllerType.GetInterfaces().Contains(typeof(IJsonApiController)))
42+
var modelType = typeof(TModel);
43+
var controllerType = typeof(TController);
44+
45+
if (!controllerType.GetInterfaces().Contains(typeof(IJsonApiController)))
4346
throw new ArgumentException("Specified type does not implement IJsonApiController", nameof(controllerType));
4447

4548
ControllerOverrides[modelType] = controllerType;

JsonApiDotNetCoreExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void ConfigureServices(IServiceCollection services)
4040
services.AddJsonApi(config => {
4141
config.SetDefaultNamespace("api/v1");
4242
config.UseContext<ApplicationDbContext>();
43-
config.UseController(typeof(TodoItem), typeof(TodoItemsController));
43+
config.UseController<TodoItem,TodoItemsController>();
4444
config.AddResourceMapping<Person, PersonResource>(map =>
4545
{
4646
map.ForMember("Name", opt => opt.MapFrom(src => $"{((Person)src).Name}_1"));

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ You can define your own controllers that implement the `IJsonApiController` like
7474
```
7575
services.AddJsonApi(config => {
7676
...
77-
config.UseController(typeof(TodoItem), typeof(TodoItemsController));
77+
config.UseController<TodoItem, TodoItemsController>();
7878
...
7979
});
8080
```

0 commit comments

Comments
 (0)