【发布时间】:2015-06-03 16:58:50
【问题描述】:
我对存储库和模拟存储库相当陌生,所以我想帮助理解这个错误的确切含义以及如何避免它。我查看了其他帖子并进行了研究,但它们对错误过于具体。 正在破坏的代码是
public int PersistComponentDb(Core.Models.ComponentDbModel componentDb)
{
return PersistComponentDb(componentDb);
}
我知道该函数正在调用自己,但我该如何阻止它这样做呢?它正在持久化一个模拟存储库,硬编码的值看起来像这样
public DataTable GetComponentDbs(int? forComponentId = null, int? forDbServerId = null, int? forEntityId = null)
{
DataTable componentDbs = new DataTable();
componentDbs.Columns.Add("ComponentDbID", typeof(Int32));
componentDbs.Columns.Add("ComponentID", typeof(Int32));
componentDbs.Columns.Add("DbServerID", typeof(Int32));
componentDbs.Columns.Add("EntityID", typeof(int));
componentDbs.Columns.Add("SecurableGuid", typeof(String));
componentDbs.Columns.Add("FriendlyName", typeof(String));
componentDbs.Columns.Add("DbName", typeof(String));
componentDbs.Rows.Add(new object[] { 1, 2, 2, 3822, "SecureableGuid", "Test #1", "DB #1" });
return componentDbs;
}
public DataRow GetComponentDb(int id)
{
DataTable componentDbs = new DataTable();
componentDbs.Columns.Add("ComponentDbID", typeof(Int32));
componentDbs.Columns.Add("ComponentID", typeof(Int32));
componentDbs.Columns.Add("DbServerID", typeof(Int32));
componentDbs.Columns.Add("EntityID", typeof(Int32));
componentDbs.Columns.Add("SecurableGuid", typeof(String));
componentDbs.Columns.Add("FriendlyName", typeof(String));
componentDbs.Columns.Add("DbName", typeof(String));
componentDbs.Rows.Add(new object[] { 123, 121, 12, null, "SecurableGuid", "Name", "Database name" });
return componentDbs.Rows[0];
}
PersistComponentDb 后面的其余代码是
public int PersistComponentDb(ComponentDbModel componentDb)
{
// Example implementation.
return _repository.PersistComponentDb(componentDb);
}
和
int PersistComponentDb(ComponentDbModel componentDb);
【问题讨论】:
-
@PrestonGuillot 我猜这是一个错字,
new不应该在那里。 -
@PrestonGuillot 你是对的,我在发布之前就已经尝试过了。我已经编辑了它是什么。
-
PersistCompnentDB应该做什么?我不确定这甚至应该是一个递归调用,但如果是你需要一个基本案例,你应该将问题分解为类似的子问题。所以应该至少有一个返回不进行递归调用,并且递归调用不应与原始调用相同。 -
@juharr 'PersistComponentDB' 假设更新并保存到“数据库”中,'int PersistComponentDb(ComponentDbModel componentDb);'和 ' public int PersistComponentDb(ComponentDbModel componentDb) { // 示例实现。 return _repository.PersistComponentDb(componentDb); }' 是 'PersistComponentDb' 后面的其余代码
-
好吧
retrun _repository.PersistComponentDb(componentDb);会起作用,因为它会在不同的对象上调用类似命名的方法。你有一个名为_repository的变量吗?
标签: c# asp.net asp.net-mvc repository