派生类应该调用基类的静态方法,但具有重写的属性

发布于 2024-12-04 15:06:33 字数 683 浏览 0 评论 0原文

我该怎么做?

场景:

abstract class DataAccess
{
    public abstract string ConnectionString { get; set; }

    public static DataTable ExecuteSql(string sql)
    {
        // ...
    }

    public static object ExecuteStoredProc(string storedProcName, ...)
    {
        // ...
    }
}

abstract class DataAccessDb1 : DataAccess
{
    public override string ConnectionString = "SetDbSpecificConnectionStringHere";

    public static DataTable GetStuff()
    {
        // Call the method with the ConnectionString set HERE.
        return ExecuteSql("select * from stuff");
    }
}

我知道可以像在派生类中一样设置连接字符串,但我想将其保持静态,因此我不会在派生类中的每个方法中设置该属性...有什么想法吗?

How do I do that?

The scenario:

abstract class DataAccess
{
    public abstract string ConnectionString { get; set; }

    public static DataTable ExecuteSql(string sql)
    {
        // ...
    }

    public static object ExecuteStoredProc(string storedProcName, ...)
    {
        // ...
    }
}

abstract class DataAccessDb1 : DataAccess
{
    public override string ConnectionString = "SetDbSpecificConnectionStringHere";

    public static DataTable GetStuff()
    {
        // Call the method with the ConnectionString set HERE.
        return ExecuteSql("select * from stuff");
    }
}

I know it's know possible to set the connection string like in the derived class, but I want to keep it static, therefore I won't set the property in every method in the derived class... Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

江南烟雨〆相思醉 2024-12-11 15:06:33

是:将连接字符串作为参数传递到方法中。

  • 字段的行为不具有多态性,因此 DataAccessDb1 中的声明无效
  • static 并且多态性不能混合

所以基本上,如果您想要多态行为,则应该使用实例成员。如果您确实不需要多态行为,请将任何变体(例如连接字符串)作为该方法的参数传递。

Yes: pass the connection string in as a parameter into the method.

  • Fields don't behave polymorphically, so the declaration in DataAccessDb1 is invalid
  • static and polymorphism don't mix

So basically, if you want polymorphic behaviour, you should be using instance members. If you don't really need polymorphic behaviour, pass any variations (such as the connection string) as a parameter for the method.

囚你心 2024-12-11 15:06:33

C# 中没有静态继承,因此让 ConnectionString 具有静态成员并覆盖它的目标将不起作用。重新考虑您的设计 - 将 ConnectionString 设置为静态确实表示该字段在所有 DataAccess 实例上应该相同,因为它是在类型本身上定义的。

使用静态方法是否有特殊原因 - 使用实例方法并在构造函数中设置连接字符串会起作用:

public class DataAccessDb1
{
    public string ConnectionString {get;set;}

    public DataAccessDb1()
    {
        ConnectionString = "SetDbSpecificConnectionStringHere";
    }

    public void DataTable GetStuff()
    {
        return DataAccess.ExecuteSql(ConnectionString, "select * from stuff");
    }
}

There is no static inheritance in C# so your goal of having the ConnectionString has a static member and override it won't work. Rethink your design - having ConnectionString as static really says this field should be the same on all your DataAccess instances since it is defined on the type itself.

Is there a particular reason you use static methods - using instance methods and setting the connection string in the constructor would work:

public class DataAccessDb1
{
    public string ConnectionString {get;set;}

    public DataAccessDb1()
    {
        ConnectionString = "SetDbSpecificConnectionStringHere";
    }

    public void DataTable GetStuff()
    {
        return DataAccess.ExecuteSql(ConnectionString, "select * from stuff");
    }
}
蓝天白云 2024-12-11 15:06:33

我认为抽象类中应该只有一个 ExecuteSql 方法,并且所有派生类都将使用各自的连接字符串实现 ExecuteSql ,

    public abstarct string ConnectionString{get;set;}

可以从基类中删除。

What I think that you should only have a ExecuteSql method in the abstract class and all derived classes would implement ExecuteSql with their respective connection string,

    public abstarct string ConnectionString{get;set;}

can be removed from the base class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文