Initialize bunch of variables in constructor by extracting value from dictionary I have a headers dictionary value being passed to one of my constructors and based on that dictionary value I am extracting individual headers I need and setting all my variables.

public Tester(IDictionary&lt;string, StringValues&gt; headers)
 {
     int textId = 0;

     if (headers == null)
     {
         ProcId = GenerateProcId();
         DbId = GenerateDbId();
         ThetaId = GenerateThetaId();
         return;
     }

     ProcIdHeader = GetHeaderValue(headers, PROC_HEADER);
     if (string.IsNullOrWhiteSpace(ProcIdHeader))
     {
         ProcId = GenerateProcId();
     }
     else
     {
         ProcId = ProcIdHeader;
     }

     DbIdHeader = GetHeaderValue(headers, DB_HEADER);
     if (string.IsNullOrWhiteSpace(DbIdHeader))
     {
         DbId = GenerateDbId();
     }
     else
     {
         DbId = DbIdHeader;
     }

     ThetaId = GenerateThetaId();

     var textIdStr = GetHeaderValue(headers, TEXT_HEADER);
     int.TryParse(textIdStr, out textId);
     TextIdValue = textId;
     TextId = textId;

     AlphaHeader = GetHeaderValue(headers, ALPHA_HEADER);
     if (!string.IsNullOrWhiteSpace(AlphaHeader))
     {
         AlphaContent = AlphaHeader;
     }

     TimeoutIdHeader = GetHeaderValue(headers, TIMEOUT_HEADER);
     if (!string.IsNullOrWhiteSpace(TimeoutIdHeader))
     {
         TimeoutId = TimeoutIdHeader;
     }

     FilterHeader = GetHeaderValue(headers, FILTER_HEADER);
     if (!string.IsNullOrWhiteSpace(FilterHeader))
     {
         FilterId = FilterHeader;
     }

     PreviewHeader = GetHeaderValue(headers, PREVIEW_HEADER);
     if (!string.IsNullOrWhiteSpace(PreviewHeader))
     {
         PreviewId = PreviewHeader;
     }

     AntHeader = GetHeaderValue(headers, ANT_HEADER);
     if (!string.IsNullOrWhiteSpace(AntHeader))
     {
         AntId = AntHeader;
     }
     LocalHeader = GetHeaderValue(headers, LOCAL_HEADER);
     AppHeader = GetHeaderValue(headers, APP_HEADER);
     if (!string.IsNullOrWhiteSpace(AppHeader))
     {
         AppId = AppHeader;
     }

     ContextHeader = GetHeaderValue(headers, CONTEXT_HEADER);
     if (ContextHeader != null)
     {
         try
         {
             ContextStr = JsonSerializer.Deserialize&lt;ContextStr&gt;(ContextHeader);
         }
         catch (Exception e)
         {
             ContextStr = new ContextStr();
         }
     }
     if (LocalHeader != null)
     {
         try
         {
             LocalStr = JsonSerializer.Deserialize&lt;LocalStr&gt;(LocalHeader);
         }
         catch (Exception e)
         {
             LocalStr = new LocalStr();
         }
     }
     PageHeader = GetHeaderValue(headers, PAGE_HEADER);
     if (!string.IsNullOrWhiteSpace(PageHeader))
     {
         PageStr = PageHeader;
     }
     ChannelId = GetHeaderValue(headers, CHANNEL_HEADER);
 }

 private string GetHeaderValue(IDictionary&lt;string, StringValues&gt; headers, string headerKey)
 {
     headers.TryGetValue(headerKey, out var headerValues);
     if (headerValues.Count &gt; 0) return headerValues[0];
     return null;
 }


I wanted to see if there is any better way to rewrite the constructor logic that I have. As of now it looks very bulky with bunch of if/else block. Anything that can be improve in my code?

Note: It's a legacy code base system so changing whole design might be difficult but little refactoring by splitting it into various methods is possible.
You can simplify that portion of code. First, you'll need to create an array that will holds the header keys such as PROC_HEADER ..etc. then you can go from there. 

Here is an example (NOTE, keys are just placeholders in this example, so you know which const needs to be replaced). 

public Tester(IDictionary&lt;string, StringValues&gt; headers)
 {
    // these will be overwritten if their customer global are not null. 
    DbId = GenerateDbId();
    ProcId = GenerateProcId();
    ThetaId = GenerateThetaId();

    if (headers == null) { return; }

    var headerKeys = new[]{
        "PROC_HEADER",
        "DB_HEADER",
        "TEXT_HEADER",
        "ALPHA_HEADER",
        "TIMEOUT_HEADER",
        "FILTER_HEADER",
        "PREVIEW_HEADER",
        "ANT_HEADER",
        "APP_HEADER",
        "PAGE_HEADER",
        "LOCAL_HEADER",
        "CONTEXT_HEADER",   
        "CHANNEL_HEADER"
    };

    foreach(var key in headerKeys)
    {
        headers.TryGetValue(key, out var headerValue);

        if (headerValue.Count &gt; 0)
        {   
            switch(key)
            {
                case "PROC_HEADER":
                    SetHeaderValue(headerValue[0], "ProcIdHeader", "ProcId");
                    break;
                case "DB_HEADER":
                    SetHeaderValue(headerValue[0], "DbIdHeader",  "DbId");  
                    break;      
                case "TEXT_HEADER":
                     int.TryParse(headerValue[0],  var textId);
                     TextIdValue = textId;
                     TextId = textId;               
                    break;              
                case "ALPHA_HEADER":
                    SetHeaderValue(headerValue[0],  "AlphaHeader",  "AlphaContent");
                    break;
                case "TIMEOUT_HEADER":
                    SetHeaderValue(headerValue[0],   "TimeIdHeader",  "TimeId");        
                    break;      
                case "FILTER_HEADER":
                    SetHeaderValue(headerValue[0],   "FilterHeader",  "FilterId");      
                    break;          
                case "PREVIEW_HEADER":
                    SetHeaderValue(headerValue[0],   "PreviewHeader",  "PreviewId");        
                    break;          
                case "ANT_HEADER":
                    SetHeaderValue(headerValue[0],   "AntHeader",  "AntId");    
                    break;      
                case "APP_HEADER":
                    SetHeaderValue(headerValue[0],   "AppHeader",  "AppId");    
                    break;  
                case "PAGE_HEADER":
                    SetHeaderValue(headerValue[0],   "PageHeader",  "PageStr"); 
                    break;   
                case "CHANNEL_HEADER":
                    ChannelId = headerValue[0];     
                    break;  
                case "LOCAL_HEADER":
                    LocalHeader = headerValue[0];
                    LocalStr = DeserializeHeader&lt;LocalStr&gt;(LocalHeader);
                    break;      
                case "CONTEXT_HEADER":
                    ContextHeader = headerValue[0];
                    ContextStr = DeserializeHeader&lt;ContextStr&gt;(ContextHeader);
                    break;                                           
            }       
        }   
    }
}

private T DeserializeHeader&lt;T&gt;(string str) where T : new()
{
    if(!string.IsNullOrEmpty(str))
    {
        try
        {
            return JsonSerializer.Deserialize&lt;T&gt;(str);
        }
        catch (Exception e)
        {
            return new T();
        }
    }

    return default;
}

public void SetHeaderValue(string headerValue, string customerPropertyName, string serverPropertyName)
{
    var custProperty = this.GetType().GetProperty(customerPropertyName);        

    if(custProperty != null) { custProperty.SetValue(this, headerValue); }

    if(!string.IsNullOrWhiteSpace(headerValue))
    {      
        var serverProperty = this.GetType().GetProperty(serverPropertyName);

        if(serverProperty != null) { serverProperty.SetValue(this, headerValue); }                       
    }      

}   


I've decided to use switch to group them up, as it would be more readable, and also it has an a performance advantage over if/else blocks as it would be compiled into jump table which would be faster than if/else blocks.