C#Refactoring
专注于C#编程与代码重构的开发者,负责审查和改进代码,以增强其可读性、可测试性、可扩展性和可维护性。
28 👀
Views
0 🌟
Ratings
Tags:
Sign up to our newsletter
Get weekly updates on trending GPTs and new features.
Related GPTs
More about this GPT 🌟
General Info 📄
Author: @noob92533838
Privacy Policy:
N/A
Last Updated:
Aug 17, 2024
Share Recipient: marketplace
Tools used: browser, python
Additional Details
ID: 122131
Slug: crefactoring
Created At: Apr 08, 2024
Updated At: Nov 18, 2024
Prompt Starters 💡
- Open/Closed Principle (OCP)示例代码: ```abstract class AdapterBase { protected string Name; public string GetName() { return Name; } } class AjaxAdapter : AdapterBase { public AjaxAdapter() { Name = "ajaxAdapter"; } } class NodeAdapter : AdapterBase { public NodeAdapter() { Name = "nodeAdapter"; } } class HttpRequester : AdapterBase { private readonly AdapterBase Adapter; public HttpRequester(AdapterBase adapter) { Adapter = adapter; } public bool Fetch(string url) { var adapterName = Adapter.GetName(); if (adapterName == "ajaxAdapter") { return MakeAjaxCall(url); } else if (adapterName == "httpNodeAdapter") { return MakeHttpCall(url); } } private bool MakeAjaxCall(string url) { // request and return promise } private bool MakeHttpCall(string url) { // request and return promise } }```
- 重构方法GetRanks中的SQL Server的语句,提高可读性和性能,输出优化的建议和问题,输出优化后的代码 ``` /// <summary> /// Gets the ranks. /// </summary> /// <param name="query">The query.</param> /// <param name="activity">The activity.</param> /// <returns>List<NMGTravelScoreRankInfo>.</returns> public List<NMGTravelScoreRankInfo> GetRanks(TravelScoreRankQuery query, NMGTravelRule activity) { DateTime startTime, endTime; (startTime, endTime) = activity.GetActivityTime(); string costFlowTableName = GetMargeTableName<VipCostFlow>(query.CustId); string vipTableName = GetMargeTableName<VipInfo>(query.CustId); if (string.IsNullOrWhiteSpace(query.AreaName) || query.AreaName == "all") { return GetSumRanks(query,activity); } string sql = string.Format(@" with T1 as ( --汇总消费总金额+卡号 select CardNo,sum(ReduceAmt) as Score from " + costFlowTableName + @" where CustId=@CustId and OperDate>@StartTime and OperDate<@EndTime and BranchNo in (select BranchNo from BranchInfo where CustID=@CustId and City in ({0}) ) group by CardNo ), T2 as ( --合并用户所属省份 select T1.*,UserArea.Province from T1 left join ( SELECT tmpTable.CardNo,tmpTable.Province,tmpTable.RowNum FROM ( SELECT cf.*,ROW_NUMBER() OVER( PARTITION BY cf.CardNo ORDER BY cf.OperDate) AS RowNum,bi.City as Province FROM " + costFlowTableName + @" cf INNER JOIN BranchInfo bi ON bi.CustId=cf.CustID AND bi.BranchNo = cf.BranchNo WHERE cf.CustId=@CustId and cf.OperDate>@StartTime and cf.BranchNo in (select BranchNo from BranchInfo where CustID=@CustId and City in ({0}) ) ) AS tmpTable WHERE tmpTable.RowNum=1 ) as UserArea on T1.CardNo=UserArea.CardNo ), T22 as ( --汇总二维码积分 select isnull(T2.CardNo,sumTable.CardNo) as CardNo,(isnull(T2.Score,0)+isnull(sumTable.TravelScore,0)) as Score,T2.Province from T2 full outer join NMGTravelScoreQRCodeSum as sumTable on T2.CardNo=sumTable.CardNo and sumTable.CustId=@CustId AND sumTable.ActivityId=@ActivityId ), T3 as ( --获取 手机号信息 select T22.*,vi.Mobile from T22 left join " + vipTableName + @" as vi on vi.CardNo=T22.CardNo and vi.CustID=@CustId ), T4 as ( select row_number() over (order by Score desc) as Rank,T3.* from T3 where T3.Province ='{1}' ) select top {2} Rank,CardNo,Mobile,Score,Province from T4 where T4.Score>0 order by Rank ", activity.ActArea, query.AreaName, activity.TopRankSize); var param = new { CustId = query.CustId, StartTime = startTime, EndTime = endTime, ActivityId = activity.Id }; return DBDataSourceHelper.ExecuteReaderReturnListT<NMGTravelScoreRankInfo>( Conn_Catering, sql, param); } ```
- 为了提高代码的可读性、可维护性和健壮性,对以下代码进行重构: 1. 方法分离:将不同的职责操作提取到单独的方法中,以提高代码的可读性和可维护性。 2. 使用更具描述性的变量名:使变量名更直观,以提高代码的可读性。 3. 避免重复代码:使用更简洁的方法来处理相似的逻辑。 4. 增加XMl格式的中文文档注释 ```csharp /// <summary> /// Updates the order pay status. /// </summary> /// <param name="orderId">The order identifier.</param> /// <param name="paymentFlows">The payment flows.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public bool UpdateOrderPayStatus(string orderId, List < Crating.Data.ModelFiles.WXPaymentFlow > paymentFlows) { if (paymentFlows.IsEmpty()) { return false; } using(var trans = DBDataSourceHelper.BeginTransaction(DBConfig.Write_ConnStrForCratingDB)) { try { string updateOrderSql = "UPDATE FoodOrderMaster SET PayFlag='1',AutoTurnBill='1' WHERE OrderID=@OrderID"; DBDataSourceHelper.ExecuteUpdate(DBConfig.Write_ConnStrForCratingDB, updateOrderSql, new { OrderID = orderId }, trans); for (int i = 0; i < paymentFlows.Count; i++) { if (i == 0) { string updatPayFlowSql = "UPDATE FoodOrderPayFlow SET Amount=@Amount,PayWay =@PayType,CardNo=@CardNo,OuterCode=@OuterCode WHERE OrderID=@OrderId AND PayWay ='O'"; DBDataSourceHelper.ExecuteUpdate(DBConfig.Write_ConnStrForCratingDB, updatPayFlowSql, new { OrderID = orderId, Amount = paymentFlows[i].Amount, PayWay = paymentFlows[i].PayType, CardNo = paymentFlows[i].CardNo, OuterCode = paymentFlows[i].XddTradeNo }, trans); } else { FoodOrderPayFlow foodOrderPayFlow = new FoodOrderPayFlow() { Amount = paymentFlows[i].Amount, CardNo = paymentFlows[i].CardNo, CoinNo = "RMB", OrderId = orderId, OuterCode = paymentFlows[i].XddTradeNo, PayWay = paymentFlows[i].PayType, SellWay = "A", TicketType = string.Empty, TicketRealPayAmt = 0.0 M, }; DBDataSourceHelper.AddOrmTranData(DBConfig.Write_ConnStrForCratingDB, foodOrderPayFlow, trans); } } trans.Commit(); return true; } catch (Exception ex) { SysLogHelper.Error(ex); trans.Rollback(); return false; } } } ```
- 单一职责原则 Single Responsibility Principle(SRP)示例代码: ```csharp class UserSettings { private User User; public UserSettings(User user) { User = user; } public void ChangeSettings(Settings settings) { if (verifyCredentials()) { // ... } } private bool VerifyCredentials() { // ... } } ```
Files 📁
- None
- None