/// <summary> /// 测试获取某个具体的配置. /// </summary> /// <param name="configTypeCode"></param> /// <param name="configCode"></param> /// <returns></returns> public JsonResult GetConfig(string configTypeCode, string configCode) { SystemConfigValue data = this.systemConfigService.GetSystemConfigValue(configTypeCode, configCode); if (data == null) { var errResult = new { Code = data.ConfigCode, Name = "DATA NOT FOUND!" }; return(Json(errResult, JsonRequestBehavior.AllowGet)); } var result = new { Code = data.ConfigCode, Name = data.ConfigName, Config = data.ConfigValueObject }; return(Json(result, JsonRequestBehavior.AllowGet)); }
public void TestObject() { SystemConfigValue configValue1 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_OBJ", // 配置代码 ConfigCode = "TEST1", // 配置名称. ConfigName = "测试项目1", // 配置数值. ConfigValueObject = new MyTestUser() { Name = "张三", Age = 25, Address = "北京" }, }; SystemConfigValue configValue2 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_OBJ", // 配置代码 ConfigCode = "TEST2", // 配置名称. ConfigName = "测试项目2", // 配置数值. ConfigValueObject = new MyTestUser() { Name = "李四", Age = 30, Address = "上海" }, }; // 先更新. string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(configValue1, ref resultMsg); Assert.IsTrue(result); result = this.systemConfigService.UpdateSystemConfigValue(configValue2, ref resultMsg); Assert.IsTrue(result); // 后查询. var dataList = this.systemConfigService.GetSystemConfigValueByType("TEST_OBJ"); // 结果数据非空. Assert.IsNotNull(dataList); // 行数为2. Assert.AreEqual(2, dataList.Count); Assert.AreEqual(configValue1.ConfigValueObject, dataList[0].ConfigValueObject); Assert.AreEqual(configValue2.ConfigValueObject, dataList[1].ConfigValueObject); }
public ActionResult Edit(string configTypeCode, string configCode, FormCollection collection) { SystemConfigValue data = null; try { // 配置类型. SystemConfigType sct = this.systemConfigService.GetSystemConfigType(configTypeCode); ViewBag.SystemConfigType = sct; // 配置属性 List <SystemConfigProperty> scp = this.systemConfigService.GetSystemConfigPropertyByType(configTypeCode); ViewBag.SystemConfigPropertys = scp; // 数据. data = this.systemConfigService.GetSystemConfigValue(configTypeCode, configCode); if (data == null) { return(HttpNotFound()); } if (data.SystemConfigTypeData.ConfigClassName == SystemConfigType.SimpleDictionary) { Dictionary <string, Object> configObj = new Dictionary <string, object>(); foreach (var prop in scp) { String value = collection[prop.PropertyName]; configObj.Add(prop.PropertyName, BasicDataConvert(prop.PropertyDataType, value)); } data.ConfigValueObject = configObj; } else { data.ConfigValueObject = BasicDataConvert(sct.ConfigClassName, collection["ConfigValue"]); } string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(data, ref resultMsg); if (!result) { return(View(model: data)); } return(RedirectToAction("Index", new { id = data.ConfigTypeCode })); } catch { return(View(model: data)); } }
public ActionResult Create(string id, string configCode, FormCollection collection) { SystemConfigValue data = null; try { // 配置类型. SystemConfigType sct = this.systemConfigService.GetSystemConfigType(id); ViewBag.SystemConfigType = sct; // 配置属性 List <SystemConfigProperty> scp = this.systemConfigService.GetSystemConfigPropertyByType(id); ViewBag.SystemConfigPropertys = scp; data = this.systemConfigService.GetSystemConfigValue(id, configCode); if (data != null) { // 数据已存在. return(View(model: data)); } data = new SystemConfigValue() { ConfigTypeCode = id, ConfigCode = configCode, ConfigName = collection["ConfigName"], }; Dictionary <string, Object> configObj = new Dictionary <string, object>(); foreach (var prop in scp) { String value = collection[prop.PropertyName]; configObj.Add(prop.PropertyName, BasicDataConvert(prop.PropertyDataType, value)); } data.ConfigValueObject = configObj; string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(data, ref resultMsg); if (!result) { return(View(model: data)); } return(RedirectToAction("Index", new { id = data.ConfigTypeCode })); } catch { return(View(model: data)); } }
public void TestString() { SystemConfigValue configValue1 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_STRING", // 配置代码 ConfigCode = "TEST1", // 配置名称. ConfigName = "测试项目1", // 配置数值. ConfigValueObject = "Test 001", }; SystemConfigValue configValue2 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_STRING", // 配置代码 ConfigCode = "TEST2", // 配置名称. ConfigName = "测试项目2", // 配置数值. ConfigValueObject = "Test 002", }; // 先更新. string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(configValue1, ref resultMsg); Assert.IsTrue(result); result = this.systemConfigService.UpdateSystemConfigValue(configValue2, ref resultMsg); Assert.IsTrue(result); // 后查询. var dataList = this.systemConfigService.GetSystemConfigValueByType("TEST_STRING"); // 结果数据非空. Assert.IsNotNull(dataList); // 行数为2. Assert.AreEqual(2, dataList.Count); Assert.AreEqual(configValue1.ConfigValueObject, dataList[0].ConfigValueObject); Assert.AreEqual(configValue2.ConfigValueObject, dataList[1].ConfigValueObject); }
public void TestBadInput() { SystemConfigValue configValue1 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "", // 配置代码 ConfigCode = "123", // 配置名称. ConfigName = "测试类型代码为空时报错.", // 配置数值. ConfigValueObject = "Test Error 001", }; SystemConfigValue configValue2 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "123", // 配置代码 ConfigCode = "", // 配置名称. ConfigName = "测试代码为空时报错.", // 配置数值. ConfigValueObject = "Test Error 002", }; SystemConfigValue configValue3 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_NOT_EXISTS_CODE", // 配置代码 ConfigCode = "123", // 配置名称. ConfigName = "测试不存在的类型代码时报错.", // 配置数值. ConfigValueObject = "Test Error 003", }; string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(configValue1, ref resultMsg); Assert.IsFalse(result); result = this.systemConfigService.UpdateSystemConfigValue(configValue2, ref resultMsg); Assert.IsFalse(result); result = this.systemConfigService.UpdateSystemConfigValue(configValue3, ref resultMsg); Assert.IsFalse(result); }
/// <summary> /// 更新配置信息 /// </summary> /// <param name="configValue"></param> /// <param name="resultMessage"></param> /// <returns></returns> public bool UpdateSystemConfigValue(SystemConfigValue configValue, ref string resultMessage) { using (MySystemConfigContext context = new MySystemConfigContext()) { // 首先, 检查 ConfigTypeCode, ConfigCode 是否非空. if (String.IsNullOrEmpty(configValue.ConfigTypeCode)) { resultMessage = "配置类型代码不能为空."; return(false); } if (String.IsNullOrEmpty(configValue.ConfigCode)) { resultMessage = "配置代码不能为空."; return(false); } // 其次, 检查 ConfigTypeCode 是否存在. var configType = context.SystemConfigTypes.Find(configValue.ConfigTypeCode); if (configType == null) { resultMessage = String.Format("配置类型代码 {0} 不存在.", configValue.ConfigTypeCode); return(false); } // 用户数据, 以Json格式存储. if (configValue.ConfigValueObject != null) { configValue.ConfigValue = JsonConvert.SerializeObject(configValue.ConfigValueObject); } // 最后, 尝试查询数据, 判断是 插入, 还是更新. var dbConfigValue = context.SystemConfigValues.Find(configValue.ConfigTypeCode, configValue.ConfigCode); if (dbConfigValue == null) { // 插入处理. context.SystemConfigValues.Add(configValue); } else { // 更新数据. dbConfigValue.ConfigValue = configValue.ConfigValue; } context.SaveChanges(); return(true); } }
// GET: SystemConfig/Details/5 public ActionResult Details(string configTypeCode, string configCode) { // 配置类型. SystemConfigType sct = this.systemConfigService.GetSystemConfigType(configTypeCode); ViewBag.SystemConfigType = sct; // 配置属性 List <SystemConfigProperty> scp = this.systemConfigService.GetSystemConfigPropertyByType(configTypeCode); ViewBag.SystemConfigPropertys = scp; // 数据. SystemConfigValue data = this.systemConfigService.GetSystemConfigValue(configTypeCode, configCode); if (data == null) { return(HttpNotFound()); } return(View(model: data)); }
/// <summary> /// 取得系统配置数值 /// </summary> /// <param name="configTypeCode"></param> /// <param name="configCode"></param> /// <returns></returns> public SystemConfigValue GetSystemConfigValue(string configTypeCode, string configCode) { using (MySystemConfigContext context = new MySystemConfigContext()) { SystemConfigValue result = context.SystemConfigValues.Find(configTypeCode, configCode); if (result == null) { return(result); } // 配置的数据类型. Type configType = null; try { configType = Type.GetType(result.SystemConfigTypeData.ConfigClassName); } catch (Exception) { configType = null; } // Json 反序列化. if (configType != null) { // 指定了数据类型. result.ConfigValueObject = JsonConvert.DeserializeObject(result.ConfigValue, configType); } else { // 未指定数据类型. result.ConfigValueObject = JsonConvert.DeserializeObject(result.ConfigValue); } return(result); } }
public void TestSerializableDictionary() { Dictionary <string, object> configValueObject1 = new Dictionary <string, object>(); configValueObject1.Add("Name", "张三"); configValueObject1.Add("Age", 25); configValueObject1.Add("Address", "北京"); configValueObject1.Add("IsEmployee", true); Dictionary <string, object> configValueObject2 = new Dictionary <string, object>(); configValueObject2.Add("Name", "李四"); configValueObject2.Add("Age", 30); configValueObject2.Add("Address", "上海"); configValueObject2.Add("IsEmployee", false); SystemConfigValue configValue1 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_DICTIONARY_1", // 配置代码 ConfigCode = "TEST1", // 配置名称. ConfigName = "测试项目1", // 配置数值. ConfigValueObject = configValueObject1, }; SystemConfigValue configValue2 = new SystemConfigValue() { // 类型代码. ConfigTypeCode = "TEST_DICTIONARY_1", // 配置代码 ConfigCode = "TEST2", // 配置名称. ConfigName = "测试项目2", // 配置数值. ConfigValueObject = configValueObject2, }; // 先更新. string resultMsg = null; bool result = this.systemConfigService.UpdateSystemConfigValue(configValue1, ref resultMsg); Assert.IsTrue(result); result = this.systemConfigService.UpdateSystemConfigValue(configValue2, ref resultMsg); Assert.IsTrue(result); // 后查询. var dataList = this.systemConfigService.GetSystemConfigValueByType("TEST_DICTIONARY_1"); // 结果数据非空. Assert.IsNotNull(dataList); // 行数为2. Assert.AreEqual(2, dataList.Count); Dictionary <string, object> configData1 = dataList[0].ConfigValueObject as Dictionary <string, object>; Dictionary <string, object> configData2 = dataList[1].ConfigValueObject as Dictionary <string, object>; // 明细数据非空. Assert.IsNotNull(configData1); Assert.IsNotNull(configData2); Assert.AreEqual("张三", configData1["Name"]); Assert.AreEqual("25", configData1["Age"].ToString()); Assert.AreEqual("北京", configData1["Address"]); Assert.AreEqual(true, configData1["IsEmployee"]); Assert.AreEqual("李四", configData2["Name"]); Assert.AreEqual("30", configData2["Age"].ToString()); Assert.AreEqual("上海", configData2["Address"]); Assert.AreEqual(false, configData2["IsEmployee"]); // 注意: 由于是 Dictionary<string, object> // Assert.AreEqual(30, configData2["Age"]); // 将会失败. // 原因是 configData2["Age"] 的数据类型是 Int64. }