为什么要使用访问数据库的静态方法 [英] Why would I use static methods for database access

查看:189
本文介绍了为什么要使用访问数据库的静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我碰到这个今天的问题,我无法找到一些有意义的解释是有一些非主观原因是使用静态方法,当涉及到数据库交互。

So I came across this issues today and I couldn't find some meaningful explanation is there some non-subjective reason to use static methods when it comes to database interactions.

现在我的工作,一切都在通过存储过程制成,例如我有一些常规的方法,如一个项目:

Right now I'm working on a project where everything is made through stored procedures and for example I have some regular methods like :

public void Delete(int clientID)
    {
      //calling store procedure
    }

public void Save(int clientID)
    {
      //calling store procedure
    }

但我也有:

public static Client GetByID(int id)
    {
        //calling stored procedure
    }

public static int AddNew(string firstName, string lastName...)
    {
      //calling store procedure
    }

和,因为我与 .NET 工作了约9个月,我一直只使用实体框架 Repository模式我到处都记得或者使用了静态方法的任何代码。不为标准 CRUD 操作,既不是与数据库相关的更具体的任务。

and since I'm working with .NET for about 9 months and I've been using only Entity Framework and Repository Pattern I can't recall anywhere or any code where static methods were used. Not for standard CRUD operations, neither for more specific tasks related to the database.

那么,这个事情关系到数据库访问的特殊方式,是它的一些做法,可以给(即使是非常小的)的性能提升,或者它只是开发商办法,我不应该给它多一个思考的时候,什么时候不能用静态方法在我的数据库相关的方法?

So is this something related to the particular way that the database is accessed, is it some practice that can give (even a very small) performance boost, or it's just the developers approach and I shouldn't give it much of a thought when and when to not use static methods in my database related methods?

推荐答案

在数据访问层的特定情况下我会避免一个简单的原因静态方法...耦合。

In the particular case of a data access layer I'd avoid static methods for one simple reason... coupling.

静态方法不能实现一个接口,实例方法可以。通过使用静态方法之一基本上坚持对编码到接口,而是编码到一个实现。因此,使用这种数据访问层一切的需要的这个的此特定的时刻数据访问层。

Static methods can't implement an interface, instance methods can. By using static methods one is essentially insisting against coding to an interface and instead coding to an implementation. Thus, everything which uses this data access layer is required to this this specific data access layer at all times.

无备用实现,没有测试存根,无依赖倒置的。业务逻辑具有一个依赖箭头指向一个基础设施的关注(数据访问层),而应该是周围的其他方法

No alternate implementations, no test stubs, no dependency inversion at all. The business logic has a dependency arrow pointing to an infrastructure concern (the data access layer), whereas that should be the other way around.

此外,它看起来这至少携带一个更大的危险的具有支配资源的问题。这也许不是这里的情况,但它真的很容易为它成为的情况。如果开发者在道路上的某个地方有好主意来提取代码的公用线到类级静态方法和属性?有点像连接的DbContext 对象吗?这将创建一些非常有趣和的非常的难以调试运行时错误。

Additionally, it seems like this at least carries a greater risk of having problems with the disposal of resources. That might not be the case here, but it's really easy for it to become the case. What if a developer somewhere down the road has the bright idea to extract common lines of code into a class-level static method and property? Something like the Connection or DBContext object? That'll create some very interesting and very difficult-to-debug run-time errors.

在另一方面,如果存储库是实例然后他们可以简单地实施的IDisposable ,并确保任何类级别的对象正确处置。

On the other hand, if repositories were instances then they can simply implement IDisposable and make sure any class-level objects are correctly disposed.

继续(我想我有更多的异议,比我想象的设计),这样的感觉的从面向对象的意识非常反直觉给我。也许这一次仅仅是个人喜好,但是这是转弯,否则这将是一个库对象变成了DB helper方法倾倒场。

Continuing (I guess I had more objections to the design than I thought), this feels very counter-intuitive to me from an object-oriented sense. Perhaps this one is just personal preference, but this is turning what would otherwise be a "repository object" into a "dumping ground of DB helper methods."

在一个系统这样我期望的随机一次性的方法数为开发人员做出快速的解决方案,以满足需求,而不考虑整体架构随着时间的推移显著增长。取而代之的是一致的,管理完善的一系列对象,你可以很可能有臃肿,难以跟踪代码库中结束。

In a system like this I would expect the number of random one-off methods to grow significantly over time as developers make quick solutions to meet requirements without thinking about the overall architecture. Instead of a consistent and well-managed series of objects, you could very likely end up with a bloated and difficult-to-follow codebase.

这篇关于为什么要使用访问数据库的静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆