Esempio n. 1
0
        /// <summary>
        /// Configures built-in .NET Core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers' need.
        /// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
        /// <see cref="IResourceGraphBuilder"/>, <see cref="IServiceDiscoveryFacade"/>, <see cref="IJsonApiExceptionFilterProvider"/>,
        /// <see cref="IJsonApiTypeMatchFilterProvider"/> and <see cref="IJsonApiRoutingConvention"/>.
        /// </summary>
        public void ConfigureMvc(Type dbContextType)
        {
            RegisterJsonApiStartupServices();

            var intermediateProvider = _services.BuildServiceProvider();

            _resourceGraphBuilder   = intermediateProvider.GetRequiredService <IResourceGraphBuilder>();
            _serviceDiscoveryFacade = intermediateProvider.GetRequiredService <IServiceDiscoveryFacade>();
            _dbContextType          = dbContextType;

            AddResourceTypesFromDbContext(intermediateProvider);

            var exceptionFilterProvider = intermediateProvider.GetRequiredService <IJsonApiExceptionFilterProvider>();
            var typeMatchFilterProvider = intermediateProvider.GetRequiredService <IJsonApiTypeMatchFilterProvider>();
            var routingConvention       = intermediateProvider.GetRequiredService <IJsonApiRoutingConvention>();

            _mvcBuilder.AddMvcOptions(options =>
            {
                options.EnableEndpointRouting = true;
                options.Filters.Add(exceptionFilterProvider.Get());
                options.Filters.Add(typeMatchFilterProvider.Get());
                options.Filters.Add(new ConvertEmptyActionResultFilter());
                options.InputFormatters.Insert(0, new JsonApiInputFormatter());
                options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
                options.Conventions.Insert(0, routingConvention);
            });

            if (_options.ValidateModelState)
            {
                _mvcBuilder.AddDataAnnotations();
            }

            _services.AddSingleton <IControllerResourceMapping>(routingConvention);
        }
Esempio n. 2
0
 public ServiceDiscoveryFacade(
     IServiceCollection services,
     IResourceGraphBuilder graphBuilder)
 {
     _services     = services;
     _graphBuilder = graphBuilder;
 }
        /// <summary>
        /// Add all the models that are part of the provided <see cref="DbContext" />
        /// that also implement <see cref="IIdentifiable"/>
        /// </summary>
        /// <typeparam name="TDbContext">The <see cref="DbContext"/> implementation type.</typeparam>
        public static IResourceGraphBuilder AddDbContext <TDbContext>(this IResourceGraphBuilder resourceGraphBuilder) where TDbContext : DbContext
        {
            var builder           = (ResourceGraphBuilder)resourceGraphBuilder;
            var contextType       = typeof(TDbContext);
            var contextProperties = contextType.GetProperties();

            foreach (var property in contextProperties)
            {
                var dbSetType = property.PropertyType;
                if (dbSetType.IsGenericType &&
                    dbSetType.GetGenericTypeDefinition() == typeof(DbSet <>))
                {
                    var resourceType = dbSetType.GetGenericArguments()[0];
                    builder.AddResource(resourceType, pluralizedTypeName: GetResourceNameFromDbSetProperty(property, resourceType));
                }
            }
            return(resourceGraphBuilder);
        }
Esempio n. 4
0
        /// <summary>
        /// Configures built-in .net core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers need.
        /// Before calling .AddJsonApi(), a developer can register their own implementation of the following services to customize startup:
        /// <see cref="IResourceGraphBuilder"/>, <see cref="IServiceDiscoveryFacade"/>, <see cref="IJsonApiExceptionFilterProvider"/>,
        /// <see cref="IJsonApiTypeMatchFilterProvider"/>, <see cref="IJsonApiRoutingConvention"/> and <see cref="IResourceNameFormatter"/>.
        /// </summary>
        public void ConfigureMvc()
        {
            RegisterJsonApiStartupServices();

            var intermediateProvider = _services.BuildServiceProvider();

            _resourceGraphBuilder   = intermediateProvider.GetRequiredService <IResourceGraphBuilder>();
            _serviceDiscoveryFacade = intermediateProvider.GetRequiredService <IServiceDiscoveryFacade>();
            var exceptionFilterProvider = intermediateProvider.GetRequiredService <IJsonApiExceptionFilterProvider>();
            var typeMatchFilterProvider = intermediateProvider.GetRequiredService <IJsonApiTypeMatchFilterProvider>();
            var routingConvention       = intermediateProvider.GetRequiredService <IJsonApiRoutingConvention>();

            _mvcBuilder.AddMvcOptions(options =>
            {
                options.EnableEndpointRouting = true;
                options.Filters.Add(exceptionFilterProvider.Get());
                options.Filters.Add(typeMatchFilterProvider.Get());
                options.InputFormatters.Insert(0, new JsonApiInputFormatter());
                options.OutputFormatters.Insert(0, new JsonApiOutputFormatter());
                options.Conventions.Insert(0, routingConvention);
            });
            _services.AddSingleton <IControllerResourceMapping>(routingConvention);
        }
Esempio n. 5
0
 protected virtual void SetupResourceGraph(IResourceGraphBuilder builder)
 {
 }
Esempio n. 6
0
 protected override void SetupResourceGraph(IResourceGraphBuilder builder)
 {
     builder.AddResource <UserResource, string>("users");
 }
Esempio n. 7
0
 public ServiceDiscoveryFacade(IServiceCollection services, IResourceGraphBuilder resourceGraphBuilder)
 {
     _services             = services ?? throw new ArgumentNullException(nameof(services));
     _resourceGraphBuilder = resourceGraphBuilder ?? throw new ArgumentNullException(nameof(resourceGraphBuilder));
 }