我一直在使用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/