是否可以创建一种用于代码重复的方法?

时间:2019-11-01 16:07:01

标签: c# azure azure-sdk

我正在寻找一种方法来为下面的重复代码(底部的最后一个代码块)做一个方法。我目前正在传递一个string []并检查它是否是任何eventHubResource.Blah对象。这是我的EventHubResource类:

public class EventHubResource : IResource
{
    public string[] ListenManageRuleNames { get; set; }
    public string[] SendManageRuleNames { get; set; }
    public string[] SendListenRuleNames { get; set; }
    public string[] ManageRuleNames { get; set; }
    public string[] ListenRuleNames { get; set; }
    public string[] SendRuleNames { get; set; }
    public string[] ConsumerGroups { get; set; }
    public int MessageRetention { get; set; }
    public int PartitionCount { get; set; }
    public string Id { get; set; }
    public string Name { get; set; }
    public ResourceGroup ResourceGroup { get; set; }
    public Dictionary<string, string> Tags { get; set; }
}

但是显然这不可能发生,因为case之后的值必须是一个常数。我的想法是这样做(仅显示switch语句中重复的块之一):

public async void UpdateEventHub(string[] array, EventHubResource eventHubResource)
{
    if (!Utils.IsNullOrEmpty(array))
    {
        switch (array)
        {
            case eventHubResource.SendRuleNames:
                foreach (var sendRuleNames in eventHubResource.SendRuleNames)
                    {
                        await eventHub.Update()
                            .WithNewSendRule(sendRuleNames)
                            .ApplyAsync();
                    };
            # all of the other cases
            default:
                break;
        }
    }
}

这是我拥有的代码的重复,其中eventHubResource.Blah是string []类型:

if (!Utils.IsNullOrEmpty(eventHubResource.ConsumerGroups))
{
    foreach (var consumerGroup in eventHubResource.ConsumerGroups)
    {
        await eventHub.Update()
            .WithNewConsumerGroup(consumerGroup)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.SendRuleNames))
{
    foreach (var sendRuleNames in eventHubResource.SendRuleNames)
    {
        await eventHub.Update()
            .WithNewSendRule(sendRuleNames)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.ListenRuleNames))
{
    foreach (var listenRuleNames in eventHubResource.ListenRuleNames)
    {
        await eventHub.Update()
            .WithNewListenRule(listenRuleNames)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.ManageRuleNames))
{
    foreach (var manageRuleNames in eventHubResource.ManageRuleNames)
    {
        await eventHub.Update()
            .WithNewManageRule(manageRuleNames)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.SendListenRuleNames))
{
    foreach (var sendListenRuleNames in eventHubResource.SendListenRuleNames)
    {
        await eventHub.Update()
            .WithNewSendRule(sendListenRuleNames)
            .WithNewListenRule(sendListenRuleNames)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.SendManageRuleNames))
{
    foreach (var sendManageRuleNames in eventHubResource.SendManageRuleNames)
    {                       
        await eventHub.Update()
            .WithNewSendRule(sendManageRuleNames)
            .WithNewManageRule(sendManageRuleNames)
            .ApplyAsync();
    }
}

if (!Utils.IsNullOrEmpty(eventHubResource.ListenManageRuleNames))
{
    foreach (var listenManageRuleNames in eventHubResource.ListenManageRuleNames)
    {
        await eventHub.Update()
            .WithNewListenRule(listenManageRuleNames)
            .WithNewManageRule(listenManageRuleNames)
            .ApplyAsync();
    }
}

我们非常感谢您的帮助,如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

public void Run(string[] list, Func<IQueryable, IQuerayble> action)
{
    if (!Utils.IsNullOrEmpty(list))
    {
        foreach (var listenManageRuleNames in list)
        {
             var query = eventHub.Update();
             query = action(query); 
             await query.ApplyAsync();
        }
    }
}

然后用

Run(eventHubResource.ConsumerGroups, WithNewConsumerGroup);
Run(eventHubResource.SendRuleNames, WithNewSendRule);
Run(eventHubResource.ListenRuleNames, WithNewListenRule);

确切的语法取决于eventHub.Update()正在生成的类型。