-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathStartup.cs
87 lines (74 loc) · 3.37 KB
/
Startup.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using AutoMapper;
using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Extensions;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCore.Services;
using JsonApiDotNetCoreExample.Data;
using JsonApiDotNetCoreExample.Models.Entities;
using JsonApiDotNetCoreExample.Models.Resources;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ResourceEntitySeparationExample.Models;
namespace ResourceEntitySeparationExample
{
public class Startup
{
public readonly IConfiguration Config;
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: false)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Config = builder.Build();
}
public virtual IServiceProvider ConfigureServices(IServiceCollection services)
{
var loggerFactory = new LoggerFactory();
loggerFactory.AddConsole(LogLevel.Warning);
services.AddSingleton<ILoggerFactory>(loggerFactory);
services.AddDbContext<AppDbContext>(options => options
.UseNpgsql(GetDbConnectionString()),
ServiceLifetime.Transient);
services.AddScoped<IDbContextResolver, DbContextResolver<AppDbContext>>();
var mvcBuilder = services.AddMvcCore();
services.AddJsonApi(options => {
options.Namespace = "api/v1";
options.DefaultPageSize = 10;
options.IncludeTotalRecordCount = true;
options.BuildContextGraph((builder) => {
builder.AddResource<CourseResource>("courses");
builder.AddResource<DepartmentResource>("departments");
builder.AddResource<StudentResource>("students");
});
}, mvcBuilder);
services.AddAutoMapper();
services.AddScoped<IResourceMapper, AutoMapperAdapter>();
services.AddScoped<IResourceService<CourseResource, int>, EntityResourceService<CourseResource, CourseEntity, int>>();
services.AddScoped<IResourceService<DepartmentResource, int>, EntityResourceService<DepartmentResource, DepartmentEntity, int>>();
services.AddScoped<IResourceService<StudentResource, int>, EntityResourceService<StudentResource, StudentEntity, int>>();
var provider = services.BuildServiceProvider();
var appContext = provider.GetRequiredService<AppDbContext>();
if (appContext == null)
throw new ArgumentException();
return provider;
}
public virtual void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
AppDbContext context)
{
context.Database.EnsureCreated();
loggerFactory.AddConsole(Config.GetSection("Logging"));
app.UseJsonApi();
}
public string GetDbConnectionString() => Config["Data:DefaultConnection"];
}
}