Skip to content

Commit 6097476

Browse files
committed
Add AutoMapper DI library
Add new MappingResourceService that can be used to separate and map between Entity Framework objects and JSON-API data transfer objects Added new example project, ResourceEntitySeparationExample, showing how to configure an entity/resource split and corresponding tests.
1 parent 404231b commit 6097476

23 files changed

+1207
-9
lines changed

JsonApiDotnetCore.sln

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OperationsExample", "src\Ex
4141
EndProject
4242
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OperationsExampleTests", "test\OperationsExampleTests\OperationsExampleTests.csproj", "{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1}"
4343
EndProject
44+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExample", "src\Examples\ResourceEntitySeparationExample\ResourceEntitySeparationExample.csproj", "{F4097194-9415-418A-AB4E-315C5D5466AF}"
45+
EndProject
46+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ResourceEntitySeparationExampleTests", "test\ResourceEntitySeparationExampleTests\ResourceEntitySeparationExampleTests.csproj", "{6DFA30D7-1679-4333-9779-6FB678E48EF5}"
47+
EndProject
4448
Global
4549
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4650
Debug|Any CPU = Debug|Any CPU
@@ -159,6 +163,30 @@ Global
159163
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1}.Release|x64.Build.0 = Release|Any CPU
160164
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1}.Release|x86.ActiveCfg = Release|Any CPU
161165
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1}.Release|x86.Build.0 = Release|Any CPU
166+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
167+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
168+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|x64.ActiveCfg = Debug|Any CPU
169+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|x64.Build.0 = Debug|Any CPU
170+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|x86.ActiveCfg = Debug|Any CPU
171+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Debug|x86.Build.0 = Debug|Any CPU
172+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
173+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|Any CPU.Build.0 = Release|Any CPU
174+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|x64.ActiveCfg = Release|Any CPU
175+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|x64.Build.0 = Release|Any CPU
176+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|x86.ActiveCfg = Release|Any CPU
177+
{F4097194-9415-418A-AB4E-315C5D5466AF}.Release|x86.Build.0 = Release|Any CPU
178+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
179+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|Any CPU.Build.0 = Debug|Any CPU
180+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|x64.ActiveCfg = Debug|Any CPU
181+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|x64.Build.0 = Debug|Any CPU
182+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|x86.ActiveCfg = Debug|Any CPU
183+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Debug|x86.Build.0 = Debug|Any CPU
184+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|Any CPU.ActiveCfg = Release|Any CPU
185+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|Any CPU.Build.0 = Release|Any CPU
186+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.ActiveCfg = Release|Any CPU
187+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.Build.0 = Release|Any CPU
188+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.ActiveCfg = Release|Any CPU
189+
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.Build.0 = Release|Any CPU
162190
EndGlobalSection
163191
GlobalSection(SolutionProperties) = preSolution
164192
HideSolutionNode = FALSE
@@ -175,6 +203,8 @@ Global
175203
{1F604666-BB0F-413E-922D-9D37C6073285} = {076E1AE4-FD25-4684-B826-CAAE37FEA0AA}
176204
{CF2C1EB6-8449-4B35-B8C7-F43D6D90632D} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
177205
{9CD2C116-D133-4FE4-97DA-A9FEAFF045F1} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
206+
{F4097194-9415-418A-AB4E-315C5D5466AF} = {026FBC6C-AF76-4568-9B87-EC73457899FD}
207+
{6DFA30D7-1679-4333-9779-6FB678E48EF5} = {24B15015-62E5-42E1-9BA0-ECE6BE7AA15F}
178208
EndGlobalSection
179209
GlobalSection(ExtensibilityGlobals) = postSolution
180210
SolutionGuid = {A2421882-8F0A-4905-928F-B550B192F9A4}

src/Examples/JsonApiDotNetCoreExample/Data/AppDbContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3939
public DbSet<Author> Authors { get; set; }
4040
public DbSet<NonJsonApiResource> NonJsonApiResources { get; set; }
4141
public DbSet<User> Users { get; set; }
42+
43+
public DbSet<StudentEntity> Students { get; set; }
4244
}
4345
}

src/Examples/JsonApiDotNetCoreExample/Migrations/20180327120810_initial.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,21 @@ protected override void Up(MigrationBuilder migrationBuilder)
149149
name: "IX_TodoItems_OwnerId",
150150
table: "TodoItems",
151151
column: "OwnerId");
152+
153+
migrationBuilder.CreateTable(
154+
name: "student",
155+
columns: table => new
156+
{
157+
Id = table.Column<int>(nullable: false)
158+
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn),
159+
firstname = table.Column<string>(name: "first-name", nullable: false),
160+
lastname = table.Column<string>(name: "last-name", maxLength: 255, nullable: false),
161+
address = table.Column<string>(nullable: true)
162+
},
163+
constraints: table =>
164+
{
165+
table.PrimaryKey("PK_student", x => x.Id);
166+
});
152167
}
153168

154169
protected override void Down(MigrationBuilder migrationBuilder)
@@ -170,6 +185,9 @@ protected override void Down(MigrationBuilder migrationBuilder)
170185

171186
migrationBuilder.DropTable(
172187
name: "People");
188+
189+
migrationBuilder.DropTable(
190+
name: "student");
173191
}
174192
}
175193
}

src/Examples/JsonApiDotNetCoreExample/Migrations/AppDbContextModelSnapshot.cs

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
// <auto-generated />
1+
// <auto-generated />
2+
using System;
23
using JsonApiDotNetCoreExample.Data;
34
using Microsoft.EntityFrameworkCore;
45
using Microsoft.EntityFrameworkCore.Infrastructure;
5-
using Microsoft.EntityFrameworkCore.Metadata;
6-
using Microsoft.EntityFrameworkCore.Migrations;
7-
using Microsoft.EntityFrameworkCore.Storage;
8-
using Microsoft.EntityFrameworkCore.Storage.Internal;
9-
using System;
6+
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
107
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
118

129
namespace JsonApiDotNetCoreExample.Migrations
@@ -19,7 +16,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1916
#pragma warning disable 612, 618
2017
modelBuilder
2118
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn)
22-
.HasAnnotation("ProductVersion", "2.0.1-rtm-125");
19+
.HasAnnotation("ProductVersion", "2.1.0-rtm-30799")
20+
.HasAnnotation("Relational:MaxIdentifierLength", 63);
2321

2422
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Article", b =>
2523
{
@@ -61,6 +59,16 @@ protected override void BuildModel(ModelBuilder modelBuilder)
6159
b.ToTable("CamelCasedModels");
6260
});
6361

62+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.NonJsonApiResource", b =>
63+
{
64+
b.Property<int>("Id")
65+
.ValueGeneratedOnAdd();
66+
67+
b.HasKey("Id");
68+
69+
b.ToTable("NonJsonApiResources");
70+
});
71+
6472
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Person", b =>
6573
{
6674
b.Property<int>("Id")
@@ -70,11 +78,37 @@ protected override void BuildModel(ModelBuilder modelBuilder)
7078

7179
b.Property<string>("LastName");
7280

81+
b.Property<int?>("UnIncludeableItemId");
82+
7383
b.HasKey("Id");
7484

85+
b.HasIndex("UnIncludeableItemId");
86+
7587
b.ToTable("People");
7688
});
7789

90+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.StudentEntity", b =>
91+
{
92+
b.Property<int>("Id")
93+
.ValueGeneratedOnAdd();
94+
95+
b.Property<string>("Address")
96+
.HasColumnName("address");
97+
98+
b.Property<string>("FirstName")
99+
.IsRequired()
100+
.HasColumnName("first-name");
101+
102+
b.Property<string>("LastName")
103+
.IsRequired()
104+
.HasColumnName("last-name")
105+
.HasMaxLength(255);
106+
107+
b.HasKey("Id");
108+
109+
b.ToTable("student");
110+
});
111+
78112
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
79113
{
80114
b.Property<int>("Id")
@@ -125,6 +159,20 @@ protected override void BuildModel(ModelBuilder modelBuilder)
125159
b.ToTable("TodoItemCollections");
126160
});
127161

162+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.User", b =>
163+
{
164+
b.Property<int>("Id")
165+
.ValueGeneratedOnAdd();
166+
167+
b.Property<string>("Password");
168+
169+
b.Property<string>("Username");
170+
171+
b.HasKey("Id");
172+
173+
b.ToTable("Users");
174+
});
175+
128176
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Article", b =>
129177
{
130178
b.HasOne("JsonApiDotNetCoreExample.Models.Author", "Author")
@@ -133,6 +181,13 @@ protected override void BuildModel(ModelBuilder modelBuilder)
133181
.OnDelete(DeleteBehavior.Cascade);
134182
});
135183

184+
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.Person", b =>
185+
{
186+
b.HasOne("JsonApiDotNetCoreExample.Models.TodoItem", "UnIncludeableItem")
187+
.WithMany()
188+
.HasForeignKey("UnIncludeableItemId");
189+
});
190+
136191
modelBuilder.Entity("JsonApiDotNetCoreExample.Models.TodoItem", b =>
137192
{
138193
b.HasOne("JsonApiDotNetCoreExample.Models.Person", "Assignee")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JsonApiDotNetCore.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
4+
namespace JsonApiDotNetCoreExample.Models
5+
{
6+
public class StudentDto : Identifiable
7+
{
8+
[Attr("name")]
9+
[Required]
10+
public string Name { get; set; }
11+
12+
[Attr("address")]
13+
public string Address { get; set; }
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using JsonApiDotNetCore.Models;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.ComponentModel.DataAnnotations.Schema;
4+
5+
namespace JsonApiDotNetCoreExample.Models
6+
{
7+
[Table("student")]
8+
public class StudentEntity : Identifiable
9+
{
10+
[Column("first-name")]
11+
[Required]
12+
public string FirstName { get; set; }
13+
14+
[Column("last-name")]
15+
[Required]
16+
[StringLength(255, MinimumLength = 3)]
17+
public string LastName { get; set; }
18+
19+
[Column("address")]
20+
public string Address { get; set; }
21+
}
22+
}

0 commit comments

Comments
 (0)