我创建了一些使用EWS API的C#代码。大部分是google-fu,因为我只用C#编写了几周的东西。我的老板要求我重构代码以使其与某种样式匹配,但是我完全迷失了(因为我不知道自己在做什么!)
我尝试对代码进行多次更改,但始终面临着一片红色。 我使用了内置的重构,但是它正在创建新的方法,并且仍未编译。
static void Main(string[] args)
{
try
{
FindItemsResults<Item> findResults;
ConfigureExchangeSearchResults(out ExchangeService service, out SearchFilter sf, out ItemView view);
if (Settings.Default.envLive == true)
{
findResults = ProcessLiveEmails(service, sf, view);
}
else
{
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));
foreach (Folder folder in findFolderResults.Folders)
{
logger.Info(folder.DisplayName);
if (folder.DisplayName == Settings.Default.envFolder)
{
do
{
findResults = service.FindItems(folder.Id, sf, view);
logger.Info("Found " + findResults.TotalCount + " Messages");
foreach (Item item in findResults.Items)
{
logger.Info(item.DateTimeReceived);
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments, EmailMessageSchema.IsRead, ItemSchema.DateTimeReceived));
CheckMessageReceivedDate(message);
message.IsRead = true;
message.Update(ConflictResolutionMode.AutoResolve);
}
}
while (findResults.MoreAvailable);
}
}
}
}
catch (Exception e)
{
logger.Error(e, "Error Occured On: " + DateTime.Now + " - Error Details: " + e.Message);
}
NLog.LogManager.Shutdown();
}
这是我当前的代码。我被要求将其重构为:
static void Main(string[] args)
{
try
{
ExchangeService service;
SearchFilter sf;
ItemView view;
ConfigureExchangeSearchParameters(out service, out sf, out view);
if (settings.Default.envLive == true)
{
ProcessLiveEmails(service, sf, view);
}
else
{
ProcessTestEmails(service, sf, view);
}
}
catch (Exception e)
{
logger.Error(e, "Error Occurred On: " + DateTime.Now + " - Error Details: " + e.Message);
}
NLog.LogManager.Shutdown();
}
本月晚些时候,我准备了一些C#培训课程,但是我需要先整理一下这些课程!
任何帮助将不胜感激。
ETA: 这是ProcessLiveEmails和ProcessTestEmails的重构方法:
private static FindItemsResults<Item> ProcessTestEmails(ExchangeService service, SearchFilter sf, ItemView view)
{
FindItemsResults<Item> findResults;
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Inbox, new FolderView(int.MaxValue));
foreach (Folder folder in findFolderResults.Folders)
{
logger.Info(folder.DisplayName);
if (folder.DisplayName == Settings.Default.envFolder)
{
do
{
findResults = service.FindItems(folder.Id, sf, view);
logger.Info("Found " + findResults.TotalCount + " Messages");
foreach (Item item in findResults.Items)
{
logger.Info(item.DateTimeReceived);
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments, EmailMessageSchema.IsRead, ItemSchema.DateTimeReceived));
CheckMessageReceivedDate(message);
message.IsRead = true;
message.Update(ConflictResolutionMode.AutoResolve);
}
}
while (findResults.MoreAvailable);
}
}
return findResults;
}
private static void ConfigureExchangeSearchResults(out ExchangeService service, out SearchFilter sf, out ItemView view)
{
service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
{
UseDefaultCredentials = true
};
service.AutodiscoverUrl(Properties.Settings.Default.autoURL, RedirectionUrlValidationCallback);
sf = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
view = new ItemView(int.MaxValue);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
}
private static FindItemsResults<Item> ProcessLiveEmails(ExchangeService service, SearchFilter sf, ItemView view)
{
FindItemsResults<Item> findResults;
do
{
findResults = service.FindItems(WellKnownFolderName.Inbox, sf, view);
logger.Info("Found " + findResults.TotalCount + " Messages");
foreach (Item item in findResults.Items)
{
logger.Info(item.DateTimeReceived);
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments, ItemSchema.HasAttachments, EmailMessageSchema.IsRead, ItemSchema.DateTimeReceived));
CheckMessageReceivedDate(message);
message.IsRead = true;
message.Update(ConflictResolutionMode.AutoResolve);
}
}
while (findResults.MoreAvailable);
return findResults;
}
ProcessLiveEmails似乎很好,问题出在ProcessTestEmails,特别是最后一行:
return findResults;