|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using JsonApiDotNetCore.Configuration; |
| 6 | +using JsonApiDotNetCore.Data; |
| 7 | +using JsonApiDotNetCore.Formatters; |
| 8 | +using JsonApiDotNetCore.Graph; |
| 9 | +using JsonApiDotNetCore.Internal; |
| 10 | +using JsonApiDotNetCore.Internal.Generics; |
| 11 | +using JsonApiDotNetCore.Managers; |
| 12 | +using JsonApiDotNetCore.Managers.Contracts; |
| 13 | +using JsonApiDotNetCore.Middleware; |
| 14 | +using JsonApiDotNetCore.Models; |
| 15 | +using JsonApiDotNetCore.Serialization; |
| 16 | +using JsonApiDotNetCore.Hooks; |
| 17 | +using JsonApiDotNetCore.Services; |
| 18 | +using Microsoft.AspNetCore.Http; |
| 19 | +using Microsoft.AspNetCore.Mvc; |
| 20 | +using Microsoft.EntityFrameworkCore; |
| 21 | +using Microsoft.Extensions.DependencyInjection; |
| 22 | +using JsonApiDotNetCore.Internal.Contracts; |
| 23 | +using JsonApiDotNetCore.Query; |
| 24 | +using JsonApiDotNetCore.Serialization.Server.Builders; |
| 25 | +using JsonApiDotNetCore.Serialization.Server; |
| 26 | +using JsonApiDotNetCore.Serialization.Client; |
| 27 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 28 | +using JsonApiDotNetCore.Builders; |
| 29 | + |
| 30 | +namespace JsonApiDotNetCore.Builders |
| 31 | +{ |
| 32 | + public class JsonApiApplicationBuilder |
| 33 | + { |
| 34 | + public readonly JsonApiOptions JsonApiOptions = new JsonApiOptions(); |
| 35 | + private IResourceGraphBuilder _resourceGraphBuilder; |
| 36 | + private IServiceDiscoveryFacade _serviceDiscoveryFacade; |
| 37 | + private bool _usesDbContext; |
| 38 | + private readonly IServiceCollection _services; |
| 39 | + private readonly IMvcCoreBuilder _mvcBuilder; |
| 40 | + |
| 41 | + public JsonApiApplicationBuilder(IServiceCollection services, IMvcCoreBuilder mvcBuilder) |
| 42 | + { |
| 43 | + _services = services; |
| 44 | + _mvcBuilder = mvcBuilder; |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + public void ConfigureJsonApiOptions(Action<JsonApiOptions> configureOptions) => configureOptions(JsonApiOptions); |
| 49 | + |
| 50 | + public void ConfigureMvc() |
| 51 | + { |
| 52 | + RegisterJsonApiStartupServices(); |
| 53 | + |
| 54 | + var intermediateProvider = _services.BuildServiceProvider(); |
| 55 | + _resourceGraphBuilder = intermediateProvider.GetRequiredService<IResourceGraphBuilder>(); |
| 56 | + _serviceDiscoveryFacade = intermediateProvider.GetRequiredService<IServiceDiscoveryFacade>(); |
| 57 | + var exceptionFilterProvider = intermediateProvider.GetRequiredService<IJsonApiExceptionFilterProvider>(); |
| 58 | + var typeMatchFilterProvider = intermediateProvider.GetRequiredService<IJsonApiTypeMatchFilterProvider>(); |
| 59 | + |
| 60 | + _mvcBuilder.AddMvcOptions(mvcOptions => |
| 61 | + { |
| 62 | + mvcOptions.Filters.Add(exceptionFilterProvider.Get()); |
| 63 | + mvcOptions.Filters.Add(typeMatchFilterProvider.Get()); |
| 64 | + mvcOptions.InputFormatters.Insert(0, new JsonApiInputFormatter()); |
| 65 | + mvcOptions.OutputFormatters.Insert(0, new JsonApiOutputFormatter()); |
| 66 | + }); |
| 67 | + |
| 68 | + var routingConvention = intermediateProvider.GetRequiredService<IJsonApiRoutingConvention>(); |
| 69 | + _mvcBuilder.AddMvcOptions(opt => opt.Conventions.Insert(0, routingConvention)); |
| 70 | + _services.AddSingleton(routingConvention); // <--- why is this needed? |
| 71 | + } |
| 72 | + |
| 73 | + public void AutoDiscover(Action<IServiceDiscoveryFacade> autoDiscover) |
| 74 | + { |
| 75 | + autoDiscover(_serviceDiscoveryFacade); |
| 76 | + } |
| 77 | + |
| 78 | + public void ConfigureResources(Action<IResourceGraphBuilder> resourceGraphBuilder) |
| 79 | + { |
| 80 | + resourceGraphBuilder(_resourceGraphBuilder); |
| 81 | + } |
| 82 | + |
| 83 | + public void ConfigureResources<TContext>(Action<IResourceGraphBuilder> resourceGraphBuilder) where TContext : DbContext |
| 84 | + { |
| 85 | + _resourceGraphBuilder.AddDbContext<TContext>(); |
| 86 | + _usesDbContext = true; |
| 87 | + _services.AddScoped<IDbContextResolver, DbContextResolver<TContext>>(); |
| 88 | + resourceGraphBuilder?.Invoke(_resourceGraphBuilder); |
| 89 | + } |
| 90 | + |
| 91 | + private void RegisterJsonApiStartupServices() |
| 92 | + { |
| 93 | + _services.AddSingleton<IJsonApiOptions>(JsonApiOptions); |
| 94 | + _services.TryAddSingleton<IResourceNameFormatter>(new KebabCaseFormatter()); |
| 95 | + _services.TryAddSingleton<IJsonApiRoutingConvention, DefaultRoutingConvention>(); |
| 96 | + _services.TryAddSingleton<IResourceGraphBuilder, ResourceGraphBuilder>(); |
| 97 | + _services.TryAddSingleton<IServiceDiscoveryFacade>(sp => new ServiceDiscoveryFacade(_services, sp.GetRequiredService<IResourceGraphBuilder>())); |
| 98 | + _services.TryAddScoped<IJsonApiExceptionFilterProvider, JsonApiExceptionFilterProvider>(); |
| 99 | + _services.TryAddScoped<IJsonApiTypeMatchFilterProvider, JsonApiTypeMatchFilterProvider>(); |
| 100 | + } |
| 101 | + |
| 102 | + public void ConfigureServices() |
| 103 | + { |
| 104 | + var graph = _resourceGraphBuilder.Build(); |
| 105 | + |
| 106 | + if (!_usesDbContext) |
| 107 | + { |
| 108 | + _services.AddScoped<DbContext>(); |
| 109 | + _services.AddSingleton(new DbContextOptionsBuilder().Options); |
| 110 | + } |
| 111 | + |
| 112 | + _services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>)); |
| 113 | + _services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>)); |
| 114 | + |
| 115 | + _services.AddScoped(typeof(IEntityReadRepository<,>), typeof(DefaultEntityRepository<,>)); |
| 116 | + _services.AddScoped(typeof(IEntityWriteRepository<,>), typeof(DefaultEntityRepository<,>)); |
| 117 | + |
| 118 | + _services.AddScoped(typeof(ICreateService<>), typeof(EntityResourceService<>)); |
| 119 | + _services.AddScoped(typeof(ICreateService<,>), typeof(EntityResourceService<,>)); |
| 120 | + |
| 121 | + _services.AddScoped(typeof(IGetAllService<>), typeof(EntityResourceService<>)); |
| 122 | + _services.AddScoped(typeof(IGetAllService<,>), typeof(EntityResourceService<,>)); |
| 123 | + |
| 124 | + _services.AddScoped(typeof(IGetByIdService<>), typeof(EntityResourceService<>)); |
| 125 | + _services.AddScoped(typeof(IGetByIdService<,>), typeof(EntityResourceService<,>)); |
| 126 | + |
| 127 | + _services.AddScoped(typeof(IGetRelationshipService<,>), typeof(EntityResourceService<>)); |
| 128 | + _services.AddScoped(typeof(IGetRelationshipService<,>), typeof(EntityResourceService<,>)); |
| 129 | + |
| 130 | + _services.AddScoped(typeof(IUpdateService<>), typeof(EntityResourceService<>)); |
| 131 | + _services.AddScoped(typeof(IUpdateService<,>), typeof(EntityResourceService<,>)); |
| 132 | + |
| 133 | + _services.AddScoped(typeof(IDeleteService<>), typeof(EntityResourceService<>)); |
| 134 | + _services.AddScoped(typeof(IDeleteService<,>), typeof(EntityResourceService<,>)); |
| 135 | + |
| 136 | + _services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>)); |
| 137 | + _services.AddScoped(typeof(IResourceService<,>), typeof(EntityResourceService<,>)); |
| 138 | + |
| 139 | + _services.AddSingleton<ILinksConfiguration>(JsonApiOptions); |
| 140 | + _services.AddSingleton(graph); |
| 141 | + _services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); |
| 142 | + _services.AddSingleton<IContextEntityProvider>(graph); |
| 143 | + _services.AddScoped<ICurrentRequest, CurrentRequest>(); |
| 144 | + _services.AddScoped<IScopedServiceProvider, RequestScopedServiceProvider>(); |
| 145 | + _services.AddScoped<IJsonApiWriter, JsonApiWriter>(); |
| 146 | + _services.AddScoped<IJsonApiReader, JsonApiReader>(); |
| 147 | + _services.AddScoped<IGenericProcessorFactory, GenericProcessorFactory>(); |
| 148 | + _services.AddScoped(typeof(GenericProcessor<>)); |
| 149 | + _services.AddScoped<IQueryParameterDiscovery, QueryParameterDiscovery>(); |
| 150 | + _services.AddScoped<ITargetedFields, TargetedFields>(); |
| 151 | + _services.AddScoped<IFieldsExplorer, FieldsExplorer>(); |
| 152 | + _services.AddScoped<IResourceDefinitionProvider, ResourceDefinitionProvider>(); |
| 153 | + _services.AddScoped<IFieldsToSerialize, FieldsToSerialize>(); |
| 154 | + _services.AddScoped<IQueryParameterActionFilter, QueryParameterActionFilter>(); |
| 155 | + |
| 156 | + AddServerSerialization(); |
| 157 | + AddQueryParameterServices(); |
| 158 | + if (JsonApiOptions.EnableResourceHooks) |
| 159 | + AddResourceHooks(); |
| 160 | + |
| 161 | + _services.AddScoped<IInverseRelationships, InverseRelationships>(); |
| 162 | + } |
| 163 | + |
| 164 | + |
| 165 | + private void AddQueryParameterServices() |
| 166 | + { |
| 167 | + _services.AddScoped<IIncludeService, IncludeService>(); |
| 168 | + _services.AddScoped<IFilterService, FilterService>(); |
| 169 | + _services.AddScoped<ISortService, SortService>(); |
| 170 | + _services.AddScoped<ISparseFieldsService, SparseFieldsService>(); |
| 171 | + _services.AddScoped<IPageService, PageService>(); |
| 172 | + _services.AddScoped<IOmitDefaultService, OmitDefaultService>(); |
| 173 | + _services.AddScoped<IOmitNullService, OmitNullService>(); |
| 174 | + |
| 175 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<IIncludeService>()); |
| 176 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<IFilterService>()); |
| 177 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<ISortService>()); |
| 178 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<ISparseFieldsService>()); |
| 179 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<IPageService>()); |
| 180 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<IOmitDefaultService>()); |
| 181 | + _services.AddScoped<IQueryParameterService>(sp => sp.GetService<IOmitNullService>()); |
| 182 | + } |
| 183 | + |
| 184 | + |
| 185 | + private void AddResourceHooks() |
| 186 | + { |
| 187 | + _services.AddSingleton(typeof(IHooksDiscovery<>), typeof(HooksDiscovery<>)); |
| 188 | + _services.AddScoped(typeof(IResourceHookContainer<>), typeof(ResourceDefinition<>)); |
| 189 | + _services.AddTransient(typeof(IResourceHookExecutor), typeof(ResourceHookExecutor)); |
| 190 | + _services.AddTransient<IHookExecutorHelper, HookExecutorHelper>(); |
| 191 | + _services.AddTransient<ITraversalHelper, TraversalHelper>(); |
| 192 | + } |
| 193 | + |
| 194 | + private void AddServerSerialization() |
| 195 | + { |
| 196 | + _services.AddScoped<IIncludedResourceObjectBuilder, IncludedResourceObjectBuilder>(); |
| 197 | + _services.AddScoped<IJsonApiDeserializer, RequestDeserializer>(); |
| 198 | + _services.AddScoped<IResourceObjectBuilderSettingsProvider, ResourceObjectBuilderSettingsProvider>(); |
| 199 | + _services.AddScoped<IJsonApiSerializerFactory, ResponseSerializerFactory>(); |
| 200 | + _services.AddScoped<ILinkBuilder, LinkBuilder>(); |
| 201 | + _services.AddScoped(typeof(IMetaBuilder<>), typeof(MetaBuilder<>)); |
| 202 | + _services.AddScoped(typeof(ResponseSerializer<>)); |
| 203 | + _services.AddScoped(sp => sp.GetRequiredService<IJsonApiSerializerFactory>().GetSerializer()); |
| 204 | + _services.AddScoped<IResourceObjectBuilder, ResponseResourceObjectBuilder>(); |
| 205 | + } |
| 206 | + } |
| 207 | +} |
0 commit comments