mvc项目架构分享系列之架构搭建之Infrastructure 架
项目架构搭建之Infrastructure的搭建 Contents ? 系列一【架构概览】 0.项目简介 1.项目解决方案分层方案? 2.所用到的技术 3.项目引用关系? ? 系列二【架构搭建初步】 4.项目架构各部分解析 5.项目创建? ? 系列三 【Infrastructure搭建】 6.项目架构搭建之Core搭建 7.项目架构搭建之Models搭建 ? 系列四 【Repository和Service的搭建】 8.项目架构搭建之IDAL搭建? ? ? 系列五 【UI搭建】 12.WebHelper搭建 13.Web搭建 14.AdminLogic搭建 ? 系列六 【项目扩展】 15.新增Model的处理 ? ? 6.项目架构搭建之Core搭建????添加对用到的类库的引用,这里用到的包括:.NET自带的两个类库System.Configuration 、 System.Web ,和两个第三方类库 log4net(日志框架) 和 Newtonsoft.Json (Json.Net) ?
? 1 /// <summary> 2 3 /// 网站根路径 4 5 </summary> 6 7 private static string siteroot = System.Web.Hosting.HostingEnvironment.MapPath("~/"); 8 9 10 11 12 13 获取配置文件中AppSetting节点的相对路径对应的绝对路径 14 15 16 17 <param name="key">相对路径设置的键值</param> 18 19 <returns>绝对路径</returns> 20 21 public string AppSettingMapPath(string key) 22 23 { 24 25 if (String.IsNullOrEmpty(siteroot)) 26 27 { 28 29 siteroot = System.Web.Hosting.HostingEnvironment.MapPath( 30 31 } 32 33 //拼接路径 34 35 string path = siteroot + System.Configuration.ConfigurationManager.AppSettings[key].ToString(); 36 37 return path; 38 39 } 40 41 42 43 44 45 将虚拟路径转换为物理路径 46 47 48 49 <param name="virtualPath">虚拟路径 50 51 虚拟路径对应的物理路径 52 53 string MapPath( virtualPath) 54 55 56 57 58 59 60 61 siteroot = System.Web.Hosting.HostingEnvironment.MapPath( 62 63 64 65 66 67 virtualPath; 68 69 70 71 72 73 74 75 76 77 获取配置文件中AppSetting节点的值 78 79 80 81 设置的键值 82 83 键值对应的值 84 85 string AppSetting(string key) => 86 87 88 89 90 91 获取配置文件中ConnectionStrings节点的值 92 93 94 95 键值 96 97 键值对应的连接字符串值 98 99 string ConnectionString( System.Configuration.ConfigurationManager.ConnectionStrings[key].ConnectionString; 100 101 102 103 bool UpdateAppSettings(string key, value) 104 105 106 107 string filename = System.Web.Hosting.HostingEnvironment.MapPath(~/web.config108 109 XmlDocument xmldoc = new XmlDocument(); 110 111 try 112 113 114 115 xmldoc.Load(filename); 116 117 118 119 catch (Exception) 120 121 122 123 return false; 124 125 126 127 XmlNodeList DocdNodeNameArr = xmldoc.DocumentElement.ChildNodes;文档节点名称数组 128 129 foreach (XmlElement element in DocdNodeNameArr) 130 131 132 133 if (element.Name == appSettings")找到名称为 appSettings 的节点 134 135 { 136 137 XmlNodeList KeyNameArr = element.ChildNodes;子节点名称数组 138 139 if (KeyNameArr.Count > 0) 140 141 { 142 143 foreach (XmlElement xmlElement KeyNameArr) 144 145 { 146 147 找到键值,修改为想要修改的值 148 149 if (xmlElement.Attributes[key].InnerXml.Equals(key)) 150 151 { 152 153 xmlElement.Attributes[value"].Value = value; 154 155 ConfigurationManager.RefreshSection(156 157 true158 159 } 160 161 } 162 163 没有相应的节点 164 165 166 167 } 168 169 else 170 171 172 173 不存在 AppSettings 节点 174 175 176 177 178 179 } 180 181 182 183 184 185 186 187 ?
? 1 2 3 4 5 6 string virtualPath) => System.Web.Hosting.HostingEnvironment.MapPath(") + virtualPath; ?
? <summary> 日志助手 </summary> class LogHelper { readonly ILog logger = null; public LogHelper(Type t) { logger = LogManager.GetLogger(t); } public LogHelper( name) { logger = LogManager.GetLogger(name); } void LogInit() { log4net.Config.XmlConfigurator.Configure(); } void Debug( msg) { logger.Debug(msg); } msg,Exception ex) { logger.Debug(msg,ex); } void Error( msg) { logger.Error(msg); } void Warn( msg) { logger.Warn(msg); } Debug(Exception ex) { logger.Debug(ex.Message,1)"> Error(Exception ex) { logger.Error(ex.Message,ex); } } ?
? 1 2 类型转换助手 3 4 ConverterHelper 5 { 利用反射和泛型 <param name="dt">DataTable 对象 10 11 static List<T> DataTableToList<T>(DataTable dt) where T : class,1)">() 13 定义集合 14 List<T> ts = new List<T>(); 15 16 获得此模型的类型 17 Type type = typeof(T); 18 定义一个临时变量 19 string tempName = 20 遍历DataTable中所有的数据行 foreach (DataRow dr dt.Rows) 23 T t = T(); 24 获得此模型的公共属性 25 PropertyInfo[] propertys = t.GetType().GetProperties(); 26 遍历该对象的所有属性 27 foreach (PropertyInfo pi propertys) 28 29 tempName = pi.Name;将属性名称赋值给临时变量 30 检查DataTable是否包含此列(列名==对象的属性名) 31 (dt.Columns.Contains(tempName)) 32 33 取值 34 object value = dr[tempName]; 35 如果非空,则赋给对象的属性 36 if (value != DBNull.Value) 37 pi.SetValue(t,value,1)">对象添加到泛型集合中 41 ts.Add(t); ts; 将object对象转换为Json数据 <param name="obj">object对象转换后的json字符串string ObjectToJson(object obj) 54 JsonConvert.SerializeObject(obj); 57 将Json对象转换为T对象 <typeparam name="T">对象的类型</typeparam> <param name="jsonString">json对象字符串由字符串转换得到的T对象 63 static T JsonToObject<T>( jsonString) 64 65 return JsonConvert.DeserializeObject<T>(jsonString); 66 67 从字符串中获取数据 <param name="content">源字符串字符串中的值 73 string GetContent( content) 74 75 return (String.IsNullOrEmpty(content) ? : content); 76 77 78 将int转换为bool类型 <param name="value"></param> 83 bool ConvertIntToBool( 84 85 return (value > 0 ? true : 87 88 #region 转Int 89 90 91 将string类型转换成int类型 92 93 <param name="s">目标字符串 96 int StringToInt(string s,1)"> 97 98 .IsNullOrWhiteSpace(s)) 99 100 101 int.TryParse(s,1)">out result)) 102 103 105 106 107 110 111 112 113 114 115 return StringToInt(s,1)">116 117 将object类型转换成int类型 120 121 目标对象122 123 124 int ObjectToInt(object o,1)">126 if (o != 127 StringToInt(o.ToString(),1)">129 130 131 132 133 134 135 136 137 o) 138 139 return ObjectToInt(o,1)">140 141 142 #endregion 转Int 143 #region 转Bool 145 146 147 将string类型转换成bool类型 148 149 150 151 152 bool StringToBool(153 154 if (s.ToLower().Equals(false155 156 true157 159 160 161 162 163 164 165 167 bool ToBool(168 169 return StringToBool(s,1)">170 171 172 173 将object类型转换成bool类型 174 175 176 177 178 bool ObjectToBool(180 181 StringToBool(o.ToString(),1)">183 185 186 187 188 189 190 191 192 193 return ObjectToBool(o,1)">194 195 196 #endregion 转Bool 197 198 #region 转DateTime 199 200 201 将string类型转换成datetime类型 static DateTime StringToDateTime( s,DateTime defaultValue) 210 DateTime result; 211 if (DateTime.TryParse(s,1)">212 213 214 215 216 217 218 219 222 223 224 StringToDateTime(s,DateTime.Now); 226 227 228 将object类型转换成datetime类型 229 230 231 232 233 static DateTime ObjectToDateTime( o,1)">234 235 236 StringToDateTime(o.ToString(),1)">237 238 239 240 241 242 243 244 245 246 247 248 ObjectToDateTime(o,1)">249 250 251 #endregion 转DateTime 252 253 #region 转Decimal 254 255 256 将string类型转换成decimal类型 257 258 259 260 261 decimal StringToDecimal(decimal262 263 264 265 266 decimal.TryParse(s,1)">267 269 271 273 274 275 278 279 280 StringToDecimal(s,0m); 282 283 284 将object类型转换成decimal类型 285 286 287 288 289 decimal ObjectToDecimal(290 292 StringToDecimal(o.ToString(),1)">293 294 295 296 297 298 299 300 301 302 303 304 ObjectToDecimal(o,1)">305 306 307 #endregion 转Decimal 308 } ? 7.项目架构搭建之Models搭建????参考 这里 了解 EF CodeFirst,首先对EF类库进行引用,由于我的应用的使用的是SQLServer数据库,所以需要对EntityFramework.SqlServer进行引用,如果不引用子在进行数据库相关操作时会出错,错误如下: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded ?
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在ASP.NET MVC(视图)中包含WebForms?
- ASP.NET MVC Core中launchSettings.json修改导致VS2017无法
- .net – 任何具有Postsharp生产经验的人?
- 在ASP.NET Web应用程序之间传递会话数据
- asp.net-mvc – 使用具有不同扩展名的自定义httphandler的M
- asp.net – 如何从HTML 5多文件输入元素捕获上传的文件
- asp.net-mvc-3 – 返回JsonFile的MVC3控制器
- asp.net-mvc – ASP.NET MVC:AJAX调用的控制器是否返回JSO
- asp.net – 如何配置nginx以支持cloudflare下的signalr3?
- entity-framework – Entity Framework 5工作单元模式 – 我