标签:
项目中使用了Oracle数据库,命名基本规范为表名和字段名全部大写,用下划线分割各个单词;
如“BASE_USER_LOGON_EXTEND”这个表表示用户登录的扩展表。
基于这个规范,通用权限管理系统配套升级了代码生成器工具。
下面以Oracle数据库中的BASE_USER_LOGON_EXTEND表来使用这个工具进行实体类和业务类代码的生成。
表结构图
代码生成器截图
实体层代码
1 //----------------------------------------------------------------------- 2 // <copyright file="BaseUserLogonExtendEntity.cs" company="Hairihan"> 3 // Copyright (c) 2015 , All rights reserved. 4 // </copyright> 5 //----------------------------------------------------------------------- 6 7 using System; 8 using System.Collections.Generic; 9 using System.Linq; 10 using System.Data; 11 12 namespace DotNet.Business 13 { 14 using DotNet.Utilities; 15 16 /// <summary> 17 /// BaseUserLogonExtendEntity 18 /// 用户登录的扩展表,账号登录方式,登录提醒方式 19 /// 20 /// 修改纪录 21 /// 22 /// 2015-01-26 版本:1.0 JiRiGaLa 创建文件。 23 /// 24 /// <author> 25 /// <name>JiRiGaLa</name> 26 /// <date>2015-01-26</date> 27 /// </author> 28 /// </summary> 29 public partial class BaseUserLogonExtendEntity : BaseEntity 30 { 31 private Decimal id; 32 /// <summary> 33 /// 主键 用户ID 34 /// </summary> 35 public Decimal Id 36 { 37 get 38 { 39 return id; 40 } 41 set 42 { 43 id = value; 44 } 45 } 46 47 private Decimal? emailRemind = null; 48 /// <summary> 49 /// 登录邮件提醒 50 /// </summary> 51 public Decimal? EmailRemind 52 { 53 get 54 { 55 return emailRemind; 56 } 57 set 58 { 59 emailRemind = value; 60 } 61 } 62 63 private Decimal? qrCodeLogon = null; 64 /// <summary> 65 /// 二维码登录 66 /// </summary> 67 public Decimal? QrCodeLogon 68 { 69 get 70 { 71 return qrCodeLogon; 72 } 73 set 74 { 75 qrCodeLogon = value; 76 } 77 } 78 79 private Decimal? jixinRemind = null; 80 /// <summary> 81 /// 登录提醒 82 /// </summary> 83 public Decimal? JixinRemind 84 { 85 get 86 { 87 return jixinRemind; 88 } 89 set 90 { 91 jixinRemind = value; 92 } 93 } 94 95 private Decimal? wechatRemind = null; 96 /// <summary> 97 /// 登录微信提醒 98 /// </summary> 99 public Decimal? WechatRemind 100 { 101 get 102 { 103 return wechatRemind; 104 } 105 set 106 { 107 wechatRemind = value; 108 } 109 } 110 111 private Decimal? dynamicCodeLogon = null; 112 /// <summary> 113 /// 动态码登录 114 /// </summary> 115 public Decimal? DynamicCodeLogon 116 { 117 get 118 { 119 return dynamicCodeLogon; 120 } 121 set 122 { 123 dynamicCodeLogon = value; 124 } 125 } 126 127 private Decimal? mobileRemind = null; 128 /// <summary> 129 /// 登录手机短信提醒 130 /// </summary> 131 public Decimal? MobileRemind 132 { 133 get 134 { 135 return mobileRemind; 136 } 137 set 138 { 139 mobileRemind = value; 140 } 141 } 142 143 private Decimal? usernamePasswordLogon = null; 144 /// <summary> 145 /// 用户名密码方式登录 146 /// </summary> 147 public Decimal? UsernamePasswordLogon 148 { 149 get 150 { 151 return usernamePasswordLogon; 152 } 153 set 154 { 155 usernamePasswordLogon = value; 156 } 157 } 158 159 /// <summary> 160 /// 从数据行读取 161 /// </summary> 162 /// <param name="dr">数据行</param> 163 protected override BaseEntity GetFrom(IDataRow dr) 164 { 165 GetFromExpand(dr); 166 Id = BaseBusinessLogic.ConvertToDecimal(dr[BaseUserLogonExtendEntity.FieldId]); 167 EmailRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldEmailRemind]); 168 QrCodeLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldQrCodeLogon]); 169 JixinRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldJixinRemind]); 170 WechatRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldWechatRemind]); 171 DynamicCodeLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldDynamicCodeLogon]); 172 MobileRemind = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldMobileRemind]); 173 UsernamePasswordLogon = BaseBusinessLogic.ConvertToNullableDecimal(dr[BaseUserLogonExtendEntity.FieldUsernamePasswordLogon]); 174 return this; 175 } 176 177 ///<summary> 178 /// 用户登录的扩展表,账号登录方式,登录提醒方式 179 ///</summary> 180 public static string TableName = "BASE_USER_LOGON_EXTEND"; 181 182 ///<summary> 183 /// 主键 用户ID 184 ///</summary> 185 public static string FieldId = "Id"; 186 187 ///<summary> 188 /// 登录邮件提醒 189 ///</summary> 190 public static string FieldEmailRemind = "EMAIL_REMIND"; 191 192 ///<summary> 193 /// 二维码登录 194 ///</summary> 195 public static string FieldQrCodeLogon = "QR_CODE_LOGON"; 196 197 ///<summary> 198 /// 登录其它提醒 199 ///</summary> 200 public static string FieldJixinRemind = "JIXIN_REMIND"; 201 202 ///<summary> 203 /// 登录微信提醒 204 ///</summary> 205 public static string FieldWechatRemind = "WECHAT_REMIND"; 206 207 ///<summary> 208 /// 动态码登录 209 ///</summary> 210 public static string FieldDynamicCodeLogon = "DYNAMIC_CODE_LOGON"; 211 212 ///<summary> 213 /// 登录手机短信提醒 214 ///</summary> 215 public static string FieldMobileRemind = "MOBILE_REMIND"; 216 217 ///<summary> 218 /// 用户名密码方式登录 219 ///</summary> 220 public static string FieldUsernamePasswordLogon = "USERNAME_PASSWORD_LOGON"; 221 } 222 }
业务层代码
1 -------------------------------------------- 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Data; 7 8 namespace DotNet.Business 9 { 10 using DotNet.Business; 11 using DotNet.Utilities; 12 13 /// <summary> 14 /// BaseUserLogonExtendManager 15 /// 用户登录的扩展表,账号登录方式,登录提醒方式 16 /// 17 /// 修改纪录 18 /// 19 /// 2015-01-26 版本:1.0 JiRiGaLa 创建文件。 20 /// 21 /// <author> 22 /// <name>JiRiGaLa</name> 23 /// <date>2015-01-26</date> 24 /// </author> 25 /// </summary> 26 public partial class BaseUserLogonExtendManager : BaseManager, IBaseManager 27 { 28 /// <summary> 29 /// 构造函数 30 /// </summary> 31 public BaseUserLogonExtendManager() 32 { 33 if (base.dbHelper == null) 34 { 35 base.dbHelper = DbHelperFactory.GetHelper(BaseSystemInfo.UserCenterDbType, BaseSystemInfo.UserCenterDbConnection); 36 } 37 if (string.IsNullOrEmpty(base.CurrentTableName)) 38 { 39 base.CurrentTableName = BaseUserLogonExtendEntity.TableName; 40 } 41 base.PrimaryKey = "Id"; 42 } 43 44 /// <summary> 45 /// 构造函数 46 /// <param name="tableName">指定表名</param> 47 /// </summary> 48 public BaseUserLogonExtendManager(string tableName) 49 { 50 base.CurrentTableName = tableName; 51 } 52 53 /// <summary> 54 /// 构造函数 55 /// </summary> 56 /// <param name="dbHelper">数据库连接</param> 57 public BaseUserLogonExtendManager(IDbHelper dbHelper): this() 58 { 59 DbHelper = dbHelper; 60 } 61 62 /// <summary> 63 /// 构造函数 64 /// </summary> 65 /// <param name="userInfo">用户信息</param> 66 public BaseUserLogonExtendManager(BaseUserInfo userInfo) : this() 67 { 68 UserInfo = userInfo; 69 } 70 71 /// <summary> 72 /// 构造函数 73 /// </summary> 74 /// <param name="userInfo">用户信息</param> 75 /// <param name="tableName">指定表名</param> 76 public BaseUserLogonExtendManager(BaseUserInfo userInfo, string tableName) : this(userInfo) 77 { 78 base.CurrentTableName = tableName; 79 } 80 81 /// <summary> 82 /// 构造函数 83 /// </summary> 84 /// <param name="dbHelper">数据库连接</param> 85 /// <param name="userInfo">用户信息</param> 86 public BaseUserLogonExtendManager(IDbHelper dbHelper, BaseUserInfo userInfo) : this(dbHelper) 87 { 88 UserInfo = userInfo; 89 } 90 91 /// <summary> 92 /// 构造函数 93 /// </summary> 94 /// <param name="dbHelper">数据库连接</param> 95 /// <param name="userInfo">用户信息</param> 96 /// <param name="tableName">指定表名</param> 97 public BaseUserLogonExtendManager(IDbHelper dbHelper, BaseUserInfo userInfo, string tableName) : this(dbHelper, userInfo) 98 { 99 base.CurrentTableName = tableName; 100 } 101 102 /// <summary> 103 /// 添加, 这里可以人工干预,提高程序的性能 104 /// </summary> 105 /// <param name="entity">实体</param> 106 /// <param name="identity">自增量方式,表主键是否采用自增的策略</param> 107 /// <param name="returnId">返回主键,不返回程序允许速度会快,主要是为了主细表批量插入数据优化用的</param> 108 /// <returns>主键</returns> 109 public string Add(BaseUserLogonExtendEntity entity, bool identity = false, bool returnId = false) 110 { 111 this.Identity = identity; 112 this.ReturnId = returnId; 113 return this.AddObject(entity); 114 } 115 116 /// <summary> 117 /// 更新 118 /// </summary> 119 /// <param name="entity">实体</param> 120 public int Update(BaseUserLogonExtendEntity entity) 121 { 122 return this.UpdateObject(entity); 123 } 124 125 /// <summary> 126 /// 获取实体 127 /// </summary> 128 /// <param name="id">主键</param> 129 public BaseUserLogonExtendEntity GetObject(string id) 130 { 131 return BaseEntity.Create<BaseUserLogonExtendEntity>(this.GetDataTable(new KeyValuePair<string, object>(this.PrimaryKey, id))); 132 } 133 134 /// <summary> 135 /// 添加实体 136 /// </summary> 137 /// <param name="entity">实体</param> 138 public string AddObject(BaseUserLogonExtendEntity entity) 139 { 140 string key = string.Empty; 141 if (entity.Id != null) 142 { 143 key = entity.Id.ToString(); 144 } 145 SQLBuilder sqlBuilder = new SQLBuilder(DbHelper, this.Identity, this.ReturnId); 146 sqlBuilder.BeginInsert(this.CurrentTableName, this.PrimaryKey); 147 if (!this.Identity) 148 { 149 // 这里已经是指定了主键了,所以不需要返回主键了 150 sqlBuilder.ReturnId = false; 151 sqlBuilder.SetValue(this.PrimaryKey, entity.Id); 152 } 153 else 154 { 155 if (!this.ReturnId && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2)) 156 { 157 if (DbHelper.CurrentDbType == CurrentDbType.Oracle) 158 { 159 sqlBuilder.SetFormula(this.PrimaryKey, "SEQ_" + this.CurrentTableName.ToUpper() + ".NEXTVAL "); 160 } 161 if (DbHelper.CurrentDbType == CurrentDbType.DB2) 162 { 163 sqlBuilder.SetFormula(this.PrimaryKey, "NEXT VALUE FOR SEQ_" + this.CurrentTableName.ToUpper()); 164 } 165 } 166 else 167 { 168 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2)) 169 { 170 BaseSequenceManager sequenceManager = new BaseSequenceManager(DbHelper); 171 entity.Id = int.Parse(sequenceManager.Increment(this.CurrentTableName)); 172 sqlBuilder.SetValue(this.PrimaryKey, entity.Id); 173 } 174 } 175 } 176 this.SetObject(sqlBuilder, entity); 177 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.SqlServer || DbHelper.CurrentDbType == CurrentDbType.Access)) 178 { 179 key = sqlBuilder.EndInsert().ToString(); 180 } 181 else 182 { 183 sqlBuilder.EndInsert(); 184 } 185 if (this.Identity && (DbHelper.CurrentDbType == CurrentDbType.Oracle || DbHelper.CurrentDbType == CurrentDbType.DB2)) 186 { 187 return entity.Id.ToString(); 188 } 189 return key; 190 } 191 192 /// <summary> 193 /// 更新实体 194 /// </summary> 195 /// <param name="entity">实体</param> 196 public int UpdateObject(BaseUserLogonExtendEntity entity) 197 { 198 SQLBuilder sqlBuilder = new SQLBuilder(DbHelper); 199 sqlBuilder.BeginUpdate(this.CurrentTableName); 200 this.SetObject(sqlBuilder, entity); 201 sqlBuilder.SetWhere(this.PrimaryKey, entity.Id); 202 return sqlBuilder.EndUpdate(); 203 } 204 205 // 这个是声明扩展方法 206 partial void SetObjectExpand(SQLBuilder sqlBuilder, BaseUserLogonExtendEntity entity); 207 208 /// <summary> 209 /// 设置实体 210 /// </summary> 211 /// <param name="entity">实体</param> 212 private void SetObject(SQLBuilder sqlBuilder, BaseUserLogonExtendEntity entity) 213 { 214 SetObjectExpand(sqlBuilder, entity); 215 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldEmailRemind, entity.EmailRemind); 216 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldQrCodeLogon, entity.QrCodeLogon); 217 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldJixinRemind, entity.JixinRemind); 218 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldWechatRemind, entity.WechatRemind); 219 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldDynamicCodeLogon, entity.DynamicCodeLogon); 220 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldMobileRemind, entity.MobileRemind); 221 sqlBuilder.SetValue(BaseUserLogonExtendEntity.FieldUsernamePasswordLogon, entity.UsernamePasswordLogon); 222 } 223 224 /// <summary> 225 /// 删除实体 226 /// </summary> 227 /// <param name="id">主键</param> 228 /// <returns>影响行数</returns> 229 public int Delete(string id) 230 { 231 return this.Delete(new KeyValuePair<string, object>(this.PrimaryKey, id)); 232 } 233 } 234 }
从生成的代码可以看出,完全符合.net的命名规范,爽心悦目~~,注意一点的就是要遵循规范命名表和字段。
后续的MVC的代码生成将会遵循以上规则,开发人员有了这个神器,就可以安心写业务层代码了,自己省时省力,为公司也节约资源,好处多多~~~
减少不必要的浪费,用更多的时间关注更重要的事。
标签:
原文地址:http://www.cnblogs.com/hnsongbiao/p/4251438.html