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.