简体   繁体   English

是否可以重写此C#方法重复(Lambdas?代表?)?

[英]Can this C# method duplication be rewritten (Lambdas? Delegates?)?

Bear with me the world of lambdas, etc is making my head hurt. 忍受lambda的世界,等等正在伤害我的头部。 I'm not even sure if the following code example is one where someone would use a lambda. 我什至不知道下面的代码示例是否是有人使用lambda的示例。

public static void AddDownloadMimeTypesToWebApps()
{
    var webApps = from svc in SPFarm.Local.Services.OfType<SPWebService>()
                  from SPWebApplication webApp in svc.WebApplications
                  where webApp.IsAdministrationWebApplication == false
                  select webApp;
    foreach (SPWebApplication webApp in webApps)
    {
        foreach (FileExtensionData fed in MimeTypes)
        {
            webApp.AllowedInlineDownloadedMimeTypes.Add(fed.MimeType);
        }
        webApp.Update();
    }
}

public static void DeleteDownloadMimeTypesFromWebApps()
{
    var webApps = from svc in SPFarm.Local.Services.OfType<SPWebService>()
                  from SPWebApplication webApp in svc.WebApplications
                  where webApp.IsAdministrationWebApplication == false
                  select webApp;
    foreach (SPWebApplication webApp in webApps)
    {
        foreach (FileExtensionData fed in MimeTypes)
        {
            webApp.AllowedInlineDownloadedMimeTypes.Remove(fed.MimeType);
        }
        webApp.Update();
    }
}

Both methods are identical except for the Add() or Remove() calls. 除了Add()或Remove()调用之外,这两种方法都是相同的。

Is there a way to refactor these that doesn't involve some strange reflection calls with passing in method names as strings, etc? 有没有一种方法可以重构这些方法,而不会通过将方法名称作为字符串等传入而涉及一些奇怪的反射调用?

Cheers! 干杯!

Initially, I would suggest you write a method to get the webApps: 最初,我建议您编写一种获取webApp的方法:

public static IEnumerable<SPWebApplication> GetWebApps()
{
    var webApps = from svc in SPFarm.Local.Services.OfType<SPWebService>()
                  from SPWebApplication webApp in svc.WebApplications
                  where webApp.IsAdministrationWebApplication == false
                  select webApp;
    return webApps;   
}

Then, despite the fact that the rest of code is quite similar, since they do different things, I wouldn't join them to one method. 然后,尽管事实上其余的代码非常相似,但是由于它们执行不同的操作,因此我不会将它们加入一种方法。

public static void AddDownloadMimeTypesToWebApps(IEnumerable<SPWebApplication> webApps)
{
    foreach (var webApp in webApps)
    {
        foreach (var fed in MimeTypes)
        {
            webApp.AllowedInlineDownloadedMimeTypes.Add(fed.MimeType);
        }
        webApp.Update();
    }
}

and

public static void DeleteDownloadMimeTypesFromWebApps(IEnumerable<SPWebApplication> webApps)
{
    foreach (var webApp in webApps)
    {
        foreach (var fed in MimeTypes)
        {
            webApp.AllowedInlineDownloadedMimeTypes.Remove(fed.MimeType);
        }
        webApp.Update();
    }
}

Now you have three different methods with three different responsibilities. 现在,您有三种不同的方法,分别承担三种不同的职责。 The first, GetWebApps , returns a sequence of the WebApps . 第一, GetWebApps ,返回序列WebApps The second adds download mime types to the web apps you pass as a parameter, AddDownloadMimeTypesToWebApps and the last one, DeleteDownloadMimeTypesFromWebApps delete them. 第二增加下载MIME类型,你作为一个参数传递,网络应用AddDownloadMimeTypesToWebApps ,最后一个, DeleteDownloadMimeTypesFromWebApps删除它们。

This way it is easy to remember what exactly does each method. 这样很容易记住每种方法的确切作用。 Their purpose are crystal clear. 它们的目的非常明确。 Futhermore you don't hide from the consumer of the method what is needed as input. 此外,您不会对方法的使用者隐藏任何需要输入的内容。 For instance, someone reads the name of the second method. 例如,某人读取第二种方法的名称。 The first question is which web apps? 第一个问题是哪些网络应用程序? In the original code, where we don't pass the web apps as a parameter, we fetch them inside the mehtod's body. 在原始代码中,我们不将Web应用程序作为参数传递,而是将其获取到Mehtod体内。 This is meaningless, since according to the name of the method we just want to add download mime types to some web apps and not to fetch them and then add the donwload mime types. 这是没有意义的,因为根据方法的名称,我们只想将下载的mime类型添加到某些Web应用程序中,而不是获取它们,然后添加下载的mime类型。

Last but not least since the retrieval of web apps is the same for both the cases of adding and deleting, we have to isolate this to one method that would serve exactly this. 最后但并非最不重要的一点是,由于在添加和删除两种情况下Web应用程序的检索都是相同的,因此我们必须将其隔离为一种恰好可以满足此要求的方法。 The last action not only enhances that mentioned above but it is also a clear appliance of the DRY principle, don't repeat yourself , which help us to write more maintainable code. 最后一个动作不仅增强了上述功能,而且清楚地体现了DRY原理, 请不要重复自己 ,这有助于我们编写更具可维护性的代码。 Now if any time in the future you have to make some change in the retrieval of the web apps, you would have only one place, in which you have to look at, not two or more places. 现在,如果将来在任何时候都需要对Web应用程序的检索进行一些更改,则只需要查看一个位置,而不需要两个或多个位置。

How bout something like this where we can make the unique code a lambda: 如何将这样的东西打包成lambda:

private static void DownloadMimeTypesToWebApps(Action<SPWebApplication,FileExtensionData> action)
{
    var webApps = from svc in SPFarm.Local.Services.OfType<SPWebService>()
              from SPWebApplication webApp in svc.WebApplications
              where webApp.IsAdministrationWebApplication == false
              select webApp;
    foreach (SPWebApplication webApp in webApps)
    {
       foreach (FileExtensionData fed in MimeTypes)
       {
           action.Invoke(webApp,fed);
       }

       webApp.Update();
    }   

}

public static void AddDownloadMimeTypesToWebApps()
{
    DownloadMimeTypesToWebApps((webApp,fed) => webApp.AllowedInlineDownloadedMimeTypes.Add(fed.MimeType));
}

public static void DeleteDownloadMimeTypesToWebApps()
{
    DownloadMimeTypesToWebApps((webApp,fed) => webApp.AllowedInlineDownloadedMimeTypes.Remove(fed.MimeType));        
}

What about just a plain old parameter and if else?: 那仅仅是一个普通的旧参数又如何呢?

public static void EditMimeTypesToWebApps(int editType) // maybe you can make it an enum
{
    var webApps = from svc in SPFarm.Local.Services.OfType<SPWebService>()
                  from SPWebApplication webApp in svc.WebApplications
                  where webApp.IsAdministrationWebApplication == false
                  select webApp;
    foreach (SPWebApplication webApp in webApps)
    {
        foreach (FileExtensionData fed in MimeTypes)
        {   
            if(editType == 0)
            {
                webApp.AllowedInlineDownloadedMimeTypes.Remove(fed.MimeType);
            }
            else
            {
                webApp.AllowedInlineDownloadedMimeTypes.Add(fed.MimeType);
            }
        }
        webApp.Update();
    }
}

Easy enough to read. 足够容易阅读。 Easy enough for any level to maintain. 易于维护,适合任何级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM