I am doing an database update operation and updating some of the fields based on type being passed to the internal method with switch case statement, at the moment it has 4 case statements and it will grow bigger..
I am looking a way to convert this switch case to dictionary with key value pair or any kind of mechanism to implement inside method.
This is main method
public async Task<MutationResponse> SetRequestStage(string requestStage, Guid requestId, MasterSectionEnum masterSectionEnum)
{
var request = _dbContext.Requests.SingleOrDefault(r => r.Id == requestId);
var rs = _dbContext.RequestStages.SingleOrDefault(rs => rs.Name == requestStage);
if (rs != null)
{
request.RequestStage = rs;
if (rs.Name == "Approved")
{
switch (masterSectionEnum)
{
case MasterSectionEnum.LOCALCODE:
await UpdateRevision<LocalCode>(request.DataId).ConfigureAwait(false);
break;
case MasterSectionEnum.NATIONALCODE:
await UpdateRevision<NationalCode>(request.DataId).ConfigureAwait(false);
break;
case MasterSectionEnum.GUIDELINES:
await UpdateRevision<Guideline>(request.DataId).ConfigureAwait(false);
break;
case MasterSectionEnum.LIBRARYA621:
await UpdateRevision<LibraryA621>(request.DataId).ConfigureAwait(false);
break;
case .....
case .....
default:
throw new ArgumentException($"SetRequestStage Error: invalid MasterSection {masterSectionEnum.ToString()}");
}
}
}
_dbContext.SaveChanges();
return new MutationResponse();
}
and this will be the enum
public enum MasterSectionEnum
{
LOCALCODE,
NATIONALCODE,
GUIDELINES,
SPACETYPE,
LIBRARYA621
// this will grow bigger
}
and this will be the internal method that i am calling from above method
private async Task UpdateRevision<T>(Guid id) where T : class, IAEIMaster, IRevisionData
{
var dbSet = this._dbContext.Set<T>();
var code = dbSet.SingleOrDefault(c => c.Id == id);
......
......
code.Revision = revision;
code.IsApproved = true;
}
could any one suggest on how to convert this switch case to alternative kind with key value pair or with types that would be very grateful to me.
many thanks in advance
Update : i am looking kind of below method, I am using dot net core with EF core
var types = new Dictionary<string, string>();
foreach(var item in types)
{
if(item.Key == "enum value")
{
await UpdateRevision<item.value>(request.DataId).ConfigureAwait(false);
}
}