1- using Microsoft . Extensions . Caching . Distributed ;
2- using Microsoft . Extensions . Logging ;
3- using System ;
1+ using System ;
42using System . Data . Common ;
3+ using System . Linq ;
4+ using System . Reflection ;
5+ using FreeSql . DataAnnotations ;
6+ using FreeSql . Internal ;
57
68namespace FreeSql {
79 public class FreeSqlBuilder {
8- IDistributedCache _cache ;
9- ILogger _logger ;
1010 DataType _dataType ;
1111 string _masterConnectionString ;
1212 string [ ] _slaveConnectionString ;
@@ -16,28 +16,10 @@ public class FreeSqlBuilder {
1616 bool _isConfigEntityFromDbFirst = false ;
1717 bool _isNoneCommandParameter = false ;
1818 bool _isLazyLoading = false ;
19+ StringConvertType _entityPropertyConvertType = StringConvertType . None ;
1920 Action < DbCommand > _aopCommandExecuting = null ;
2021 Action < DbCommand , string > _aopCommandExecuted = null ;
2122
22- /// <summary>
23- /// 使用缓存,不指定默认使用内存
24- /// </summary>
25- /// <param name="cache">缓存实现</param>
26- /// <returns></returns>
27- public FreeSqlBuilder UseCache ( IDistributedCache cache ) {
28- _cache = cache ;
29- return this ;
30- }
31-
32- /// <summary>
33- /// 使用日志,不指定默认输出控制台
34- /// </summary>
35- /// <param name="logger"></param>
36- /// <returns></returns>
37- public FreeSqlBuilder UseLogger ( ILogger logger ) {
38- _logger = logger ;
39- return this ;
40- }
4123 /// <summary>
4224 /// 使用连接串
4325 /// </summary>
@@ -124,17 +106,45 @@ public FreeSqlBuilder UseMonitorCommand(Action<DbCommand> executing, Action<DbCo
124106 return this ;
125107 }
126108
127- public IFreeSql Build ( ) => Build < IFreeSql > ( ) ;
109+ /// <summary>
110+ /// 自动转换实体属性名称 Entity Property -> Db Filed
111+ /// <para></para>
112+ /// *不会覆盖 [Column] 特性设置的Name
113+ /// </summary>
114+ /// <param name="convertType"></param>
115+ /// <returns></returns>
116+ public FreeSqlBuilder UseEntityPropertyNameConvert ( StringConvertType convertType )
117+ {
118+ _entityPropertyConvertType = convertType ;
119+ return this ;
120+ }
121+
122+ public IFreeSql Build ( ) => Build < IFreeSql > ( ) ;
128123 public IFreeSql < TMark > Build < TMark > ( ) {
124+ if ( string . IsNullOrEmpty ( _masterConnectionString ) ) throw new Exception ( "参数 masterConnectionString 不可为空,请检查 UseConnectionString" ) ;
129125 IFreeSql < TMark > ret = null ;
126+ Type type = null ;
130127 switch ( _dataType ) {
131- case DataType . MySql : ret = new MySql . MySqlProvider < TMark > ( _cache , _logger , _masterConnectionString , _slaveConnectionString ) ; break ;
132- case DataType . SqlServer : ret = new SqlServer . SqlServerProvider < TMark > ( _cache , _logger , _masterConnectionString , _slaveConnectionString ) ; break ;
133- case DataType . PostgreSQL : ret = new PostgreSQL . PostgreSQLProvider < TMark > ( _cache , _logger , _masterConnectionString , _slaveConnectionString ) ; break ;
134- case DataType . Oracle : ret = new Oracle . OracleProvider < TMark > ( _cache , _logger , _masterConnectionString , _slaveConnectionString ) ; break ;
135- case DataType . Sqlite : ret = new Sqlite . SqliteProvider < TMark > ( _cache , _logger , _masterConnectionString , _slaveConnectionString ) ; break ;
128+ case DataType . MySql :
129+ type = Type . GetType ( "FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySql" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
130+ if ( type == null ) type = Type . GetType ( "FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySqlConnector" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
131+ if ( type == null ) throw new Exception ( "缺少 FreeSql 数据库实现包:FreeSql.Provider.MySql.dll,可前往 nuget 下载" ) ;
132+ break ;
133+ case DataType . SqlServer : type = Type . GetType ( "FreeSql.SqlServer.SqlServerProvider`1,FreeSql.Provider.SqlServer" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
134+ if ( type == null ) throw new Exception ( "缺少 FreeSql 数据库实现包:FreeSql.Provider.SqlServer.dll,可前往 nuget 下载" ) ;
135+ break ;
136+ case DataType . PostgreSQL : type = Type . GetType ( "FreeSql.PostgreSQL.PostgreSQLProvider`1,FreeSql.Provider.PostgreSQL" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
137+ if ( type == null ) throw new Exception ( "缺少 FreeSql 数据库实现包:FreeSql.Provider.PostgreSQL.dll,可前往 nuget 下载" ) ;
138+ break ;
139+ case DataType . Oracle : type = Type . GetType ( "FreeSql.Oracle.OracleProvider`1,FreeSql.Provider.Oracle" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
140+ if ( type == null ) throw new Exception ( "缺少 FreeSql 数据库实现包:FreeSql.Provider.Oracle.dll,可前往 nuget 下载" ) ;
141+ break ;
142+ case DataType . Sqlite : type = Type . GetType ( "FreeSql.Sqlite.SqliteProvider`1,FreeSql.Provider.Sqlite" ) ? . MakeGenericType ( typeof ( TMark ) ) ;
143+ if ( type == null ) throw new Exception ( "缺少 FreeSql 数据库实现包:FreeSql.Provider.Sqlite.dll,可前往 nuget 下载" ) ;
144+ break ;
136145 default : throw new Exception ( "未指定 UseConnectionString" ) ;
137146 }
147+ ret = Activator . CreateInstance ( type , new object [ ] { _masterConnectionString , _slaveConnectionString } ) as IFreeSql < TMark > ;
138148 if ( ret != null ) {
139149 ret . CodeFirst . IsAutoSyncStructure = _isAutoSyncStructure ;
140150
@@ -146,7 +156,75 @@ public IFreeSql<TMark> Build<TMark>() {
146156 var ado = ret . Ado as Internal . CommonProvider . AdoProvider ;
147157 ado . AopCommandExecuting += _aopCommandExecuting ;
148158 ado . AopCommandExecuted += _aopCommandExecuted ;
149- }
159+
160+ //添加实体属性名全局AOP转换处理
161+ if ( _entityPropertyConvertType != StringConvertType . None )
162+ {
163+ // 局部方法判断是否存在Column特性以及是否设置Name的值
164+ bool CheckEntityPropertyColumnAttribute ( PropertyInfo propertyInfo )
165+ {
166+ var attr = propertyInfo . GetCustomAttribute < ColumnAttribute > ( ) ;
167+ if ( attr == null || string . IsNullOrEmpty ( attr . Name ) )
168+ {
169+ return true ;
170+ }
171+
172+ return false ;
173+ }
174+
175+ switch ( _entityPropertyConvertType )
176+ {
177+ case StringConvertType . Lower :
178+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
179+ {
180+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
181+ {
182+ e . ModifyResult . Name = e . Property . Name . ToLower ( ) ;
183+ }
184+ } ;
185+ break ;
186+ case StringConvertType . Upper :
187+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
188+ {
189+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
190+ {
191+ e . ModifyResult . Name = e . Property . Name . ToUpper ( ) ;
192+ }
193+ } ;
194+ break ;
195+ case StringConvertType . PascalCaseToUnderscore :
196+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
197+ {
198+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
199+ {
200+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) ;
201+ }
202+ } ;
203+ break ;
204+ case StringConvertType . PascalCaseToUnderscoreWithLower :
205+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
206+ {
207+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
208+ {
209+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) . ToLower ( ) ;
210+ }
211+ } ;
212+ break ;
213+ case StringConvertType . PascalCaseToUnderscoreWithUpper :
214+ ret . Aop . ConfigEntityProperty = ( s , e ) =>
215+ {
216+ if ( CheckEntityPropertyColumnAttribute ( e . Property ) )
217+ {
218+ e . ModifyResult . Name = StringUtils . PascalCaseToUnderScore ( e . Property . Name ) . ToUpper ( ) ;
219+ }
220+ } ;
221+ break ;
222+ default :
223+ break ;
224+ }
225+ }
226+ }
227+
150228 return ret ;
151229 }
152230 }
0 commit comments