Saturday, 15 September 2012

c# - Extending a class and one of its properites -



c# - Extending a class and one of its properites -

there class design issue maintain running on , on , i've come realize must doing wrong. thought class b : a. these classes using configuration objects , bconfig : aconfig. issue can't come way create properties in bconfig accessible.

here's simplified illustration of issue.

public class request{ public virtual requestconfiguration config{ { if(config == null) config= configurationmanager.getsection("requestconfig") requestconfiguration; homecoming config; } } public virtual string dosomething(){ homecoming "url:" + config.url; } } public class authrequest : request { public override requestconfiguration config { if(config == null) config= configurationmanager.getsection("requestconfig") authrequestconfiguration; homecoming config; } } public override string dosomething(){ homecoming string.format("url:{0} u:{1} p:{2}",config.url,config.user,config.pass); } } /*---- configuration classes ----*/ public class requestconfiguration : configurationsection { [configurationproperty("requesthost", isrequired = true)] public string requesthost { { homecoming (string)base["requesthost"]; } } } public class authrequestconfiguration : requestconfiguration { [configurationproperty("user", isrequired = true)] public string user { { homecoming (string)base["user"]; } } [configurationproperty("pass", isrequired = true)] public string pass { { homecoming (string)base["pass"]; } } }

obviously code doesn't compile. is there little changes code made accomplish same principle? or need take whole different approach together?

the goal can utilize simple dependency injection set in configuration file determine type of request make.

what making request generic; request<tconfig> tconfig : requestconfiguration:

class="lang-cs prettyprint-override">public class request<tconfig> tconfig : requestconfiguration { private tconfig _config; public virtual tconfig config { { homecoming _config ?? (_config = configurationmanager.getsection("requestconfig") tconfig); } } public virtual string dosomething() { homecoming "url:" + config.url; } } public class authrequest : request<authrequestconfiguration> { public override string dosomething() { homecoming string.format("url:{0} u:{1} p:{2}", config.url, config.user, config.pass); } } public class requestconfiguration : configurationsection { [configurationproperty("url", isrequired = true)] public string url { { homecoming (string) this["url"]; } } } public class authrequestconfiguration : requestconfiguration { [configurationproperty("user", isrequired = true)] public string user { { homecoming (string) this["user"]; } } [configurationproperty("pass", isrequired = true)] public string pass { { homecoming (string) this["pass"]; } } }

would solve problem? @ to the lowest degree code builds :)

c# dependency-injection software-engineering

No comments:

Post a Comment