扩展类及其中一个属性

时间:2013-02-17 17:57:57

标签: c# dependency-injection

我一直在不断地遇到这个类设计问题而且我已经意识到我必须做错事。这个想法是B类:A。这些类使用配置对象和BConfig:AConfig。问题是我无法想出一个很好的方法来使BConfig中的属性可访问。

以下是该问题的简化示例。

public class Request{

  public virtual RequestConfiguration Config{
   get
   {
     if(Config == null)
       Config= ConfigurationManager.GetSection("RequestConfig")
              as RequestConfiguration;
     return Config;
   }
  }

  public virtual string DoSomething(){
    return "Url:" + Config.Url;
  }

}

public class AuthRequest : Request
{
   public override RequestConfiguration Config
   get
   {
     if(Config == null)
       Config= ConfigurationManager.GetSection("RequestConfig")
              as AuthRequestConfiguration;
     return Config;
   }
  }

  public override string DoSomething(){
    return 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
    {
        get { return (string)base["RequestHost"]; }
    }
}    
public class AuthRequestConfiguration : RequestConfiguration
{
    [ConfigurationProperty("User", IsRequired = true)]
    public string User
    {
        get { return (string)base["User"]; }
    }

    [ConfigurationProperty("Pass", IsRequired = true)]
    public string Pass
    {
        get { return (string)base["Pass"]; }
    }
}

显然这段代码没有编译。 是否可以对此代码进行任何微小更改以实现相同的原则?或者我是否需要采取完全不同的方法?

目标是我可以在配置文件中使用简单的依赖注入来确定要发出的请求类型。

2 个答案:

答案 0 :(得分:1)

如何使Request通用; Request<TConfig> where TConfig : RequestConfiguration

public class Request<TConfig> where TConfig : RequestConfiguration
{
    private TConfig _config;

    public virtual TConfig Config
    {
        get { return _config ?? (_config = ConfigurationManager.GetSection("RequestConfig") as TConfig); }
    }

    public virtual string DoSomething()
    {
        return "Url:" + Config.Url;
    }
}

public class AuthRequest : Request<AuthRequestConfiguration>
{
    public override string DoSomething()
    {
        return 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
    {
        get { return (string) this["Url"]; }
    }
}

public class AuthRequestConfiguration : RequestConfiguration
{
    [ConfigurationProperty("User", IsRequired = true)]
    public string User
    {
        get { return (string) this["User"]; }
    }

    [ConfigurationProperty("Pass", IsRequired = true)]
    public string Pass
    {
        get { return (string) this["Pass"]; }
    }
}

这会解决您的问题吗?至少代码构建:)

答案 1 :(得分:0)

这取决于如何在RequestConfiguration和AuthRequestConfiguration上声明属性。

例如,如果您在RequestConfiguration类中将Url属性声明为virtual并在AuthRequestConfiguration类中将其声明为覆盖,则它应该执行您期望的操作。

MSDN的详细示例在http://msdn.microsoft.com/en-us/library/9fkccyh4(v=vs.80).aspx