asp.net-mvc - 有什么理由为什么我的存储库方法不应该是静态的?

标签 asp.net-mvc static repository

我一直在使用MVC应用程序并创建用于处理,验证,更新和读取/写入数据的存储库。它们都是静态的。这是一个例子:

public static int Create(user u)
{
      using(DataContext db = new DataContext())
      {
          //do the thing and submit changes...
      }

      //return the new user id
}

(Note: this is just a sample, I am not looking for tips about creating users or returning user ids, etc.)



然后我可以调用int id = RepoClassName.Create(userVariable);
使用这样的静态方法有什么问题吗?我只是不明白为什么我需要实例化一个对象来做到这一点。

最佳答案

好吧,如果您不打算解耦,测试和轻松维护您的“存储库”,我想静态就可以了。

如果您想更多地了解为什么将静态方法视为代码气味,请添加here's a nice article at the Google Testing Blog。当然,这假定您完全关心测试代码。

但是,嘿,这是2011年,谁不会呢!

关于asp.net-mvc - 有什么理由为什么我的存储库方法不应该是静态的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7123953/

相关文章:

java - Java中静态字段的确切含义是什么?

git - 在 GIT 中的 Rep. 中生成发行说明

java - Spring 启动: Repository interface not found with annotations

git - 如何使用 Aptana Studio 3 设置 Git?

asp.net-mvc - 回发到 Controller 操作后 MVC ViewData 模型为空

c# - 在 Razor View 中使用 Html.DisplayFor 应用 css 类

c# - 与 TransactionScope 一起使用时 MVC 5 Identity 抛出超时异常

java - 将值从一个非静态方法传递到另一个主方法?

c# - ASP.NET MVC 路由 : Can I have an action name with a slash in it?

Django测试加载css文件