C#Refactoring

Created: 2024-03-08Author: Deloris Huels
Data Analysis
Browser
Dall·e

-

Ratings(0)

programming

Category

5

Conversations

Capabilities

Data Analysis
Visual data analysis
Browser
Online Search and Web Reading
Dall·e
Image Generation

Description

专注于C#编程与代码重构的开发者,负责审查和改进代码,以增强其可读性、可测试性、可扩展性和可维护性。

Prompts

  • 重构以下单元测试代码 ``` [Test] public void CheckTime_OutOfCycleLimit_Monthly_ReturnsActivityNotStartedYet() { var nowTime = DateTime.Now; int curMonthDays = DateTime.DaysInMonth(nowTime.Year,nowTime.Month);//当月有多少天 int dayOfMonth = nowTime.Day; List<int> monthDays = Enumerable.Range(1, curMonthDays).ToList(); monthDays.Remove(dayOfMonth); var cycleTimes = string.Join(",", monthDays); var weeklyMarketingMaster = new Marketing.Data.MarketingMaster() { BeginDate = nowTime.AddDays(-5), EndDate = nowTime.AddDays(5), CycleTimes = cycleTimes, CycleType = (byte)CycleType.MONTHLY, }; // Act var result = CycleTimeBI.CheckTime(weeklyMarketingMaster, nowTime); // Assert Assert.IsFalse(result.IsOk); Assert.AreEqual("活动未开始", result.msg); } ```
  • 单一职责原则 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() { // ... } }
  • 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&lt;NMGTravelScoreRankInfo&gt;.</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); } ```

More programming GPTs

2.425.0K
2.325.0K
2.625.0K
2.425.0K
2.625.0K
2.410.0K
2.510.0K