0
public static class WebConfig
 {     
private static IList<SystemConfigs> sysconfkeys;
static WebConfig()
{         
    sysconfkeys = systemconfigrep.GetAllSystemConfig();
}

private static string _SMTPPort = "";
public static string SMTPPort
{
    get
    {
        return _SMTPPort = sysconfkeys.FirstOrDefault(e => e.metakey == "SMTPPort") != null ? sysconfkeys.FirstOrDefault(e => e.metakey == "SMTPPort").metavalue : "25";
    }
}

private static string _SMTPUsername = "";
public static string SMTPUsername
{
    get
    {
        return _SMTPUsername = sysconfkeys.FirstOrDefault(e => e.metakey == "SMTPUsername") != null ? sysconfkeys.FirstOrDefault(e => e.metakey == "SMTPUsername").metavalue : "";
    }
}

}

i am using the system level variable in the above way all over my system in my views or controllers. how can i design my this class in such a way that after calling a method to execute a query i straight away call some property like this.

usage: webconfig.getallsystemconfig().LogicalPath;

webconfig.getallsystemconfig().Smtpport;

4

1 回答 1

1

you could use extension methods to accomplish something like this:

public static class WebConfig
{
    private static IList<SystemConfigs> sysconfkeys;

    public static IList<SystemConfigs> Configs()
    {                                   
        return sysconfkeys ?? (sysconfkeys = systemconfigrep.GetAllSystemConfig());
    }

    public static string SMTPPort(this IList<SystemConfigs> configs)
    {
         return sysconfkeys.Any(e => e.metakey == "SMTPPort") ? sysconfkeys.FirstOrDefault(e => e.metakey == "SMTPPort").metavalue : "25";
    }
}

You would use it like

WebConfig.Configs().SMTPPort();

if doesn't do the caching as in your example, but it should be a negligible loss anyway

于 2013-04-30T09:36:48.117 回答