Skip to content

Commit 84cf741

Browse files
committed
feat(example): use Dapper and Npgsql in NoEF example
1 parent 0264ea8 commit 84cf741

File tree

9 files changed

+107
-111
lines changed

9 files changed

+107
-111
lines changed

src/NoEntityFrameworkExample/Controllers/MyModelsController.cs renamed to src/NoEntityFrameworkExample/Controllers/CustomTodoItemsController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
using JsonApiDotNetCore.Controllers;
22
using JsonApiDotNetCore.Services;
3+
using JsonApiDotNetCoreExample.Models;
34
using Microsoft.Extensions.Logging;
4-
using NoEntityFrameworkExample.Models;
55

66
namespace NoEntityFrameworkExample.Controllers
77
{
8-
public class MyModelsController : JsonApiController<MyModel>
8+
public class CustomTodoItemsController : JsonApiController<TodoItem>
99
{
10-
public MyModelsController(
10+
public CustomTodoItemsController(
1111
IJsonApiContext jsonApiContext,
12-
IResourceService<MyModel> resourceService,
12+
IResourceService<TodoItem> resourceService,
1313
ILoggerFactory loggerFactory)
1414
: base(jsonApiContext, resourceService, loggerFactory)
1515
{ }

src/NoEntityFrameworkExample/Models/MyModel.cs

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

src/NoEntityFrameworkExample/NoEntityFrameworkExample.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@
1010

1111
<ItemGroup>
1212
<ProjectReference Include="../JsonApiDotNetCore/JsonApiDotNetCore.csproj" />
13+
<ProjectReference Include="../JsonApiDotNetCoreExample/JsonApiDotNetCoreExample.csproj" />
1314
</ItemGroup>
1415

1516
<ItemGroup>
1617
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" />
1718
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" />
1819
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.1" />
20+
<PackageReference Include="Npgsql" Version="3.2.2" />
21+
<PackageReference Include="Dapper" Version="1.50.2" />
1922
</ItemGroup>
2023

2124
</Project>

src/NoEntityFrameworkExample/Services/MyModelService.cs

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using JsonApiDotNetCore.Models;
5+
using JsonApiDotNetCore.Services;
6+
using Microsoft.Extensions.Configuration;
7+
using Npgsql;
8+
using Dapper;
9+
using System.Data;
10+
using JsonApiDotNetCoreExample.Models;
11+
using System.Linq;
12+
13+
namespace NoEntityFrameworkExample.Services
14+
{
15+
public class TodoItemService : IResourceService<TodoItem>
16+
{
17+
private readonly string _connectionString;
18+
19+
public TodoItemService(IConfiguration config)
20+
{
21+
_connectionString = config.GetValue<string>("Data:DefaultConnection");
22+
}
23+
24+
private IDbConnection Connection
25+
{
26+
get
27+
{
28+
return new NpgsqlConnection(_connectionString);
29+
}
30+
}
31+
32+
private async Task<IEnumerable<T>> QueryAsync<T>(Func<IDbConnection, Task<IEnumerable<T>>> query)
33+
{
34+
using (IDbConnection dbConnection = Connection)
35+
{
36+
dbConnection.Open();
37+
return await query(dbConnection);
38+
}
39+
}
40+
41+
public async Task<IEnumerable<TodoItem>> GetAsync()
42+
{
43+
return await QueryAsync<TodoItem>(async connection =>
44+
{
45+
return await connection.QueryAsync<TodoItem>("select * from \"TodoItems\"");
46+
});
47+
}
48+
49+
public async Task<TodoItem> GetAsync(int id)
50+
{
51+
return (await QueryAsync<TodoItem>(async connection =>
52+
{
53+
return await connection.QueryAsync<TodoItem>("select * from \"TodoItems\" where \"Id\"= @id", new { id });
54+
})).SingleOrDefault();
55+
}
56+
57+
public Task<object> GetRelationshipAsync(int id, string relationshipName)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
62+
public Task<object> GetRelationshipsAsync(int id, string relationshipName)
63+
{
64+
throw new NotImplementedException();
65+
}
66+
67+
public Task<TodoItem> CreateAsync(TodoItem entity)
68+
{
69+
throw new NotImplementedException();
70+
}
71+
72+
public Task<bool> DeleteAsync(int id)
73+
{
74+
throw new NotImplementedException();
75+
}
76+
77+
public Task<TodoItem> UpdateAsync(int id, TodoItem entity)
78+
{
79+
throw new NotImplementedException();
80+
}
81+
82+
public Task UpdateRelationshipsAsync(int id, string relationshipName, List<DocumentData> relationships)
83+
{
84+
throw new NotImplementedException();
85+
}
86+
}
87+
}

src/NoEntityFrameworkExample/Startup.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
using JsonApiDotNetCore.Extensions;
22
using JsonApiDotNetCore.Services;
3+
using JsonApiDotNetCoreExample.Models;
34
using Microsoft.AspNetCore.Builder;
45
using Microsoft.AspNetCore.Hosting;
5-
using Microsoft.EntityFrameworkCore;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
88
using Microsoft.Extensions.Logging;
9-
using NoEntityFrameworkExample.Models;
109
using NoEntityFrameworkExample.Services;
1110

1211
namespace NoEntityFrameworkExample
@@ -34,11 +33,12 @@ public void ConfigureServices(IServiceCollection services)
3433
services.AddJsonApi(options => {
3534
options.Namespace = "api/v1";
3635
options.BuildContextGraph((builder) => {
37-
builder.AddResource<MyModel>("my-models");
36+
builder.AddResource<TodoItem>("custom-todo-items");
3837
});
3938
}, mvcBuilder);
4039

41-
services.AddScoped<IResourceService<MyModel>, MyModelService>();
40+
services.AddScoped<IResourceService<TodoItem>, TodoItemService>();
41+
services.AddSingleton<IConfiguration>(Configuration);
4242
}
4343

4444
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

src/NoEntityFrameworkExample/appsettings.Development.json

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

src/NoEntityFrameworkExample/appsettings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
2+
"Data": {
3+
"DefaultConnection": "Host=localhost;Port=5432;Database=JsonApiDotNetCoreExample;User ID=postgres;Password=password"
4+
},
25
"Logging": {
36
"IncludeScopes": false,
47
"LogLevel": {

test/JsonApiDotNetCoreExampleTests/Acceptance/Extensibility/NoEntityFrameworkTests.cs

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
using NoEntityFrameworkExample;
66
using System.Net.Http;
77
using JsonApiDotNetCoreExampleTests.Helpers.Extensions;
8-
using NoEntityFrameworkExample.Models;
98
using System.Threading.Tasks;
109
using System.Net;
11-
using System;
12-
using Newtonsoft.Json;
13-
using JsonApiDotNetCore.Models;
14-
using System.Collections.Generic;
10+
using JsonApiDotNetCoreExample.Models;
1511

1612
namespace JsonApiDotNetCoreExampleTests.Acceptance.Extensibility
1713
{
@@ -23,39 +19,25 @@ public async Task Can_Implement_Custom_IResourceService_Without_EFAsync()
2319
// arrange
2420
var builder = new WebHostBuilder()
2521
.UseStartup<Startup>();
22+
2623
var server = new TestServer(builder);
2724
var client = server.CreateClient();
2825

2926
var httpMethod = new HttpMethod("GET");
30-
var route = $"/api/v1/my-models";
27+
var route = $"/api/v1/custom-todo-items";
3128

3229
var request = new HttpRequestMessage(httpMethod, route);
3330

3431
// act
3532
var response = await client.SendAsync(request);
3633
var responseBody = await response.Content.ReadAsStringAsync();
3734
var deserializedBody = server.GetService<IJsonApiDeSerializer>()
38-
.DeserializeList<MyModel>(responseBody);
39-
40-
var expectedBody = JsonConvert.SerializeObject(new Documents {
41-
Data = new List<DocumentData> {
42-
new DocumentData {
43-
Id = "1",
44-
Type = "my-models",
45-
Attributes = new Dictionary<string, object> {
46-
{"description", "description"}
47-
}
48-
}
49-
}
50-
}, new JsonSerializerSettings {
51-
NullValueHandling = NullValueHandling.Ignore
52-
})
53-
.Replace(" ", string.Empty)
54-
.ToLower();
35+
.DeserializeList<TodoItem>(responseBody);
5536

5637
// assert
5738
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
58-
Assert.Equal(expectedBody, responseBody);
39+
Assert.NotNull(deserializedBody);
40+
Assert.NotEmpty(deserializedBody);
5941
}
6042
}
6143
}

0 commit comments

Comments
 (0)