Skip to content

Commit 9417907

Browse files
author
Chris Santero
committed
refactor to streamline API surface
1 parent 20cbcb4 commit 9417907

21 files changed

+613
-372
lines changed

JSONAPI.EntityFramework.Tests.TestWebApp/Startup.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Reflection;
43
using System.Web;
54
using System.Web.Http;
6-
using System.Web.Http.Dispatcher;
75
using Autofac;
86
using Autofac.Integration.WebApi;
9-
using JSONAPI.ActionFilters;
107
using JSONAPI.Core;
11-
using JSONAPI.EntityFramework.ActionFilters;
128
using JSONAPI.EntityFramework.Tests.TestWebApp.Models;
13-
using JSONAPI.Http;
14-
using JSONAPI.Json;
159
using Microsoft.Owin;
1610
using Owin;
1711

@@ -62,27 +56,21 @@ public void Configuration(IAppBuilder app)
6256

6357
private static HttpConfiguration GetWebApiConfiguration()
6458
{
65-
var config = new HttpConfiguration();
66-
6759
var pluralizationService = new PluralizationService();
68-
var modelManager = new ModelManager(pluralizationService);
6960

70-
var formatter = new JsonApiFormatter(modelManager);
71-
config.Formatters.Clear();
72-
config.Formatters.Add(formatter);
61+
var httpConfig = new HttpConfiguration();
7362

74-
// Global filters
75-
config.Filters.Add(new EnumerateQueryableAsyncAttribute());
76-
config.Filters.Add(new EnableSortingAttribute(modelManager));
77-
config.Filters.Add(new EnableFilteringAttribute(modelManager));
63+
// Configure JSON API
64+
new JsonApiConfiguration()
65+
.PluralizeResourceTypesWith(pluralizationService)
66+
.UseEntityFramework()
67+
.Apply(httpConfig);
7868

79-
// Override controller selector
80-
config.Services.Replace(typeof(IHttpControllerSelector), new PascalizedControllerSelector(config));
8169

8270
// Web API routes
83-
config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional });
71+
httpConfig.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional });
8472

85-
return config;
73+
return httpConfig;
8674
}
8775
}
8876
}

JSONAPI.EntityFramework/ActionFilters/EnumerateQueryableAsyncAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace JSONAPI.EntityFramework.ActionFilters
1111
{
12-
public class EnumerateQueryableAsyncAttribute : ActionFilterAttribute
12+
internal class EnumerateQueryableAsyncAttribute : ActionFilterAttribute
1313
{
1414
private readonly Lazy<MethodInfo> _toArrayAsyncMethod = new Lazy<MethodInfo>(() =>
1515
typeof(QueryableExtensions).GetMethods().FirstOrDefault(x => x.Name == "ToArrayAsync" && x.GetParameters().Count() == 2));

JSONAPI.EntityFramework/JSONAPI.EntityFramework.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
<DocumentationFile>bin\Release\JSONAPI.EntityFramework.XML</DocumentationFile>
3939
</PropertyGroup>
4040
<ItemGroup>
41+
<Reference Include="Autofac, Version=3.5.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da, processorArchitecture=MSIL">
42+
<SpecificVersion>False</SpecificVersion>
43+
<HintPath>..\packages\Autofac.3.5.2\lib\net40\Autofac.dll</HintPath>
44+
</Reference>
4145
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
4246
<SpecificVersion>False</SpecificVersion>
4347
<HintPath>..\packages\EntityFramework.6.1.2\lib\net45\EntityFramework.dll</HintPath>
@@ -74,6 +78,7 @@
7478
<Compile Include="EntityFrameworkMaterializer.cs" />
7579
<Compile Include="EntityFrameworkMaterializer_Util.cs" />
7680
<Compile Include="Http\ApiController.cs" />
81+
<Compile Include="JsonApiConfigurationExtensions.cs" />
7782
<Compile Include="PluralizationService.cs" />
7883
<Compile Include="Properties\AssemblyInfo.cs" />
7984
</ItemGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Web.Http.Filters;
2+
using Autofac;
3+
using JSONAPI.Core;
4+
using JSONAPI.EntityFramework.ActionFilters;
5+
6+
namespace JSONAPI.EntityFramework
7+
{
8+
/// <summary>
9+
/// Extension Methods for JSONAPI.JsonApiConfiguration
10+
/// </summary>
11+
public static class JsonApiConfigurationExtensions
12+
{
13+
/// <summary>
14+
/// Add Entity Framework specific handling to the configuration
15+
/// </summary>
16+
/// <param name="jsonApiConfig">The configuration object to modify</param>
17+
/// <returns>The same configuration object that was passed in</returns>
18+
public static JsonApiConfiguration UseEntityFramework(this JsonApiConfiguration jsonApiConfig)
19+
{
20+
jsonApiConfig.Builder.RegisterType<EnumerateQueryableAsyncAttribute>().As<IActionFilter>();
21+
22+
return jsonApiConfig;
23+
}
24+
}
25+
}

JSONAPI.EntityFramework/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Autofac" version="3.5.2" targetFramework="net45" />
34
<package id="EntityFramework" version="6.1.2" targetFramework="net45" />
45
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.2" targetFramework="net45" />
56
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.2" targetFramework="net45" />

JSONAPI.Tests/ActionFilters/EnableSortingAttributeTests.cs

Lines changed: 0 additions & 181 deletions
This file was deleted.

0 commit comments

Comments
 (0)