vendredi 5 octobre 2018

Optimize creation of object in dictionary c#

public static Dictionary<string, Func<ReportsFactoryService>> objects =
           new Dictionary<string, Func<ReportsFactoryService>>
               {
                        {"level1", () => new TrialBalanceLevel1Service()},
                        {"level2", () => new TrialBalanceLevel2Service()},
                        {"level3", () => new TrialBalanceLevel3Service()},
                        {"level4", () => new TrialBalanceLevel4Service()},
                        {"level4-RSA", () => new TrialBalanceLevel4_ACSService()},
                        {"level4-ACS", () => new TrialBalanceLevel4_RSAService()}
               };

    public IValidationDictionary SetModelState { set { validationDictionary = value; } }

    public ReportsFactoryService CreateObject(string businessObjectName)
    {
        if (string.IsNullOrWhiteSpace(businessObjectName))
        {
            return new ReportsFactoryService();
        }

        Func<ReportsFactoryService> objectCtor = null;
        objects.TryGetValue(businessObjectName.ToLower(), out objectCtor);

        return objectCtor != null ? objectCtor() : new ReportsFactoryService();
    }

The above code is adding objects in dictionary. I have more than 20 instances to add to dictionary. Kindly let me know how can i optimize the above code. Thanks

Aucun commentaire:

Enregistrer un commentaire