简体   繁体   English

Class 在 C# 中设计 - DAL

[英]Class Designing in C# - DAL

I came across a Data Access Layer Class that was made up entirely of static methods.我遇到了一个完全由 static 方法组成的数据访问层 Class。 This class was consumed by a Web Application此 class 由 Web 应用程序使用

For eg例如

public class DataAccessLayer
{
Public static PersonDetails GetDetails(int iPersonID);
{
//implementation
}
Public static bool SaveDetails(PersonDetails objPerson);
{
//implementation
}

}

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, but will this cause any concurrency errors when multiple users call the Page?我可以理解使用 static 方法时性能会稍微好一些,但是当多个用户调用页面时会导致任何并发错误吗?

Static or not, you may get concurrency issues, it depends very much on how the actual code in your data access methods is written (eg use of transactions, are there fields used for concurrency checking in your data source etc.). Static 与否,您可能会遇到并发问题,这在很大程度上取决于您的数据访问方法中的实际代码是如何编写的(例如,事务的使用,您的数据源中是否有用于并发检查的字段等)。

Is it a good practice to write such code.编写这样的代码是一个好习惯吗?

Firing offense in my teams, breaks so many good practices I suggest whoever writes something like that goes to my competitors and works for them.在我的团队中发起进攻,破坏了如此多的良好做法,我建议写出类似内容的人去找我的竞争对手并为他们工作。

I can understand the fact that the performance will be slightly better when I use static methods.我可以理解,当我使用 static 方法时性能会稍微好一些。

Like 0.000000000000000000001%?比如 0.0000000000000000000001%?

but will this cause any concurrency errors when multiple users call the Page?但是当多个用户调用页面时,这会导致任何并发错误吗?

Depends how the methods are written.取决于方法的编写方式。 It violates object orientation, though, makes dependency injection harder, makes it hard to use proper transactional demarcations under a transaction coordinator and makes it hard to unit test / mock things.但是,它违反了 object 方向,使依赖注入更加困难,难以在事务协调器下使用正确的事务分界,并且难以对事物进行单元测试/模拟。

The static variable dies in a standalone application, because the garbage collector cleans all your object because your application has ended. static 变量在独立应用程序中消失,因为垃圾收集器会清理所有 object,因为您的应用程序已经结束。

But if you are using this in a webapplication, your application is not dead until your web server dies.但是,如果您在 Web 应用程序中使用它,则您的应用程序不会死,直到您的 web 服务器死亡。 So even if you closes the web browser, your web server is not close.所以即使你关闭了 web 浏览器,你的 web 服务器也没有关闭。 Try to shut down your server and your variable will be no more.尝试关闭您的服务器,您的变量将不再存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM