簡體   English   中英

BLL中的靜態方法與實例方法

[英]Static methods vs instance methods in BLL

今天被問到為什么我在asp.net應用程序中為bll類使用這樣的代碼:

public class StudentBll
{
    public static DataTable GetStudents()
    {
        return DBHelper.ExecuteSp("GetStudents");
    }
    public static DataTable GetStudentById(int studentId) 
    {
        return DBHelper.ExecuteSp("GetStudentById", studentId);
    }
}

代替

public class StudentBll
{
    public DataTable GetStudents()
    {
        return DBHelper.ExecuteSp("GetStudents");
    }
    public DataTable GetStudentById(int studentId) 
    {
        return DBHelper.ExecuteSp("GetStudentById", studentId);
    }
}

我唯一能想到的是

A) 性能略有提高(不確定具體細節)

B) 可讀性 StudentBll.GetStudents(); 而不是

StudentBll studentBll = new StudentBll();
studentBll.GetStudents();

但是,我對這些答案不太有信心。 有人願意啟發我嗎?

關於性能,如果您無法顯示增加的幅度,則不支持您的主張。 有人還可能認為,靜態方法調用與實例方法調用的性能提升與往返旅行和數據庫時間的增長微不足道。

您還已經鎖定了實現(或至少迫使使用者進行了一些更難修改的事情)。 如果您將靜態方法和代碼丟失到接口,則不同層次的測試人員和開發人員可以構建模擬,因此不會被迫使用您提供的任何模擬。

當我看到公共靜態方法時,可測試性是我想到的第一件事。 還要忘記oop-這里沒有繼承(類和接口),因此您沒有類實例。 沒有繼承就意味着沒有抽象。 沒有抽象意味着緊密耦合。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM