Skip to content

Commit 77817b3

Browse files
author
Bart Koelman
committed
Updated and simplified NoEntityFrameworkExample project + tests
1 parent 6587475 commit 77817b3

File tree

15 files changed

+266
-305
lines changed

15 files changed

+266
-305
lines changed

src/Examples/GettingStarted/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace GettingStarted
99
{
1010
public sealed class Startup
1111
{
12+
// This method gets called by the runtime. Use this method to add services to the container.
1213
public void ConfigureServices(IServiceCollection services)
1314
{
1415
services.AddDbContext<SampleDbContext>(
@@ -18,6 +19,7 @@ public void ConfigureServices(IServiceCollection services)
1819
options => options.Namespace = "api");
1920
}
2021

22+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
2123
public void Configure(IApplicationBuilder app, SampleDbContext context)
2224
{
2325
context.Database.EnsureDeleted();
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using NoEntityFrameworkExample.Models;
21
using Microsoft.EntityFrameworkCore;
2+
using NoEntityFrameworkExample.Models;
33

44
namespace NoEntityFrameworkExample.Data
55
{
66
public sealed class AppDbContext : DbContext
77
{
8-
public AppDbContext(DbContextOptions<AppDbContext> options)
9-
: base(options)
10-
{ }
11-
128
public DbSet<TodoItem> TodoItems { get; set; }
9+
10+
public AppDbContext(DbContextOptions<AppDbContext> options)
11+
: base(options)
12+
{
13+
}
1314
}
1415
}

src/Examples/NoEntityFrameworkExample/Models/TodoItem.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ namespace NoEntityFrameworkExample.Models
55
{
66
public sealed class TodoItem : Identifiable
77
{
8-
public TodoItem()
9-
{
10-
GuidProperty = Guid.NewGuid();
11-
}
12-
8+
[Attr]
139
public bool IsLocked { get; set; }
1410

1511
[Attr]
@@ -19,6 +15,6 @@ public TodoItem()
1915
public long Ordinal { get; set; }
2016

2117
[Attr]
22-
public Guid GuidProperty { get; set; }
18+
public Guid UniqueId { get; set; } = Guid.NewGuid();
2319
}
2420
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
using Microsoft.AspNetCore;
21
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.Extensions.Hosting;
33

44
namespace NoEntityFrameworkExample
55
{
66
public class Program
77
{
88
public static void Main(string[] args)
99
{
10-
CreateWebHostBuilder(args).Build().Run();
10+
CreateHostBuilder(args).Build().Run();
1111
}
12-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
13-
WebHost.CreateDefaultBuilder(args)
14-
.UseStartup<Startup>();
12+
13+
public static IHostBuilder CreateHostBuilder(string[] args) =>
14+
Host.CreateDefaultBuilder(args)
15+
.ConfigureWebHostDefaults(webBuilder =>
16+
{
17+
webBuilder.UseStartup<Startup>();
18+
});
1519
}
1620
}
Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,30 @@
11
{
2-
"iisSettings": {
3-
"windowsAuthentication": false,
4-
"anonymousAuthentication": true,
5-
"iisExpress": {
6-
"applicationUrl": "http://localhost:22785/",
7-
"sslPort": 44393
8-
}
9-
},
10-
"profiles": {
11-
"IIS Express": {
12-
"commandName": "IISExpress",
13-
"launchBrowser": true,
14-
"environmentVariables": {
15-
"ASPNETCORE_ENVIRONMENT": "Development"
16-
}
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:14149",
8+
"sslPort": 44349
9+
}
1710
},
18-
"NoEntityFrameworkExample": {
19-
"commandName": "Project",
20-
"launchBrowser": true,
21-
"environmentVariables": {
22-
"ASPNETCORE_ENVIRONMENT": "Development"
23-
},
24-
"applicationUrl": "https://localhost:5001;http://localhost:5000"
11+
"profiles": {
12+
"IIS Express": {
13+
"commandName": "IISExpress",
14+
"launchBrowser": false,
15+
"launchUrl": "/api/reports",
16+
"environmentVariables": {
17+
"ASPNETCORE_ENVIRONMENT": "Development"
18+
}
19+
},
20+
"Kestrel": {
21+
"commandName": "Project",
22+
"launchBrowser": false,
23+
"launchUrl": "/api/reports",
24+
"applicationUrl": "https://localhost:44349;http://localhost:14149",
25+
"environmentVariables": {
26+
"ASPNETCORE_ENVIRONMENT": "Development"
27+
}
28+
}
2529
}
26-
}
27-
}
30+
}
Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,37 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Linq;
35
using System.Threading.Tasks;
6+
using Dapper;
47
using JsonApiDotNetCore.Services;
58
using Microsoft.Extensions.Configuration;
6-
using Npgsql;
7-
using Dapper;
8-
using System.Data;
99
using NoEntityFrameworkExample.Models;
10-
using System.Linq;
10+
using Npgsql;
1111

1212
namespace NoEntityFrameworkExample.Services
1313
{
1414
public sealed class TodoItemService : IResourceService<TodoItem>
1515
{
1616
private readonly string _connectionString;
1717

18-
public TodoItemService(IConfiguration config)
18+
public TodoItemService(IConfiguration configuration)
1919
{
20-
_connectionString = config.GetValue<string>("Data:DefaultConnection");
21-
}
22-
23-
private IDbConnection Connection => new NpgsqlConnection(_connectionString);
24-
25-
private async Task<IEnumerable<T>> QueryAsync<T>(Func<IDbConnection, Task<IEnumerable<T>>> query)
26-
{
27-
using IDbConnection dbConnection = Connection;
28-
dbConnection.Open();
29-
return await query(dbConnection);
20+
_connectionString = configuration["Data:DefaultConnection"];
3021
}
3122

3223
public async Task<IEnumerable<TodoItem>> GetAsync()
3324
{
3425
return await QueryAsync(async connection =>
35-
await connection.QueryAsync<TodoItem>("select * from \"TodoItems\""));
26+
await connection.QueryAsync<TodoItem>(@"select * from ""TodoItems"""));
3627
}
3728

3829
public async Task<TodoItem> GetAsync(int id)
3930
{
40-
var query = await QueryAsync(async connection =>
41-
await connection.QueryAsync<TodoItem>("select * from \"TodoItems\" where \"Id\"= @id", new {id}));
42-
43-
return query.SingleOrDefault();
31+
var query = await QueryAsync(async connection =>
32+
await connection.QueryAsync<TodoItem>(@"select * from ""TodoItems"" where ""Id""=@id", new { id }));
33+
34+
return query.Single();
4435
}
4536

4637
public Task<object> GetRelationshipAsync(int id, string relationshipName)
@@ -57,15 +48,16 @@ public async Task<TodoItem> CreateAsync(TodoItem entity)
5748
{
5849
return (await QueryAsync(async connection =>
5950
{
60-
var query = "insert into \"TodoItems\" (\"Description\", \"IsLocked\", \"Ordinal\", \"GuidProperty\") values (@description, @isLocked, @ordinal, @guidProperty) returning \"Id\",\"Description\", \"IsLocked\", \"Ordinal\", \"GuidProperty\"";
61-
var result = await connection.QueryAsync<TodoItem>(query, new { description = entity.Description, ordinal = entity.Ordinal, guidProperty = entity.GuidProperty, isLocked = entity.IsLocked});
51+
var query = @"insert into ""TodoItems"" (""Description"", ""IsLocked"", ""Ordinal"", ""UniqueId"") values (@description, @isLocked, @ordinal, @uniqueId) returning ""Id"", ""Description"", ""IsLocked"", ""Ordinal"", ""UniqueId""";
52+
var result = await connection.QueryAsync<TodoItem>(query, new { description = entity.Description, ordinal = entity.Ordinal, uniqueId = entity.UniqueId, isLocked = entity.IsLocked });
6253
return result;
6354
})).SingleOrDefault();
6455
}
6556

66-
public Task DeleteAsync(int id)
57+
public async Task DeleteAsync(int id)
6758
{
68-
throw new NotImplementedException();
59+
await QueryAsync(async connection =>
60+
await connection.QueryAsync<TodoItem>(@"delete from ""TodoItems"" where ""Id""=@id", new { id }));
6961
}
7062

7163
public Task<TodoItem> UpdateAsync(int id, TodoItem entity)
@@ -77,5 +69,14 @@ public Task UpdateRelationshipsAsync(int id, string relationshipName, object rel
7769
{
7870
throw new NotImplementedException();
7971
}
72+
73+
private async Task<IEnumerable<T>> QueryAsync<T>(Func<IDbConnection, Task<IEnumerable<T>>> query)
74+
{
75+
using IDbConnection dbConnection = GetConnection;
76+
dbConnection.Open();
77+
return await query(dbConnection);
78+
}
79+
80+
private IDbConnection GetConnection => new NpgsqlConnection(_connectionString);
8081
}
8182
}

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,47 @@
22
using JsonApiDotNetCore;
33
using JsonApiDotNetCore.Services;
44
using Microsoft.AspNetCore.Builder;
5-
using Microsoft.AspNetCore.Hosting;
5+
using Microsoft.EntityFrameworkCore;
66
using Microsoft.Extensions.Configuration;
77
using Microsoft.Extensions.DependencyInjection;
8-
using NoEntityFrameworkExample.Services;
9-
using Microsoft.EntityFrameworkCore;
108
using NoEntityFrameworkExample.Data;
119
using NoEntityFrameworkExample.Models;
10+
using NoEntityFrameworkExample.Services;
1211

1312
namespace NoEntityFrameworkExample
1413
{
1514
public class Startup
1615
{
17-
public Startup(IWebHostEnvironment env)
16+
private readonly string _connectionString;
17+
18+
public Startup(IConfiguration configuration)
1819
{
19-
var builder = new ConfigurationBuilder()
20-
.SetBasePath(env.ContentRootPath)
21-
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: false)
22-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
23-
.AddEnvironmentVariables();
24-
Configuration = builder.Build();
20+
_connectionString = configuration["Data:DefaultConnection"];
2521
}
2622

27-
public IConfigurationRoot Configuration { get; }
28-
2923
// This method gets called by the runtime. Use this method to add services to the container.
3024
public virtual void ConfigureServices(IServiceCollection services)
3125
{
32-
// Add framework services.
33-
var mvcBuilder = services.AddMvcCore();
3426
services.AddJsonApi(
35-
options => options.Namespace = "api/v1",
36-
resources: resources => resources.AddResource<TodoItem>("todoItems"),
37-
mvcBuilder: mvcBuilder
38-
);
27+
options => options.Namespace = "api/v1",
28+
resources: builder => builder.AddResource<TodoItem>("todoItems")
29+
);
30+
3931
services.AddScoped<IResourceService<TodoItem>, TodoItemService>();
40-
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
41-
optionsBuilder.UseNpgsql(GetDbConnectionString(), options => options.SetPostgresVersion(new Version(9,6)));
42-
services.AddSingleton<IConfiguration>(Configuration);
43-
services.AddSingleton(optionsBuilder.Options);
44-
services.AddScoped<AppDbContext>();
32+
33+
services.AddDbContext<AppDbContext>(options =>
34+
{
35+
options.UseNpgsql(_connectionString,
36+
postgresOptions => postgresOptions.SetPostgresVersion(new Version(9, 6)));
37+
});
4538
}
4639

4740
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
4841
public void Configure(IApplicationBuilder app, AppDbContext context)
4942
{
5043
context.Database.EnsureCreated();
44+
5145
app.UseJsonApi();
5246
}
53-
54-
public string GetDbConnectionString() => Configuration["Data:DefaultConnection"];
5547
}
5648
}
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"Data": {
3-
"DefaultConnection": "Host=localhost;Port=5432;Database=JsonApiDotNetCoreNoEFCoreExample;User ID=postgres;Password=postgres"
3+
"DefaultConnection": "Host=localhost;Port=5432;Database=NoEntityFrameworkExample;User ID=postgres;Password=postgres"
44
},
55
"Logging": {
6-
"IncludeScopes": false,
76
"LogLevel": {
8-
"Default": "Warning"
7+
"Default": "Warning",
8+
"Microsoft": "Warning",
9+
"Microsoft.EntityFrameworkCore.Database.Command": "Information"
910
}
10-
}
11+
},
12+
"AllowedHosts": "*"
1113
}

src/Examples/ReportsExample/Startup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ namespace ReportsExample
66
{
77
public sealed class Startup
88
{
9+
// This method gets called by the runtime. Use this method to add services to the container.
910
public void ConfigureServices(IServiceCollection services)
1011
{
1112
services.AddJsonApi(
1213
options => options.Namespace = "api",
1314
discovery => discovery.AddCurrentAssembly());
1415
}
1516

17+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
1618
public void Configure(IApplicationBuilder app)
1719
{
1820
app.UseJsonApi();

0 commit comments

Comments
 (0)