Ejemplo n.º 1
0
        public void WrapperSuccess_MoreCustomerModel()
        {
            var originalData = "dududu";

            CustomWrapperModel customWrapperModel = new CustomWrapperModel();

            customWrapperModel.AddProperty("Name", s => "Name");
            customWrapperModel.AddProperty("Person", s => "Person");
            customWrapperModel.AddProperty("Old", s => "1");

            CustomWrapperModel customWrapperModel2 = new CustomWrapperModel();

            customWrapperModel2.AddProperty("DiDiDi", s => "DiDiDi");

            var costomConfigs = new Dictionary <Range, CustomWrapperModel>();

            costomConfigs.Add(200..201, customWrapperModel);
            costomConfigs.Add(300..401, customWrapperModel2);

            DataWrapperOptions options = new DataWrapperOptions()
            {
                UseCustomModel    = true,
                CustomModelConfig = costomConfigs
            };
            DataWrapperContext context = new DataWrapperContext(new ObjectResult(originalData), CreateFakeHttpContext("Get", 200), options);
            var result = _dataWrapperExecutor.WrapSuccesfullysResult(originalData, context);

            Assert.NotNull(result.GetType().GetProperty("Name").Name);

            DataWrapperContext context2 = new DataWrapperContext(new ObjectResult(originalData), CreateFakeHttpContext("Get", 300), options);
            var result2 = _dataWrapperExecutor.WrapSuccesfullysResult(originalData, context2);

            Assert.NotNull(result2.GetType().GetProperty("DiDiDi").Name);
        }
Ejemplo n.º 2
0
        public void WrapperSuccess_OneCustomerModel_InRange()
        {
            var originalData = "dududu";

            CustomWrapperModel customWrapperModel = new CustomWrapperModel();

            customWrapperModel.AddProperty("Name", s => "Name");
            customWrapperModel.AddProperty("Person", s => "Person");
            customWrapperModel.AddProperty("Old", s => "1");

            var costomConfigs = new Dictionary <Range, CustomWrapperModel>();

            costomConfigs.Add(200..300, customWrapperModel);

            DataWrapperOptions options = new DataWrapperOptions()
            {
                UseCustomModel    = true,
                CustomModelConfig = costomConfigs
            };
            DataWrapperContext context = new DataWrapperContext(new ObjectResult(originalData), CreateFakeHttpContext("Get", 200), options);
            var result = _dataWrapperExecutor.WrapSuccesfullysResult(originalData, context);

            Assert.NotNull(result);
            Assert.IsAssignableFrom <IResultDataWrapper>(result);
        }
Ejemplo n.º 3
0
        private static CustomWrapperModel CreateCustomModel()
        {
            CustomWrapperModel result = new CustomWrapperModel("MiCakeCustomModel");

            result.AddProperty("company", s => "MiCake");
            result.AddProperty("statusCode", s => (s.ResultData as ObjectResult).StatusCode ?? s.HttpContext.Response.StatusCode);
            result.AddProperty("result", s => (s.ResultData as ObjectResult).Value);

            return(result);
        }
Ejemplo n.º 4
0
        private Type CreateCustomEmitType(CustomWrapperModel customWrapperModel)
        {
            var dyClass = EmitHelper.CreateClass(customWrapperModel.ModelName,
                                                 MiCakeReflectionPredefined.DynamicAssembly,
                                                 MiCakeReflectionPredefined.DynamicModule,
                                                 baseType: typeof(BaseResultDataWrapper));

            foreach (var customerProperty in customWrapperModel.GetAllConfigProperties())
            {
                CheckValue.NotNullOrEmpty(customerProperty.Key, nameof(customerProperty.Key));
                EmitHelper.CreateProperty(dyClass, customerProperty.Key, typeof(object));
            }

            return(dyClass.CreateType());
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create customModel instance,and given value by config.
        /// </summary>
        /// <param name="wrapperContext"><see cref="DataWrapperContext"/></param>
        /// <returns>customModel instance.</returns>
        protected virtual object CreateCustomModel(DataWrapperContext wrapperContext)
        {
            var currentHttpCode = wrapperContext.HttpContext.Response.StatusCode;

            if (_cacheCustomerType.TryGetValue(currentHttpCode, out var resultConfigInfo))
            {
                return(AssignValueToCustomType(resultConfigInfo, wrapperContext));
            }

            var customModels = wrapperContext.WrapperOptions.CustomModelConfig;

            CustomWrapperModel optimizationCustomModel = null;

            foreach (var customModel in customModels)
            {
                if (customModel.Key.IsInRange(currentHttpCode))
                {
                    optimizationCustomModel = customModel.Value;
                    break;
                }
            }

            if (optimizationCustomModel == null)
            {
                //If it is not empty, it means that there is no configuration,
                //then when the status code is encountered in the future, the source data will be returned
                _cacheCustomerType.TryAdd(currentHttpCode, null);
                return(null);
            }

            var emitType        = CreateCustomEmitType(optimizationCustomModel);
            var customModelInfo = new CustomModelWithType(optimizationCustomModel, emitType);

            _cacheCustomerType.TryAdd(currentHttpCode, customModelInfo);

            return(AssignValueToCustomType(customModelInfo, wrapperContext));
        }
Ejemplo n.º 6
0
 public CustomModelWithType(CustomWrapperModel customModel, Type emitType)
 {
     ModelConfig = customModel;
     EmitType    = emitType;
 }