//public IEdmModel Configure(IApplicationBuilder app)
        //{
        //    ODataModelBuilder modelBuilder;
        //    return Configure(app, out modelBuilder);
        //}

        //public IEdmModel Configure(IApplicationBuilder app, out ODataModelBuilder modelBuilder)
        //{
        //    return Configure(app, out modelBuilder);
        //}

        public IEdmModel Configure(IAssemblyProvider assemblyProvider,
                                   IServiceProvider serviceProvider, out ODataModelBuilder modelBuilder)
        {
            // OData actions are HTTP POST
            // OData functions are HTTP GET
            var builder = new ODataConventionModelBuilder(assemblyProvider);

            builder.Namespace = Namespace;

            var serviceType     = typeof(TService);
            var entitySetMethod = builder.GetType().GetMethod(nameof(ODataConventionModelBuilder.EntitySet));

            foreach (var property in serviceType.GetProperties())
            {
                if (typeof(IQueryable).IsAssignableFrom(property.PropertyType) && property.PropertyType.IsGenericType)
                {
                    entitySetMethod.MakeGenericMethod(property.PropertyType.GenericTypeArguments[0])
                    .Invoke(builder, new object[] { property.Name });
                }
            }

            // Get the actions and functions into the model
            ApplyAttributes(assemblyProvider, builder);

            ApplyCustomConfigurators(serviceProvider, builder);

            var model = builder.GetEdmModel() as EdmModel;

            modelBuilder = builder;
            return(model);
        }
        private static void BuildContainer(IContainerBuilder container, ApplicationConfiguration appConfig, HttpConfiguration config)
        {
            container.AddService(ServiceLifetime.Singleton, sp => appConfig);
            container.AddService <ODataUriResolver>(ServiceLifetime.Singleton, sp => new UnqualifiedODataUriResolver());

            container.AddService(ServiceLifetime.Singleton, sp =>
            {
                var conventions = ODataRoutingConventions.CreateDefaultWithAttributeRouting("odata", config);
                conventions.Insert(0, new UnboundFunctionsConvention());
                //conventions.Insert(0, new AnyNavigationRoutingConvension());
                conventions.Insert(0, new SingleEntityRoutingConvention());
                return(conventions.AsEnumerable());
            });

            var controllers = appConfig.ConfigureModel.Method.DeclaringType.Assembly.ExportedTypes
                              .Where(t => !t.IsAbstract)
                              .Where(typeof(ODataController).IsAssignableFrom).ToList();

            container.AddService <MetadataController>(ServiceLifetime.Transient);
            container.AddService <ExtendedMetadataController>(ServiceLifetime.Transient);

            container.AddService <FilterBinder, ExtendedFilterBinder>(ServiceLifetime.Singleton);
            ExtendedFilterBinder.RegisterCustomFunctions();

            var builder = new ODataConventionModelBuilder();

            builder.Namespace = typeof(Service).Namespace;
            builder.EnableLowerCamelCase();
            builder.ContainerName = "DefaultContainer";
            var entitySetFactory = builder.GetType().GetMethod("EntitySet");

            foreach (var controller in controllers)
            {
                container.AddService(ServiceLifetime.Transient, controller);

                var modelType = GetControllerBaseModel(controller);

                if (modelType != null)
                {
                    var setName = controller.Name.Replace("Controller", "");
                    entitySetFactory.MakeGenericMethod(modelType).Invoke(builder, new object[] { setName });
                }
            }



            builder.Function(Constants.ValidationFunctionName).Returns <ValidationEntityDocument>();
            //builder.Function(Constants.GenerateFunctionName).Returns<IHttpActionResult>();
            appConfig.ConfigureModel(builder);
            appConfig.Register(container);

            var model = builder.GetEdmModel();

            container.AddService(ServiceLifetime.Singleton, sp => model);
        }
Exemplo n.º 3
0
        private IEdmModel GetEdmModel()
        {
            ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

            // Register entities which has GeneratedController attribute
            MethodInfo registerMethod = builder.GetType().GetMethod("EntitySet", new Type[] { typeof(String) });

            var currentAssembly = typeof(Startup).Assembly;
            var candidates      = currentAssembly.GetExportedTypes().Where(x => x.GetCustomAttributes <Helpers.GeneratedControllerAttribute>().Any());

            foreach (var candidate in candidates)
            {
                MethodInfo genericMethod = registerMethod.MakeGenericMethod(candidate);
                genericMethod.Invoke(builder, new object[] { candidate.Name });
            }

            var model = builder.GetEdmModel();

            var converter    = new ODataSwaggerConverter(model);
            var swaggerModel = converter.GetSwaggerModel();
            var swaggerPaths = (JObject)swaggerModel.GetValue("paths");

            List <JProperty> childrenToRemove = new List <JProperty>();

            foreach (var property in swaggerPaths.Properties())
            {
                var path = property.Value <JToken>();
                var obj  = (JObject)path.FirstOrDefault();
                foreach (var child in obj.Children <JProperty>())
                {
                    if (child.Name == "patch")
                    {
                        childrenToRemove.Add(child);
                    }
                }
            }

            childrenToRemove.ForEach((child) =>
            {
                child.Remove();
            });

            var swaggerPath = Path.Combine(JsonPath, "swagger.json");

            File.WriteAllText(swaggerPath, converter.GetSwaggerModel().ToString(), Encoding.UTF8);

            return(model);
        }
Exemplo n.º 4
0
        //public IEdmModel Configure(IApplicationBuilder app)
        //{
        //    ODataModelBuilder modelBuilder;
        //    return Configure(app, out modelBuilder);
        //}

        //public IEdmModel Configure(IApplicationBuilder app, out ODataModelBuilder modelBuilder)
        //{
        //    return Configure(app, out modelBuilder);
        //}

        public IEdmModel Configure(IAssemblyProvider assemblyProvider, IServiceProvider serviceProvider, out ODataModelBuilder modelBuilder)
        {
            // OData actions are HTTP POST
            // OData functions are HTTP GET
            var builder = new ODataConventionModelBuilder(assemblyProvider);

            builder.Namespace = Namespace;

            var serviceType     = typeof(TService);
            var entitySetMethod = builder.GetType().GetMethod(nameof(ODataConventionModelBuilder.EntitySet));

            foreach (var property in serviceType.GetProperties())
            {
                if (typeof(IQueryable).IsAssignableFrom(property.PropertyType) && property.PropertyType.IsGenericType)
                {
                    entitySetMethod.MakeGenericMethod(property.PropertyType.GenericTypeArguments[0])
                    .Invoke(builder, new object[] { property.Name });
                }
            }
            var configurators = new List <IODataEntitySetConfigurator>();
            var types         = ConfigurationAssembly.DefinedTypes;

            foreach (var type in types)
            {
                if (typeof(IODataEntitySetConfigurator).IsAssignableFrom(type) && !type.IsAbstract && !type.IsInterface)
                {
                    configurators.Add((IODataEntitySetConfigurator)ActivatorUtilities.CreateInstance(serviceProvider, type));
                }
            }
            foreach (var configurator in configurators)
            {
                configurator.Configure(builder, action => ModelConfigurators.Add(action));
            }

            var model = builder.GetEdmModel() as EdmModel;

            foreach (var modelConfigurator in ModelConfigurators)
            {
                modelConfigurator(model);
            }
            modelBuilder = builder;
            return(model);
        }