优化字典c#

时间:2018-10-05 11:34:19

标签: design-patterns

我的代码正在字典中添加对象。我有20多个实例要添加到字典中。如何优化代码?

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

0 个答案:

没有答案