-1

I am a looking for a way to make a method for the repeated code below (last code block at the bottom). I am currently passing in a string[] and checking to see if it is any of the eventHubResource.Blah objects. Here is my EventHubResource class:

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; }
}

But obviously this can't happen because the value after case needs to be a constant. My thought was to do something like this (this only shows one of the repeated blocks in the switch statement):

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;
        }
    }
}

Here is the repetition of code that I have, where eventHubResource.Blah are type 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();
    }
}

Any help is greatly appreciated, and let me know if you need any more info.

TyngeOfTheGinge
  • 524
  • 1
  • 4
  • 14
  • Are you trying to create a Visual-Studio-Plugin that creates Code snippets ? Or a way to actually shorten your code, to avoid the repetition. Then generics, or subroutines with parameters might be an option. – Holger Nov 01 '19 at 17:23

1 Answers1

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

And you call this with

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

The exact syntax depends on the types your eventHub.Update() is producing.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Holger
  • 2,446
  • 1
  • 14
  • 13