コード例 #1
0
        public static void SetupHttpConfigurationUsingAutofac(this IJsonApiConfiguration configuration,
                                                              HttpConfiguration httpConfiguration, ILifetimeScope parentLifetimeScope)
        {
            var configurator = new JsonApiHttpAutofacConfigurator(parentLifetimeScope);

            configurator.Apply(httpConfiguration, configuration);
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: BradBarnich/JSONAPI.NET
        public void Configuration(IAppBuilder app)
        {
            var configuration = new JsonApiConfiguration();
            configuration.RegisterEntityFrameworkResourceType<Building>();
            configuration.RegisterEntityFrameworkResourceType<City>();
            configuration.RegisterEntityFrameworkResourceType<Comment>();
            configuration.RegisterEntityFrameworkResourceType<Company>();
            configuration.RegisterEntityFrameworkResourceType<Language>();
            configuration.RegisterEntityFrameworkResourceType<LanguageUserLink>(c =>
            {
                c.OverrideDefaultFilterById(LanguageUserLinkFilterByIdFactory);
                c.OverrideDefaultSortById(LanguageUserLinkSortByIdFactory);
            });
            configuration.RegisterResourceType<Post>();
            configuration.RegisterEntityFrameworkResourceType<State>();
            configuration.RegisterEntityFrameworkResourceType<Tag>();
            configuration.RegisterEntityFrameworkResourceType<User>();
            configuration.RegisterEntityFrameworkResourceType<UserGroup>();
            configuration.RegisterResourceType<Sample>(); // Example of a resource type not controlled by EF
            configuration.RegisterMappedType<Starship, StarshipDto, StarshipDocumentMaterializer>(c =>
            {
                c.ConfigureRelationship(s => s.Officers,
                    rc => rc.UseMaterializer<StarshipOfficersRelatedResourceMaterializer>());
                c.ConfigureRelationship(s => s.ShipCounselor,
                    rc => rc.UseMaterializer<StarshipShipCounselorRelatedResourceMaterializer>());
            }); // Example of a resource that is mapped from a DB entity
            configuration.RegisterResourceType<StarshipOfficerDto>();

            var configurator = new JsonApiHttpAutofacConfigurator();
            configurator.OnApplicationLifetimeScopeCreating(builder =>
            {
                builder.Register(c => _dbContextFactory())
                    .AsSelf()
                    .As<DbContext>()
                    .InstancePerRequest();
                builder.RegisterModule<JsonApiAutofacEntityFrameworkModule>();
                builder.RegisterType<CustomEntityFrameworkResourceObjectMaterializer>()
                    .As<IEntityFrameworkResourceObjectMaterializer>();
                builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
            });
            configurator.OnApplicationLifetimeScopeBegun(applicationLifetimeScope =>
            {
                // TODO: is this a candidate for spinning into a JSONAPI.Autofac.WebApi.Owin package? Yuck
                app.UseAutofacMiddleware(applicationLifetimeScope);
            });

            var httpConfig = new HttpConfiguration
            {
                IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always
            };

            // Additional Web API routes
            httpConfig.Routes.MapHttpRoute("Samples", "samples", new { Controller = "Samples" });
            httpConfig.Routes.MapHttpRoute("Search", "search", new { Controller = "Search" });
            httpConfig.Routes.MapHttpRoute("Trees", "trees", new { Controller = "Trees" });

            configurator.Apply(httpConfig, configuration);
            app.UseWebApi(httpConfig);
            app.UseAutofacWebApi(httpConfig);
        }