我遇到了一个完全由静态方法组成的数据访问层类。此类由 Web 应用程序使用
例如
public class DataAccessLayer
{
Public static PersonDetails GetDetails(int iPersonID);
{
//implementation
}
Public static bool SaveDetails(PersonDetails objPerson);
{
//implementation
}
}
写这样的代码是不是一个好习惯。我可以理解使用静态方法时性能会稍微好一些,但是当多个用户调用Page时会不会导致并发错误?
最佳答案
Is it a good practice to write such code.
在我的团队中开火,破坏了很多好的做法,我建议任何写出类似东西的人都去给我的竞争对手并为他们工作。
I can understand the fact that the performance will be slightly better when I use static methods.
比如 0.000000000000000000001%?
but will this cause any concurrency errors when multiple users call the Page?
取决于方法的编写方式。但是,它违反了面向对象,使依赖注入(inject)变得更加困难,难以在事务协调器下使用适当的事务分界,并且难以对事物进行单元测试/模拟。
关于c# - C# 中的类设计 - DAL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6733100/