Example #1
0
        public static IServiceCollection AddApplicationServices(this IServiceCollection services)
        {
            services.AddScoped <IProductRepos, ProductRepository>();
            services.AddScoped(typeof(IGenericRepository <>), typeof(GenericRepository <>));
            services.AddScoped <IOrderService, OrderService>();
            services.AddScoped <IPaymentService, PaymentService>();
            services.AddScoped <IUnitOfWork, UnitOfWork>();
            services.AddAutoMapper(typeof(MappingProfiles));
            services.AddScoped <IBasketRepository, BasketRepository>();
            // this configure must placed after services.AddControllers();
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = actionContext =>
                {
                    var errors = actionContext.ModelState
                                 .Where(e => e.Value.Errors.Count > 0)
                                 .SelectMany(x => x.Value.Errors)
                                 .Select(x => x.ErrorMessage).ToArray();

                    var errorResponse = new APiValidationErrorResponse()
                    {
                        Errors = errors
                    };
                    return(new BadRequestObjectResult(errorResponse));// new response
                };
            });

            return(services);
        }
        public static IServiceCollection AddApplicationServices(this IServiceCollection services)
        {
            services.AddScoped <IProductRepository, ProductRepository>();
            services.AddScoped(typeof(IGenericRepository <>), (typeof(GenericRepository <>)));

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = actionContext =>
                {
                    var errors = actionContext.ModelState
                                 .Where(e => e.Value.Errors.Count > 0)
                                 .SelectMany(x => x.Value.Errors)
                                 .Select(x => x.ErrorMessage).ToArray();

                    var errorResponse = new APiValidationErrorResponse
                    {
                        Errors = errors
                    };

                    return(new BadRequestObjectResult(errorResponse));
                };
            });

            return(services);
        }