Skip to content

Commit 84f45a9

Browse files
authored
Merge pull request #409 from json-api-dotnet/feat/#293
Feat/#293: Naming Conventions, Improvements to AutoDiscovery, and ResourceDefinition
2 parents adddc1f + e3a3345 commit 84f45a9

38 files changed

+1065
-125
lines changed

Diff for: Build.ps1

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ $revision = "{0:D4}" -f [convert]::ToInt32($revision, 10)
2323

2424
dotnet restore
2525

26+
dotnet build ./src/Examples/GettingStarted/GettingStarted.csproj
27+
CheckLastExitCode
28+
2629
dotnet test ./test/UnitTests/UnitTests.csproj
2730
CheckLastExitCode
2831

@@ -38,7 +41,10 @@ CheckLastExitCode
3841
dotnet test ./test/ResourceEntitySeparationExampleTests/ResourceEntitySeparationExampleTests.csproj
3942
CheckLastExitCode
4043

41-
dotnet build ./src/JsonApiDotNetCore -c Release
44+
dotnet test ./test/DiscoveryTests/DiscoveryTests.csproj
45+
CheckLastExitCode
46+
47+
dotnet build ./src/JsonApiDotNetCore/JsonApiDotNetCore.csproj -c Release
4248
CheckLastExitCode
4349

4450
Write-Output "APPVEYOR_REPO_TAG: $env:APPVEYOR_REPO_TAG"

Diff for: JsonApiDotnetCore.sln

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio 15
33
VisualStudioVersion = 15.0.27130.2010
44
MinimumVisualStudioVersion = 10.0.40219.1
@@ -45,6 +45,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExa
4545
EndProject
4646
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExampleTests", "test\ResourceEntitySeparationExampleTests\ResourceEntitySeparationExampleTests.csproj", "{6DFA30D7-1679-4333-9779-6FB678E48EF5}"
4747
EndProject
48+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GettingStarted", "src\Examples\GettingStarted\GettingStarted.csproj", "{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}"
49+
EndProject
50+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscoveryTests", "test\DiscoveryTests\DiscoveryTests.csproj", "{09C0C8D8-B721-4955-8889-55CB149C3B5C}"
51+
EndProject
4852
Global
4953
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5054
Debug|Any CPU = Debug|Any CPU
@@ -187,6 +191,30 @@ Global
187191
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.Build.0 = Release|Any CPU
188192
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.ActiveCfg = Release|Any CPU
189193
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.Build.0 = Release|Any CPU
194+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|Any CPU.Build.0 = Debug|Any CPU
196+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|x64.ActiveCfg = Debug|Any CPU
197+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|x64.Build.0 = Debug|Any CPU
198+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|x86.ActiveCfg = Debug|Any CPU
199+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Debug|x86.Build.0 = Debug|Any CPU
200+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|Any CPU.ActiveCfg = Release|Any CPU
201+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|Any CPU.Build.0 = Release|Any CPU
202+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|x64.ActiveCfg = Release|Any CPU
203+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|x64.Build.0 = Release|Any CPU
204+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|x86.ActiveCfg = Release|Any CPU
205+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71}.Release|x86.Build.0 = Release|Any CPU
206+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
207+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
208+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x64.ActiveCfg = Debug|Any CPU
209+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x64.Build.0 = Debug|Any CPU
210+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x86.ActiveCfg = Debug|Any CPU
211+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x86.Build.0 = Debug|Any CPU
212+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
213+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|Any CPU.Build.0 = Release|Any CPU
214+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x64.ActiveCfg = Release|Any CPU
215+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x64.Build.0 = Release|Any CPU
216+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.ActiveCfg = Release|Any CPU
217+
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.Build.0 = Release|Any CPU
190218
EndGlobalSection
191219
GlobalSection(SolutionProperties) = preSolution
192220
HideSolutionNode = FALSE
@@ -205,6 +233,8 @@ Global
205233
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
206234
{F4097194-9415-418A-AB4E-315C5D5466AF} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
207235
{6DFA30D7-1679-4333-9779-6FB678E48EF5} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
236+
{9B2A5AD7-0BF4-472E-A1B4-8BB623FDEB71} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
237+
{09C0C8D8-B721-4955-8889-55CB149C3B5C} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
208238
EndGlobalSection
209239
GlobalSection(ExtensibilityGlobals) = postSolution
210240
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}

Diff for: src/Examples/GettingStarted/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.db
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using GettingStarted.Models;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
5+
namespace GettingStarted
6+
{
7+
public class ArticlesController : JsonApiController<Article>
8+
{
9+
public ArticlesController(
10+
IJsonApiContext jsonApiContext,
11+
IResourceService<Article> resourceService)
12+
: base(jsonApiContext, resourceService)
13+
{ }
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using GettingStarted.Models;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
5+
namespace GettingStarted
6+
{
7+
public class PeopleController : JsonApiController<Person>
8+
{
9+
public PeopleController(
10+
IJsonApiContext jsonApiContext,
11+
IResourceService<Person> resourceService)
12+
: base(jsonApiContext, resourceService)
13+
{ }
14+
}
15+
}

Diff for: src/Examples/GettingStarted/Data/SampleDbContext.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using GettingStarted.Models;
2+
using GettingStarted.ResourceDefinitionExample;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
namespace GettingStarted
6+
{
7+
public class SampleDbContext : DbContext
8+
{
9+
public SampleDbContext(DbContextOptions<SampleDbContext> options)
10+
: base(options)
11+
{ }
12+
13+
public DbSet<Article> Articles { get; set; }
14+
public DbSet<Person> People { get; set; }
15+
public DbSet<Model> Models { get; set; }
16+
}
17+
}

Diff for: src/Examples/GettingStarted/GettingStarted.csproj

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<Folder Include="wwwroot\" />
9+
</ItemGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="../../JsonApiDotNetCore/JsonApiDotNetCore.csproj" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.AspNetCore" Version="2.1.0" />
17+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="2.1.0" />
18+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.0" />
19+
</ItemGroup>
20+
21+
</Project>

Diff for: src/Examples/GettingStarted/Models/Article.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using JsonApiDotNetCore.Models;
2+
3+
namespace GettingStarted.Models
4+
{
5+
public class Article : Identifiable
6+
{
7+
[Attr]
8+
public string Title { get; set; }
9+
10+
[HasOne]
11+
public Person Author { get; set; }
12+
public int AuthorId { get; set; }
13+
}
14+
}

Diff for: src/Examples/GettingStarted/Models/Person.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Models;
3+
4+
namespace GettingStarted.Models
5+
{
6+
public class Person : Identifiable
7+
{
8+
[Attr]
9+
public string Name { get; set; }
10+
11+
[HasMany]
12+
public List<Article> Articles { get; set; }
13+
}
14+
}

Diff for: src/Examples/GettingStarted/Program.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
10+
11+
namespace GettingStarted
12+
{
13+
public class Program
14+
{
15+
public static void Main(string[] args)
16+
{
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
23+
.UseUrls("http://localhost:5001")
24+
.Build();
25+
}
26+
}

Diff for: src/Examples/GettingStarted/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Sample project
2+
3+
## Usage
4+
5+
`dotnet run` to run the project
6+
7+
You can verify the project is running by checking this endpoint:
8+
`localhost:5001/api/sample-model`
9+
10+
For further documentation and implementation of a JsonApiDotnetCore Application see the documentation or GitHub page:
11+
12+
Repository: https://github.com/json-api-dotnet/JsonApiDotNetCore
13+
14+
Documentation: https://json-api-dotnet.github.io/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using JsonApiDotNetCore.Models;
2+
3+
namespace GettingStarted.ResourceDefinitionExample
4+
{
5+
public class Model : Identifiable
6+
{
7+
[Attr]
8+
public string DontExpose { get; set; }
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Collections.Generic;
2+
using JsonApiDotNetCore.Models;
3+
4+
namespace GettingStarted.ResourceDefinitionExample
5+
{
6+
public class ModelDefinition : ResourceDefinition<Model>
7+
{
8+
// this allows POST / PATCH requests to set the value of a
9+
// property, but we don't include this value in the response
10+
// this might be used if the incoming value gets hashed or
11+
// encrypted prior to being persisted and this value should
12+
// never be sent back to the client
13+
protected override List<AttrAttribute> OutputAttrs()
14+
=> Remove(model => model.DontExpose);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using GettingStarted.Models;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
5+
namespace GettingStarted.ResourceDefinitionExample
6+
{
7+
public class ModelsController : JsonApiController<Model>
8+
{
9+
public ModelsController(
10+
IJsonApiContext jsonApiContext,
11+
IResourceService<Model> resourceService)
12+
: base(jsonApiContext, resourceService)
13+
{ }
14+
}
15+
}

Diff for: src/Examples/GettingStarted/Startup.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Hosting;
7+
using Microsoft.AspNetCore.Http;
8+
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.EntityFrameworkCore;
10+
using JsonApiDotNetCore.Extensions;
11+
12+
namespace GettingStarted
13+
{
14+
public class Startup
15+
{
16+
public void ConfigureServices(IServiceCollection services)
17+
{
18+
services.AddDbContext<SampleDbContext>(options =>
19+
{
20+
options.UseSqlite("Data Source=sample.db");
21+
});
22+
23+
var mvcCoreBuilder = services.AddMvcCore();
24+
services.AddJsonApi(
25+
options => options.Namespace = "api",
26+
mvcCoreBuilder,
27+
discover => discover.AddCurrentAssembly());
28+
}
29+
30+
public void Configure(IApplicationBuilder app, IHostingEnvironment env, SampleDbContext context)
31+
{
32+
context.Database.EnsureDeleted(); // indicies need to be reset
33+
context.Database.EnsureCreated();
34+
35+
app.UseJsonApi();
36+
}
37+
}
38+
}

Diff for: src/Examples/JsonApiDotNetCoreExample/Startup.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
4141
options.IncludeTotalRecordCount = true;
4242
},
4343
mvcBuilder,
44-
discovery => discovery.AddCurrentAssemblyServices());
44+
discovery => discovery.AddCurrentAssembly());
4545

46-
var provider = services.BuildServiceProvider();
47-
return provider;
46+
return services.BuildServiceProvider();
4847
}
4948

5049
public virtual void Configure(

Diff for: src/Examples/ReportsExample/Startup.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ public Startup(IHostingEnvironment env)
2525
public virtual void ConfigureServices(IServiceCollection services)
2626
{
2727
var mvcBuilder = services.AddMvcCore();
28-
services.AddJsonApi(
29-
opt => opt.Namespace = "api",
30-
mvcBuilder,
31-
discovery => discovery.AddCurrentAssemblyServices());
28+
services.AddJsonApi(
29+
opt => opt.Namespace = "api",
30+
mvcBuilder,
31+
discovery => discovery.AddCurrentAssembly());
3232
}
3333

3434
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)

0 commit comments

Comments
 (0)