Ejemplo n.º 1
0
        /// <summary>
        /// Configuración de los servicios web
        /// </summary>
        /// <param name="container">Contenedor IoC</param>
        public override void Configure( Container container )
        {
            // JSON
            JsConfig.EmitCamelCaseNames = true;
            JsConfig.IncludeNullValues = false;
            JsConfig.DateHandler = DateHandler.ISO8601;
            JsConfig.EscapeUnicode = true;
            //JsConfig<LinkRelationType>.SerializeFn = text => text.ToString().ToCamelCase();
            //JsConfig<RelationType>.SerializeFn = text => text.ToString().ToCamelCase();

            // ServiceStack
            SetConfig(new HostConfig
            {
                DefaultContentType = MimeTypes.Json,
                DebugMode = true,
                WebHostUrl = TestConfig.AbsoluteBaseUri.ToString()
            });

            // Plugins
            var queryPlugin = new QueryLanguageFeature();
            queryPlugin.RegisterAssociation(typeof(Customer), typeof(CustomerEntity));
            queryPlugin.RegisterAssociation(typeof(GetCustomers), typeof(CustomerEntity));
            queryPlugin.RegisterAssociation(typeof(Order), typeof(OrderEntity));
            queryPlugin.RegisterAssociation(typeof(GetOrders), typeof(OrderEntity));

            Plugins.Add(queryPlugin as IPlugin);
            Plugins.Add(new ValidationFeature());
            Plugins.Add(new CorsFeature());
            Plugins.Add(new AuthFeature(() => new NorthwindUserSession(),
                new IAuthProvider[]
                {
                    new NorthwindAuthProvider(),
                })
            {
                HtmlRedirect = null
            });

            // Validaciones
            container.RegisterValidators(typeof(CustomerValidator).Assembly);

            // Caché
            container.Register<ICacheClient>(new MemoryCacheClient());

            // Acceso a datos
            var dbFactory = new OrmLiteConnectionFactory(":memory:", /*false,*/ SqliteDialect.Provider);

            //container.Register<IDbConnectionFactory>(dbFactory);
            container.Register(dbFactory);

            using ( var db = dbFactory.OpenDbConnection() )
            {
                NorthwindData.LoadData();

                db.CreateTables(false, NorthwindFactory.ModelTypes.ToArray());
                db.InsertAll(NorthwindData.Categories);
                db.InsertAll(NorthwindData.Customers);
                db.InsertAll(NorthwindData.OrderDetails);
                db.InsertAll(NorthwindData.Orders);
                db.InsertAll(NorthwindData.Products);
                db.InsertAll(NorthwindData.Regions);
                db.InsertAll(NorthwindData.Shippers);
                db.InsertAll(NorthwindData.Suppliers);
                //db.InsertAll(NorthwindData.Territories);
                db.InsertAll(NorthwindData.Users);
            }

            // Dependencias
            //container.RegisterAs<CategoryEntityRepository, ICategoryEntityRepository>();
            //container.RegisterAs<CustomerEntityRepository, ICustomerEntityRepository>();
            //container.RegisterAs<EmployeeEntityRepository, IEmployeeEntityRepository>();
            //container.RegisterAs<OrderEntityRepository, IOrderEntityRepository>();
            //container.RegisterAs<OrderDetailEntityRepository, IOrderDetailEntityRepository>();
            //container.RegisterAs<ProductEntityRepository, IProductEntityRepository>();
            //container.RegisterAs<ShipperEntityRepository, IShipperEntityRepository>();
            //container.RegisterAs<SupplierEntityRepository, ISupplierEntityRepository>();
            //container.RegisterAs<RegionEntityRepository, IRegionEntityRepository>();
            //container.RegisterAs<TerritoryEntityRepository, ITerritoryEntityRepository>();

            container.Register<ICustomerEntityRepository>(c => new CustomerEntityRepository(dbFactory)
            {
                OrderRepository = new OrderEntityRepository(dbFactory)
            });

            container.Register<IOrderEntityRepository>(c => new OrderEntityRepository(dbFactory));
            container.Register<IUserEntityRepository>(c => new UserEntityRepository(dbFactory));

            //container.RegisterAs<CategoryEntityRepository, IRepository<CategoryEntity>>();
            //container.RegisterAs<CustomerEntityRepository, IRepository<CustomerEntity>>();
            //container.RegisterAs<EmployeeEntityRepository, IRepository<EmployeeEntity>>();
            //container.RegisterAs<OrderEntityRepository, IRepository<OrderEntity>>();
            //container.RegisterAs<OrderDetailEntityRepository, IRepository<OrderDetailEntity>>();
            //container.RegisterAs<ProductEntityRepository, IRepository<ProductEntity>>();
            //container.RegisterAs<ShipperEntityRepository, IRepository<ShipperEntity>>();
            //container.RegisterAs<SupplierEntityRepository, IRepository<SupplierEntity>>();
            //container.RegisterAs<RegionEntityRepository, IRepository<RegionEntity>>();
            //container.RegisterAs<TerritoryEntityRepository, IRepository<TerritoryEntity>>();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Configuración de los servicios web
        /// </summary>
        /// <param name="container">Contenedor IoC</param>
        public override void Configure( Container container )
        {
            // Configuración JSON
            JsConfig.EmitCamelCaseNames = true;
            JsConfig.IncludeNullValues = false;
            JsConfig.DateHandler = DateHandler.ISO8601;
            JsConfig.EscapeUnicode = true;
            //JsConfig<LinkRelationType>.SerializeFn = (text => text.ToString().ToLower());

            // Configuración de ServiceStack
            SetConfig(new HostConfig
            {
                DefaultContentType = MimeTypes.Json,
                DebugMode = true,
                ReturnsInnerException = true,
                WebHostUrl = "http://localhost:2828"/*HttpContext.Current.Request.Url.ToString()*/
            });

            // Plugins
            var queryPlugin = new QueryLanguageFeature();
            queryPlugin.RegisterAssociation(typeof(Customer), typeof(CustomerEntity));
            queryPlugin.RegisterAssociation(typeof(GetCustomers), typeof(CustomerEntity));
            queryPlugin.RegisterAssociation(typeof(Order), typeof(OrderEntity));
            queryPlugin.RegisterAssociation(typeof(GetOrders), typeof(OrderEntity));

            Plugins.Add(queryPlugin as IPlugin);
            Plugins.Add(new ValidationFeature());
            Plugins.Add(new SwaggerFeature());
            Plugins.Add(new RazorFormat());
            Plugins.Add(new CorsFeature());
            Plugins.Add(new AuthFeature(() => new NorthwindUserSession(),
                new IAuthProvider[]
                {
                    new NorthwindAuthProvider(),
                })
            {
                HtmlRedirect = null
            });

            // Caché
            container.Register(new MemoryCacheClient());

            // Acceso a datos
            var dbFactory = new OrmLiteConnectionFactory("~/Northwind.sqlite".MapHostAbsolutePath(), SqliteDialect.Provider);

            container.Register(dbFactory);

            container.Register<ICustomerEntityRepository>(c => new CustomerEntityRepository(dbFactory));
            container.Register<IOrderEntityRepository>(c => new OrderEntityRepository(dbFactory));
            container.Register<IUserEntityRepository>(c => new UserEntityRepository(dbFactory));

            container.RegisterAutoWired<CustomersService>();
        }